<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="行政编号" prop="code">
        <el-input v-model="form.code" disabled/>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="状态" prop="enableStatus">
        <el-select v-model="form.enableStatus" placeholder="状态" size="small">
          <el-option
              v-for="dict in enableStatusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { enableStatusList } from '@/constant/statusMap'
import { updateAreaCode } from '@/api/xq/areaCode'

export default {
  components: {
  },
  data () {
    return {
      enableStatusList,
      open: false,
      title: '',
      form:{
        code: undefined,
        name: undefined,
        enableStatus: undefined,
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        enableStatus: [
          { required: true, message: "数据不能为空", trigger: "change" }
        ],
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (row) {
      this.title = "修改";
      this.open = true;
      this.$nextTick(() => {
        this.form.code = row.code;
        this.form.name = row.name;
        this.form.enableStatus = row.enableStatus;
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          updateAreaCode(this.form).then(data => {
            this.$modal.msgSuccess("修改成功");
            this.$modal.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
  }
}
</script>
