<template>
  <div class="supplierInfoEdit-wrap">
    <a-page-header :ghost="false" :backIcon="false" class="supplierInfoEdit-cont">
      <template slot="subTitle">
        <a href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
      </template>
    </a-page-header>
    <a-card size="small" :bordered="false" class="supplierInfoEdit-table-page-wrapper">
      <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" @change="getCityList" v-model="form.provinceSn" placeholder="请选择省">
                  <a-select-option v-for="item in addrProvinceList" :value="item.sn" :key="item.sn + '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" @change="getAreaList" v-model="form.citySn" placeholder="请选择市">
                  <a-select-option v-for="item in addrCityList" :value="item.sn" :key="item.sn + '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" @change="areaCharged" v-model="form.countySn" placeholder="请选择区/县">
                  <a-select-option v-for="item in addrDistrictList" :value="item.sn" :key="item.sn + '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="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="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="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">
          <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-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;">
          <a-button type="primary" @click="handleSubmit" size="large" id="supplierInfoEdit-btn-submit" style="padding: 0 60px;">保存</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-card>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { getArea } from '@/api/data'
import { supplierDetail, supplierSave } from '@/api/supplier'
export default {
  name: 'SupplierInfoEdit',
  components: { STable, VSelect },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 16 }
      },
      form: {
        supplierName: '', // 供应商名称
        provinceSn: undefined, //  省
        provinceName: '',
        citySn: undefined, // 市
        cityName: '',
        countySn: undefined, // 区
        districtName: '',
        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
    },
    //  获取供应商信息
    getGoodsDetail () {
      supplierDetail({ id: this.$route.params.id }).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.$route.params.id ? this.$route.params.id : null
          supplierSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.handleBack()
              }, 1000)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  返回
    handleBack () {
      this.$router.push({ path: '/supplierManagement/supplierInfo/list' })
    },
    // 获取城市列表
    getCityList (val) {
      const index = this.addrProvinceList.findIndex(item => item.sn == val)
      if (index >= 0) {
        this.form.provinceName = this.addrProvinceList[index].name
      }
      this.addrCityList = []
      this.addrDistrictList = []
      this.form.citySn = undefined
      this.form.countySn = undefined
      this.form.supplierAddress = ''
      this.getArea('city', val)
    },
    // 获取区县列表
    getAreaList (val) {
      const index = this.addrCityList.findIndex(item => item.sn == val)
      if (index >= 0) {
        this.form.cityName = this.addrCityList[index].name
      }
      this.addrDistrictList = []
      this.form.countySn = undefined
      this.form.supplierAddress = ''
      this.getArea('district', val)
    },
    // 区县变更
    areaCharged (val) {
      const index = this.addrDistrictList.findIndex(item => item.sn == val)
      if (index >= 0) {
        this.form.countyName = this.addrDistrictList[index].name
      }
      this.form.supplierAddress = ''
    },
    //  省/市/区
    getArea (type, sn) {
      let params
      if (type == 'province') {
        params = { level: '1' }
      } else {
        params = { psn: sn }
      }
      getArea(params).then(res => {
        if (res.status == 200) {
          if (type == 'province') {
            this.addrProvinceList = res.data || []
          } else if (type == 'city') {
            this.addrCityList = res.data || []
          } else if (type == 'district') {
            this.addrDistrictList = res.data || []
          }
        } else {
          if (type == 'province') {
            this.addrProvinceList = []
          } else if (type == 'city') {
            this.addrCityList = []
          } else if (type == 'district') {
            this.addrDistrictList = []
          }
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getArea('province')
      vm.$refs.ruleForm.resetFields()
      if (vm.$route.params.id) { //  编辑页
        vm.getGoodsDetail()
      }
    })
  }
}
</script>

<style lang="less">
  .supplierInfoEdit-wrap{
    .supplierInfoEdit-cont{
      margin-bottom: 15px;
    }
    .supplierInfoEdit-table-page-wrapper{
      padding-top: 20px;
    }
  }
</style>
