<template>
  <div class="club-info">
    <el-form :model="formData" :rules="rules" ref="form" label-position="left">
      <el-form-item prop="name" label="机构名称:">
        <el-input
          placeholder="请输入机构名称"
          v-model="formData.name"
        ></el-input>
      </el-form-item>
      <el-form-item prop="mobile" label="联系电话:">
        <el-input
          placeholder="请输入对外联系电话"
          v-model="formData.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="address" label="所在地区:">
        <br />
        <input type="text" v-model="formData.address" v-show="false" />
        <div class="flex items-center justify-between">
          <el-select
            placeholder="请选择"
            v-model="formData.provinceId"
            @change="onProvinceChange"
          >
            <template v-for="item in provinceList">
              <el-option :label="item.name" :value="item.id" :key="item.id">
              </el-option>
            </template>
          </el-select>
          <el-select
            placeholder="请选择"
            v-model="formData.cityId"
            @change="onCityChange"
          >
            <template v-for="item in cityList">
              <el-option :label="item.name" :value="item.id" :key="item.id">
              </el-option>
            </template>
          </el-select>
          <el-select
            placeholder="请选择"
            v-model="formData.townId"
            @change="onTownChange"
          >
            <template v-for="item in townList">
              <el-option :label="item.name" :value="item.id" :key="item.id">
              </el-option>
            </template>
          </el-select>
        </div>
        <el-input
          class="mt-4"
          type="textarea"
          :rows="4"
          v-model="formData.fullAddress"
          @input="onFullAddressInput"
          placeholder="建议您如实填写详细收货地址，例如：街道名称，门牌号码，楼层和房间号等信息"
        ></el-input>
      </el-form-item>
      <el-form-item prop="point" label="">
        <el-input v-model="formData.point" v-show="false"></el-input>
        <div class="normal-row">
          <div class="label">
            <i>*</i>所在位置:<span
              >（提示:打开地图，将定位图标移到具体位置）</span
            >
          </div>
          <div class="postion-btn">定位</div>
        </div>
      </el-form-item>
      <el-form-item prop="logoImage" label="logo:">
        <br />
        <el-input v-show="false" v-model="formData.logoImage" />
        <SimpleUploadImage
          :limit="1"
          :image-list="logoList"
          :before-upload="beforeLogoUpload"
          @success="uploadLogoSuccess"
          @remove="handleLogoRemove"
        />
      </el-form-item>
      <el-form-item prop="banner">
        <div class="normal-row">
          <div class="label"><i>*</i>门头照:<span>（可上传6张）</span></div>
          <el-input v-show="false" v-model="formData.banner" />
          <SimpleUploadImage
            :limit="6"
            :image-list="bannerList"
            :before-upload="beforeBannerUpload"
            @success="uploadBannerSuccess"
            @remove="handleBannerRemove"
          />
        </div>
      </el-form-item>

      <el-form-item label="机构类型:">
        <el-radio-group v-model="formData.firstClubType">
          <el-radio :label="1">医美</el-radio>
          <el-radio :label="2">生美</el-radio>
          <el-radio :label="3">项目公司</el-radio>
          <el-radio :label="4">个人</el-radio>
          <el-radio :label="5">其他</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="formData.firstClubType === 1" label="医美类型:">
        <el-radio-group v-model="formData.secondClubType">
          <el-radio :label="1">诊所</el-radio>
          <el-radio :label="2">门诊</el-radio>
          <el-radio :label="3">医院</el-radio>
          <el-radio :label="4">其他</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="formData.firstClubType === 2" label="生美类型:">
        <el-radio-group v-model="formData.secondClubType">
          <el-radio :label="5">美容院</el-radio>
          <el-radio :label="6">养生馆</el-radio>
          <el-radio :label="7">其他</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="医疗许可证:" prop="medicalLicenseImage">
        <br />
        <el-input v-show="false" v-model="formData.medicalLicenseImage" />
        <SimpleUploadImage
          :limit="1"
          :image-list="licenseImageList"
          :before-upload="beforeLicenseImageUpload"
          @success="uploadLicenseImageSuccess"
          @remove="handleLicenseImageRemove"
        />
      </el-form-item>

      <el-form-item label="员工人数:" prop="empNum">
        <el-input
          v-model.number="formData.empNum"
          placeholder="请输入员工人数"
          clearable
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import SimpleUploadImage from '@/components/SimpleUploadImage'
import SimpleRadio from '@/components/SimpleRadio'
import { isPoint, isNumber } from '@/utils/validator'
export default {
  components: {
    SimpleUploadImage,
    SimpleRadio,
  },
  data() {
    var validatePoint = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('经纬度坐标不能为空'))
      } else {
        if (isPoint(value)) {
          callback()
        } else {
          callback(
            new Error('经纬度坐标格式不正确，(例如：114.095294,22.536004)')
          )
        }
      }
    }

    var validateMobile = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('联系方式不能为空'))
      } else {
        if (isNumber(value)) {
          callback()
        } else {
          callback(new Error('联系方式格式不正确'))
        }
      }
    }

    var validateAddress = (rule, value, callback) => {
      if (
        !this.formData.provinceId ||
        !this.formData.cityId ||
        !this.formData.townId ||
        !this.formData.fullAddress
      ) {
        callback(new Error('请输入完整的地址'))
      } else {
        callback()
      }
    }

    return {
      clubTypeList: [
        { value: 1, name: '医美' },
        { value: 2, name: '生美' },
        { value: 3, name: '项目公司' },
        { value: 4, name: '个人' },
        { value: 5, name: '其他' },
      ],
      medicalTypeList: [
        { value: 1, name: '诊所' },
        { value: 2, name: '门诊' },
        { value: 3, name: '医院' },
        { value: 4, name: '其他' },
      ],
      formData: {
        name: '',
        address: [],
        fullAddress: '',
        point: '',
        mobile: '',
        userMobile: '',
        logoImage: '',
        banner: '',
        customFlag: 0,
        remarks: '',
        empNum: '',
        firstClubType: 1,
        secondClubType: 1,
        medicalLicenseImage: '',
        provinceId: '',
        cityId: '',
        townId: '',
      },
      rules: {
        name: [
          { required: true, message: '机构名称不能为空', trigger: ['blur'] },
        ],
        logoImage: [
          { required: true, message: '请上传logo', trigger: ['change'] },
        ],
        mobile: [
          {
            required: true,
            validator: validateMobile,
            trigger: ['blur', 'change'],
          },
        ],
        point: [
          {
            required: true,
            validator: validatePoint,
            trigger: ['change'],
          },
        ],
        address: [
          {
            required: true,
            message: '所在地区不能为空',
            trigger: ['change'],
          },
          {
            validator: validateAddress,
            trigger: ['change'],
          },
        ],
        banner: [
          {
            required: true,
            message: '门头照不能为空',
            trigger: ['change'],
          },
        ],
        empNum: [
          { required: true, message: '员工人数不能为空', trigger: ['blur'] },
        ],
      },
      // logo图片列表
      logoList: [],
      // banner图片列表
      bannerList: [],
      // 级联选择的地址
      address: '',
      // 医疗许可证图片
      licenseImageList: [],

      provinceList: [],
    }
  },
  computed: {
    cityList() {
      const province = this.provinceList.find(
        (item) => item.id === this.formData.provinceId
      )
      if (province) {
        return province.children
      }
      return []
    },
    townList() {
      const city = this.cityList.find(
        (item) => item.id === this.formData.cityId
      )
      if (city) {
        return city.children
      }
      return []
    },
  },
  created() {
    this.fetchAllCityList()
  },
  methods: {
    async fetchAllCityList() {
      try {
        const res = await this.$http.api.fetchAllCityList()
        console.log(res)
        this.provinceList = res.data
      } catch (error) {
        console.log(error)
      }
    },

    // 表单验证
    validate() {
      this.$emit('step', this.formData)
      return this.$refs.form.validate()
    },

    onProvinceChange() {
      this.formData.cityId = ''
      this.formData.townId = ''
      this.countAddress()
    },
    onCityChange() {
      this.formData.townId = ''
      this.countAddress()
    },
    onTownChange() {
      this.countAddress()
    },

    onFullAddressInput() {
      this.countAddress()
    },

    countAddress() {
      this.formData.address =
        this.formData.cityId +
        this.formData.provinceId +
        this.formData.townId +
        this.formData.fullAddress
    },

    // logo上传
    uploadLogoSuccess({ response, file, fileList }) {
      this.logoList = fileList
      this.formData.logoImage = fileList[0].response.data
    },
    handleLogoRemove({ file, fileList }) {
      this.logoList = fileList
      this.formData.logoImage = ''
    },
    beforeLogoUpload(file) {
      const flag = file.size / 1024 < 500
      if (!flag) {
        this.$message.error('上传logo图片大小不能超过 500kb!')
      }
      return flag
    },

    // banner上传
    uploadBannerSuccess({ response, file, fileList }) {
      this.bannerList = fileList
      console.log(this.bannerList)
      this.formData.banner = fileList.length > 0 ? fileList.length : ''
    },
    handleBannerRemove({ file, fileList }) {
      this.bannerList = fileList
      this.formData.banner = fileList.length > 0 ? fileList.length : ''
    },
    beforeBannerUpload(file) {
      const flag = file.size / 1024 / 1024 < 1
      if (!flag) {
        this.$message.error('上传banner图片大小不能超过 1MB!')
      }
      return flag
    },

    // 医疗许可证图片上传
    uploadLicenseImageSuccess({ response, file, fileList }) {
      this.licenseImageList = fileList
      console.log(this.licenseImageList)
      this.formData.medicalLicenseImage = response.data
    },
    handleLicenseImageRemove({ file, fileList }) {
      this.licenseImageList = fileList
      this.formData.medicalLicenseImage = ''
    },
    beforeLicenseImageUpload(file) {
      const flag = file.size / 1024 / 1024 < 1
      if (!flag) {
        this.$message.error('医疗许可证图片大小不能超过 1MB!')
      }
      return flag
    },
  },
}
</script>

