<!-- <template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="dist">
      <el-form-item label="名称" prop="distName">
        <el-input v-model.trim="form.distName" />
      </el-form-item>
      <el-form-item label="助记名" prop="abbrName">
        <el-input v-model.trim="form.abbrName" />
      </el-form-item>
      <el-form-item label="说明" prop="distDesc">
        <el-input v-model.trim="form.distDesc" />
      </el-form-item>
      <el-form-item label="负责人" prop="distBoss">
        <el-input v-model.trim="form.distBoss" />
      </el-form-item>
      <el-form-item label="电话" prop="distBossPhone">
        <el-input v-model.trim="form.distBossPhone" />
      </el-form-item>
      <el-form-item label="选择省份" prop="provinceDesc">
        <el-select
          ref="optionRef"
          v-model="form.provinceDesc"
          placeholder="请选择"
          @change="selectDistrict">
          <el-option
            v-for="item in district"
            :key="item.id"
            :label="item.distName"
            :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="省份" prop="provinceId" style="display: none">
        <el-input v-model.trim="form.provinceId" />
      </el-form-item>
      <el-form-item label="销售人员" prop="belongSale">
        <el-input v-model.trim="form.belongSale" />
      </el-form-item>
      <el-form-item label="业务范围" prop="businessScope">
        <el-input v-model.trim="form.businessScope" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template> -->
<template>
  <el-dialog append-to-body :title="title" :visible.sync="dialogFormVisible" @close="handleClose">
    <el-form ref="form" :model="form" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="名称" prop="distName">
            <el-input v-model="form.prodName" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="助记名" prop="abbrName">
            <el-input v-model="form.abbrName" placeholder="请输入助记名" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="说明" prop="distDesc">
            <el-input v-model.trim="form.distDesc" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="distBoss">
            <el-input v-model.trim="form.distBoss" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话" prop="distBossPhone">
            <el-input v-model.trim="form.distBossPhone" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="选择省份" prop="provinceDesc">
            <el-select ref="optionRef" v-model="form.provinceDesc" placeholder="请选择" @change="selectDistrict">
              <el-option v-for="item in district" :key="item.id" :label="item.distName" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="省份" prop="provinceId" style="display: none">
            <el-input v-model.trim="form.provinceId" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售人员" prop="businessScope">
            <el-input v-model.trim="form.businessScope" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="业务范围" prop="distName">
            <el-input v-model="form.prodName" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          maxlength="500"
          placeholder="请输入备注"
          resize="none"
          :rows="5"
          show-word-limit
          type="textarea" />
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  import distrApi from '@/api/distr'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

  export default {
    name: 'UserEdit',
    data() {
      return {
        form: {
          dist: [],
        },
        dist: {
          distName: [{ required: true, trigger: 'blur', message: '请输入名称' }],
          distBoss: [{ required: true, trigger: 'blur', message: '请输入负责人' }],
          distBossPhone: [
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: '请输入正确的手机号码',
              trigger: 'blur',
              required: true,
            },
          ],
          businessScope: [{ required: true, trigger: 'blur', message: '请填写业务范围' }],
          provinceDesc: [{ required: true, trigger: 'blur', message: '请选择省份' }],
          belongSale: [{ required: true, trigger: 'blur', message: '请填写归属销售' }],
        },
        //省份
        district: [],
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      //省份
      this.getProvinceInfo()
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '新建'
        } else {
          this.title = '编辑'

          this.form = Object.assign({}, row)
          console.log('回显', this.form)
          //  this.pid = this.form.provinceId
          // this.form.provinceId = this.form.provinceDesc
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      async getProvinceInfo() {
        const { data: data } = await distrApi.getProvinceInfo({})
        console.log('区域', data.list)
        this.district = data.list
      },
      selectDistrict(id) {
        let obj = {}
        obj = this.district.find((item) => {
          return item.id === id //筛选出匹配数据
        })
        console.log('省份名称', obj.distName)
        this.provinceDesc = obj.distName
        this.form.provinceId = obj.id
        this.form.provinceDesc = obj.distName
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              console.log(this.form)
              this.form.distBossPhone = this.form.distBossPhone.toString()

              this.form.provinceId = parseInt(this.form.provinceId)

              console.log('表单修改提交内容:', this.form)
              const { msg } = await distrApi.doEdit(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              console.log('表单提交内容:', this.form)
              const { msg } = await distrApi.doAdd(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
