<template>
  <a-modal
    centered
    class="supplierInfoEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancle="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 表单 -->
      <div>
        <a-form-model
          id="supplierInfoEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="供应商名称" prop="supplierName">
            <a-input
              id="supplierInfoEdit-supplierName"
              :maxLength="30"
              v-model.trim="form.supplierName"
              @change="filterEmpty"
              placeholder="请输入供应商名称(最多30个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="所在地区" required style="margin: 0;">
            <a-row :gutter="20">
              <!-- 所在地区 -->
              <a-col span="8">
                <a-form-model-item prop="provinceSn">
                  <a-select id="supplierInfoEdit-provinceSn" allowClear @change="getCityList" v-model="form.provinceSn" placeholder="请选择省">
                    <a-select-option v-for="item in addrProvinceList" :value="item.areaSn" :key="item.areaSn + 'a'">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col span="8">
                <a-form-model-item prop="citySn">
                  <a-select id="supplierInfoEdit-citySn" allowClear @change="getAreaList" v-model="form.citySn" placeholder="请选择市">
                    <a-select-option v-for="item in addrCityList" :value="item.areaSn" :key="item.areaSn + 'b'">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col span="8">
                <a-form-model-item prop="countySn">
                  <a-select id="supplierInfoEdit-countySn" allowClear @change="areaCharged" v-model="form.countySn" placeholder="请选择区/县">
                    <a-select-option v-for="item in addrDistrictList" :value="item.areaSn" :key="item.areaSn + 'c'">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model-item>
          <a-form-model-item label="详细地址" prop="supplierAddress">
            <a-input
              id="supplierInfoEdit-supplierAddress"
              :maxLength="60"
              v-model.trim="form.supplierAddress"
              placeholder="请输入详细地址(最多60个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="联系人" prop="contactPerson">
            <a-input
              id="supplierInfoEdit-contactPerson"
              :maxLength="30"
              v-model.trim="form.contactPerson"
              placeholder="请输入联系人(最多30个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="联系电话" prop="contactTel">
            <a-input
              id="supplierInfoEdit-contactTel"
              :maxLength="20"
              v-model.trim="form.contactTel"
              placeholder="请输入联系电话(最多20个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="主营内容" prop="businessScope">
            <a-textarea
              id="supplierInfoEdit-businessScope"
              :maxLength="200"
              v-model="form.businessScope"
              placeholder="请输入主营内容(最多200个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="状态" prop="enabledFlag" v-if="isState">
            <a-radio-group
              name="radioGroup"
              v-model="form.enabledFlag"
              id="supplierInfoEdit-enabledFlag" >
              <a-radio :value="1">启用</a-radio>
              <a-radio :value="0">禁用</a-radio>
            </a-radio-group>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="supplierInfoEdit-btn-submit" @click="handleSubmit">保存</a-button>
          <a-button id="supplierInfoEdit-btn-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { getArea } from '@/api/data'
import { supplierDetail, supplierSave } from '@/api/supplier'
import { commonMixin } from '@/utils/mixin'
export default {
  name: 'SupplierInfoEditModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {
      type: [Number, String],
      default: ''
    },
    isState: { //  是否显示状态
      type: Boolean,
      default: true
    }
  },
  computed: {
    modalTit () {
      return this.itemId ? '编辑' : '新增'
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        supplierName: '', // 供应商名称
        provinceSn: undefined, //  省
        provinceName: '',
        citySn: undefined, // 市
        cityName: '',
        countySn: undefined, // 区
        countyName: '',
        supplierAddress: '', //  详细地址
        contactPerson: '', //  联系人
        contactTel: '', //  联系电话
        businessScope: '', //  主营内容
        enabledFlag: 1 //  启禁用
      },
      rules: {
        supplierName: [
          { required: true, message: '请输入供应商名称', trigger: 'blur' }
        ],
        provinceSn: [
          { required: true, message: '请选择省', trigger: 'change' }
        ],
        citySn: [
          { required: true, message: '请选择市', trigger: 'change' }
        ],
        countySn: [
          { required: true, message: '请选择区/县', trigger: 'change' }
        ],
        supplierAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ],
        contactPerson: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        contactTel: [
          { required: true, message: '请输入联系人电话', trigger: 'blur' }

        ],
        enabledFlag: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      },
      addrProvinceList: [], //  省下拉
      addrCityList: [], //  市下拉
      addrDistrictList: [] //  区下拉
    }
  },
  methods: {
    filterEmpty () {
      let str = this.form.supplierName
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.supplierName = str
    },
    //  获取供应商信息
    getDetail () {
      supplierDetail({ id: this.itemId }).then(res => {
        if (res.status == 200) {
          if (res.data.provinceSn) { this.getArea('city', res.data.provinceSn) }
          if (res.data.citySn) { this.getArea('district', res.data.citySn) }
          this.form = Object.assign(this.form, res.data)
          this.form.enabledFlag = Number(this.form.enabledFlag)
        }
      })
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = _this.form
          form.id = this.itemId ? this.itemId : null
          _this.spinning = true
          supplierSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              _this.isShow = false
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 获取城市列表
    getCityList (val) {
      this.addrCityList = []
      this.addrDistrictList = []
      this.form.citySn = undefined
      this.form.countySn = undefined
      if (val) {
        this.getArea('city', val)
        this.form.provinceName = this.addrProvinceList.find(item => item.areaSn == val).name
      } else {
        this.form.provinceName = ''
      }
    },
    // 获取区县列表
    getAreaList (val) {
      this.addrDistrictList = []
      this.form.countySn = undefined
      if (val) {
        this.getArea('district', val)
        this.form.cityName = this.addrCityList.find(item => item.areaSn == val).name
      } else {
        this.form.cityName = ''
      }
    },
    // 区县变更
    areaCharged (val) {
      if (val) {
        this.form.countyName = this.addrDistrictList.find(item => item.areaSn == val).name
      } else {
        this.form.countyName = ''
      }
    },
    //  省/市/区
    getArea (leve, sn) {
      let params
      if (leve == 'province') {
        params = { type: '2' }
      } else {
        params = { parentId: sn, type: leve == 'city' ? '3' : '4' }
      }
      getArea(params).then(res => {
        if (res.status == 200) {
          if (leve == 'province') {
            this.addrProvinceList = res.data || []
          } else if (leve == 'city') {
            this.addrCityList = res.data || []
          } else if (leve == 'district') {
            this.addrDistrictList = res.data || []
          }
        } else {
          if (leve == 'province') {
            this.addrProvinceList = []
          } else if (leve == 'city') {
            this.addrCityList = []
          } else if (leve == 'district') {
            this.addrDistrictList = []
          }
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
        this.addrCityList = []
        this.addrDistrictList = []
      } else {
        this.getArea('province')
      }
    },
    itemId (newValue, oldValue) {
      if (this.isShow && newValue) { //  编辑
        this.getDetail()
      }
    }
  }
}
</script>

<style lang="less">
  .supplierInfoEdit-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
