<template>
  <el-drawer v-model="isDrawer" :size="width" :title="title" @close="handleReset">
    <el-form
      :model="formParams"
      :rules="rules"
      ref="formRef"
      label-placement="left"
      :label-width="80"
    >
      <el-form-item label="上级区域" prop="parentId">
        <el-tree-select
          :data="getRegionTreeList"
          clearable
          v-model="formParams.parentId"
          placeholder="请选择区域，省份无需选择"
        />
      </el-form-item>

      <el-form-item label="区域名称" prop="regionName">
        <el-input placeholder="请输入区域名称" v-model="formParams.regionName" />
      </el-form-item>

      <el-form-item label="区域编码" prop="regionCode">
        <el-input placeholder="请输入区域编码" v-model="formParams.regionCode" />
      </el-form-item>

      <el-form-item label="显示排序" prop="regionOrders">
        <el-input-number placeholder="显示排序" v-model="formParams.regionOrders" class="w-full" />
      </el-form-item>

      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="是否省份" prop="isProinve">
            <el-switch v-model="formParams.isProinve" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="是否城市" prop="isCity">
            <el-switch v-model="formParams.isCity" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="是否区县" prop="isDistrict">
            <el-switch v-model="formParams.isDistrict" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="是否启用" prop="isEnable">
            <el-switch v-model="formParams.isEnable" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-space>
        <el-button @click="handleReset">重置</el-button>
        <el-button type="primary" :loading="subLoading" @click="formSubmit">提交</el-button>
      </el-space>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import { ElMessage } from 'element-plus';
  import type { formParamsType } from './types';

  import { addRegion, editRegion, regionInfo } from '@/api/system/region';
  import { replaceParams } from '@/utils/helper/treeHelper';

  const rules = {
    regionName: {
      required: true,
      message: '区域名称不能为空',
      trigger: 'blur',
    },
    regionCode: {
      required: true,
      message: '区域编码不能为空',
      trigger: 'blur',
    },
  };

  const emit = defineEmits(['change']);

  const props = defineProps({
    title: {
      type: String,
      default: '添加区域',
    },
    width: {
      type: Number,
      default: 380,
    },
    regionList: {
      type: Array,
    },
  });

  const getRegionTreeList = computed(() => {
    return replaceParams(props.regionList || [], 'regionName', 'id');
  });

  const defaultValueRef = () => ({
    id: null,
    isCenterCity: false,
    isProinve: false,
    isCity: false,
    isDistrict: false,
    isEnable: true,
    parentId: null,
    regionCode: '',
    regionName: '',
    regionOrders: undefined,
  });

  const message = ElMessage;
  const formRef: any = ref(null);
  const isDrawer = ref(false);
  const subLoading = ref(false);

  const formParams = ref<formParamsType>(defaultValueRef());

  function openDrawer(id?) {
    if (id) {
      formParams.value.id = id;
      getInfo();
      return;
    }
    isDrawer.value = true;
  }

  function closeDrawer() {
    isDrawer.value = false;
  }

  function formSubmit() {
    formRef.value.validate((valid) => {
      if (!valid) {
        return message.error('请填写完整信息');
      }
      if (formParams.value.id) {
        editRegion(formParams.value).then((_) => {
          message.success('编辑成功');
          emit('change');
          handleReset();
          closeDrawer();
        });
      } else {
        addRegion(formParams.value).then((_) => {
          message.success('添加成功');
          emit('change');
          handleReset();
          closeDrawer();
        });
      }
    });
  }

  function handleReset() {
    formRef.value.resetFields();
    formParams.value = Object.assign(formParams.value, defaultValueRef());
  }

  function getInfo() {
    regionInfo({ id: formParams.value.id }).then((res) => {
      const info = {
        id: res.id,
        isCenterCity: res.isCenterCity,
        isProinve: res.isProinve,
        isCity: res.isCity,
        isDistrict: res.isDistrict,
        isEnable: res.isEnable,
        parentId: res.parentId,
        regionCode: res.regionCode,
        regionName: res.regionName,
        regionOrders: res.regionOrders,
      };
      formParams.value = info;
      isDrawer.value = true;
    });
  }

  defineExpose({
    openDrawer,
    closeDrawer,
  });
</script>
