<template>
  <vxe-modal
    resize
    v-model="showModal"
    :title="modalTitle"
    @hide="onHide"
    width="500px"
    show-maximize
    show-footer
    esc-closable
  >
    <el-form ref="formRef" :model="form" :rules="rules" inline label-width="110px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item class="w-full" label="公司名称" prop="orgName">
            <el-input v-model="form.orgName" placeholder="请输入公司名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="w-full" label="所属区域" prop="orgName">
            <el-cascader
              v-model="form.arealist"
              :props="{ label: 'name', value: 'name' }"
              class="w-full"
              :checkStrictly="true"
              :options="citydata"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="w-full" label="负责人手机" prop="offenderPhone">
            <el-input v-model="form.offenderPhone" placeholder="负责人手机" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="w-full" label="负责人" prop="offender">
            <el-input v-model="form.offender" placeholder="负责人" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="w-full" label="是否共案处理" prop="mergerCase">
            <el-radio-group v-model="form.mergerCase">
              <el-radio value="Y">是</el-radio>
              <el-radio value="N">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="w-full" label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio value="enable">启用</el-radio>
              <el-radio value="disable">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button type="default" @click="showModal = false">取消</el-button>
      <el-button :loading="loading" type="primary" @click="submitForm">提交</el-button>
    </template>
  </vxe-modal>
</template>

<script setup name="tenantFormModal">
  import { ref, computed } from 'vue';
  import { ElMessage } from 'element-plus';
  import { saveManageOrg } from '@/api/manageOrg';
  import citydata from '../../../../assets/citydata';
  const emits = defineEmits(['success']);
  const currentTenant = ref(null);
  const showModal = ref(false);
  const formRef = ref(null);

  const isEdit = computed(() => !!currentTenant.value);
  const modalTitle = computed(() => (isEdit.value ? '编辑公司' : '新增公司'));

  const form = ref({
    arealist: [],
    orgName: '',
    offenderPhone: '',
    offender: '',
    mergerCase: 'N',
    status: 'enable',
  });
  const validatePhone = (rule, value, callback) => {
    const reg = /^1[3-9]\d{9}$/;
    if (!value) {
      callback(new Error('请输入手机号'));
    } else if (!reg.test(value)) {
      callback(new Error('请输入正确的手机号'));
    } else {
      callback();
    }
  };
  const rules = ref({
    orgName: { required: true, message: '请输入公司名称', trigger: 'blur' },
    offenderPhone: [
    { required: true, message: '请输入负责人手机', trigger: 'blur' },
    { validator: validatePhone, trigger: 'blur' }
    ],
    offender: { required: true, message: '请输入负责人', trigger: 'blur' },
  });

  const loading = ref(false);
  const submitForm = async () => {
    try {
      await formRef.value.validate();
      loading.value = true;
      const params = JSON.parse(JSON.stringify(form.value));
      if (params.arealist && params.arealist.length > 0) {
        params['province'] = params.arealist[0];
        params['city'] = params.arealist.length > 1 ? params.arealist[1] : null;
        params['district'] = params.arealist.length > 2 ? params.arealist[2] : null;
        delete params['arealist'];
      }
      await saveManageOrg(params);
      emits('success');
      ElMessage({
        type: 'success',
        message: isEdit.value ? '修改成功！' : '添加成功!',
        plain: true,
      });
      showModal.value = false;
    } catch {}
    loading.value = false;
  };
  const onHide = () => {
    form.value = {
      arealist: [],
      orgName: '',
      offenderPhone: '',
      offender: '',
      mergerCase: 'N',
      status: 'enable',
    };
    formRef.value.clearValidate();
    currentTenant.value = null;
  };
  const openModal = (role) => {
    role && (form.value = role);
    form.value.arealist = role ? [role.province, role.city, role.district] : [];
    console.log('role', role, form.value.arealist);
    currentTenant.value = role;
    showModal.value = true;
  };

  defineExpose({
    openModal,
  });
</script>