<style lang="scss" scoped>
// pc端
@media screen and (min-width: 768px) {
  .normal-row {
    position: relative;
    .label {
      font-size: 14px;
      color: #606266;

      i {
        color: #f56c6c;
        margin-right: 4px;
      }

      span {
        color: #b2b2b2;
      }
    }
    .postion-btn {
      position: absolute;
      top: 50%;
      right: 0;
      transform: translateY(-50%);
      width: 62px;
      height: 28px;
      line-height: 28px;
      font-size: 14px;
      color: #fff;
      background: #1890ff;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      border-radius: 4px;

      &::before {
        content: '';
        display: inline-block;
        width: 16px;
        height: 16px;
        background: url(https://static.caimei365.com/www/authentic/pc/icon-position.png)
          no-repeat center;
        background-size: 16px 16px;
      }
    }
  }
}

// 移动端
@media screen and (max-width: 768px) {
  ::v-deep {
    .el-form-item__label {
      font-size: 3.4vw;
    }
  }

  .normal-row {
    position: relative;
    .label {
      font-size: 14px;
      color: #606266;

      i {
        color: #f56c6c;
        margin-right: 4px;
      }

      span {
        color: #b2b2b2;
        font-size: 2.6vw;
      }
    }
    .postion-btn {
      position: absolute;
      top: 50%;
      right: 0;
      transform: translateY(-50%);
      width: 14vw;
      height: 6.8vw;
      line-height: 6.8vw;
      font-size: 3.2vw;
      color: #fff;
      background: #1890ff;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      border-radius: 0.4vw;

      &::before {
        content: '';
        display: inline-block;
        width: 3.58vw;
        height: 3.58vw;
        background: url(https://static.caimei365.com/www/authentic/pc/icon-position.png)
          no-repeat center;
        background-size: 3.58vw;
      }
    }
  }
}
</style>
