<template>
  <el-dialog v-model="props.modelValue" title="添加省市区数据表信息" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row :gutter="20">

        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="上级菜单" prop="RegionPid">
            <el-cascader class="w100" :options="dataList"
              :props="{ checkStrictly: true, value: 'regionId', label: 'regionName', emitPath: false }"
              placeholder="请选择上级菜单" clearable v-model="formData.regionPid">
              <template #default="{ node, data }">
                <span>{{ data.regionName }}</span>
                <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
              </template>
            </el-cascader>
          </el-form-item>
        </el-col>

        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="区划名称" prop="regionName">
            <el-input v-model="formData.regionName" placeholder="请输入区划名称" />
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="区划编码" prop="regionCode">
            <el-input v-model="formData.regionCode" placeholder="请输入区划编码" />
          </el-form-item>
        </el-col>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="层级(1省级 2市级 3区/县级)" prop="regionLevel">
            <el-input v-model="formData.regionLevel" placeholder="请输入层级(1省级 2市级 3区/县级)" />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>

    <template #footer>
      <div key="dialog-footer">
        <el-button type="primary" @click="handleAddClick(formRef)">添加</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </div>
    </template>
  </el-dialog>
</template>


<script setup>
import { reactive, ref, watch } from "vue";
import { ElMessage } from 'element-plus'
import modal from '@/plugins/modal.js'
import { regionTreeList, addOrUpdateRegion } from '@/api/business/Custom/Regions/region.js';


// 打开弹窗时回调
const openDialog = async () => {


  await getTreeList()
}

// -业务参数
const dataList = ref([])


// -业务方法



// -基础参数
const labelWidth = 100;
const formRef = ref();
const { proxy } = getCurrentInstance()
const emits = defineEmits(["update:modelValue"]);
const formData = reactive({
});
const props = defineProps({
  modelValue: Boolean,
  done: Function,
});
const imgData = ref({
  fileDir: "Region"
})

// 验证
const rules = reactive({
  regionName: [{ required: true, message: "区划名称不能为空", trigger: "blur" }],
  regionCode: [{ required: true, message: "区划编码不能为空", trigger: "blur" }],
  regionLevel: [{ required: true, message: "层级(1省级 2市级 3区/县级)不能为空", trigger: "blur", type: "number" }],
});

// -基础方法
async function getTreeList() {
  regionTreeList().then((res) => {
    if (res.code == 200) {
      dataList.value = res.data
    }
  })
}
watch(props, async (v) => {

});

// 提交
const handleAddClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }


    const { code } = await addOrUpdateRegion(formData);
    if (code == 200) {
      modal.msgSuccess('添加成功')
      closeDialog();
    }
  });
};
const closeDialog = () => {
  handleResetClick(formRef.value);
  props.done();
  emits("update:modelValue", false);
};
const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>