<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="680"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate ? '编辑租户' : '添加租户'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="{ md: { span: 4 }, sm: { span: 24 } }"
      :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }"
      layout="horizontal"
    >
      <a-form-item label="租户名称" name="tenantName" v-if="!isUpdate">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="将作为系统的名称使用"
          v-model:value="form.tenantName"
        />
      </a-form-item>
      <a-form-item label="初始密码" name="password" v-if="!isUpdate">
        <a-input
          allow-clear
          :maxlength="20"
          placeholder="请输入超级管理员初始密码"
          v-model:value="form.password"
        />
      </a-form-item>
      <!--      <a-form-item label="所属客户" name="customerId">-->
      <!--        <CustomerSelect v-model:value="form.customerId" @select="onSelect" />-->
      <!--      </a-form-item>-->
      <!--      <a-form-item label="状态">-->
      <!--        <a-switch-->
      <!--          checked-children="运行中"-->
      <!--          un-checked-children="未上线"-->
      <!--          :checked="form.status === 0"-->
      <!--          @update:checked="updateStatus(form.status === 1 ? 0 : 1)"-->
      <!--        />-->
      <!--      </a-form-item>-->
      <div style="margin-bottom: 22px">
        <a-divider />
      </div>
      <a-form-item label="备注" name="comments">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入备注"
          v-model:value="form.comments"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { message } from 'ant-design-vue';
  import { addTenant, updateTenant } from '@/api/oa/tennat';
  import type { Tenant } from '@/api/oa/tennat/model';
  import { createMerchantCode } from '@/utils/common';
  import { FormInstance, Rule } from 'ant-design-vue/es/form';
  import useFormData from '@/utils/use-form-data';

  // 是否是修改
  const isUpdate = ref(false);
  const formRef = ref<FormInstance | null>(null);
  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: Tenant | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);

  // 用户信息
  const { form, resetFields, assignFields } = useFormData<Tenant>({
    tenantName: '',
    appId: `${createMerchantCode()}`,
    comments: '',
    tenantId: undefined,
    password: '',
    customerId: undefined
  });

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 表单验证规则
  const rules = reactive<Record<string, Rule[]>>({
    tenantName: [
      {
        required: true,
        message: '请填写租户名称',
        type: 'string',
        trigger: 'blur'
      }
    ],
    password: [
      {
        required: true,
        type: 'string',
        message: '请输入初始密码',
        trigger: 'blur'
      }
    ]
  });

  // const { resetFields, validate, validateInfos } = useForm(form, rules);

  // const onSelect = (item) => {
  //   if (!isUpdate.value) {
  //     form.customerId = item.customerId;
  //     form.tenantName = item.customerName;
  //     form.password = 'gxwebsoft.com';
  //   }
  // };

  // 更新状态
  // const status = ref(0);
  // const updateStatus = (index) => {
  //   status.value = index;
  // };

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const data = {
          ...form
        };
        const saveOrUpdate = isUpdate.value ? updateTenant : addTenant;
        saveOrUpdate(data)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            resetFields();
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          loading.value = false;
          assignFields({
            ...props.data
          });
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
<style lang="less"></style>
