<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="regionEdit">
      <el-form-item label="区域ID" style="display: none">
        <el-input v-model="form.regionId" type="input" />
      </el-form-item>
      <el-form-item label="省份选择" prop="provinceCode">
        <el-select ref="optionRef" v-model="form.provinceCode" 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="备注">
        <el-input v-model="form.remark" type="textarea" />
      </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>

<script>
  import distrApi from '@/api/distr'
  import regionApi from '@/api/base/region/region'

  export default {
    name: 'Edit',
    data() {
      return {
        form: {
          regionEdit: [],
        },
        regionEdit: {
          provinceCode: [{ required: true, trigger: 'blur', message: '请选择省份' }],
        },
        title: '',
        dialogFormVisible: false,
        //省份
        district: [],
        //区域
        options: [],
        provinceCode: '',
        // 区域
        regionId: '',
        vaule: '',
      }
    },
    created() {
      //区域
      this.getRegion()
      //省份
      this.getProvinceInfo()
      this.setRegion()
    },
    methods: {
      setRegion(id) {
        console.log('setRegionnid', id)
        this.form.regionId = id
      },
      async getRegion() {
        const { data: data } = await regionApi.getRegion({})
        this.options = data.list
      },
      selectClass(id) {
        let obj = {}
        obj = this.options.find((item) => {
          return item.id === id //筛选出匹配数据
        })
        this.regionId = obj.id
      },
      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 //筛选出匹配数据
        })
        this.provinceCode = obj.id
      },

      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        // this.$refs['form'].resetFields()
        // this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              console.log(this.form)
              const { msg } = await regionApi.doEdit(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              console.log('from', this.form)
              const { msg } = await regionApi.doAdd(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
