<template>
  <BasicModal
    v-bind="$attrs"
    :title="title"
    @register="registerInnerModal"
    @ok="handleSubmit"
    @cancel="resetForm"
    :width="800"
  >
    <template #footer>
      <div v-if="isUpdate !== 3">
        <a-button type="default" @click="handleCancle">取消</a-button>
        <a-button type="primary" @click="handleSubmit">确认</a-button>
      </div>
    </template>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script setup lang="ts">
import { BasicModal, useModalInner } from '@/components/Modal';
import { BasicForm, useForm } from '@/components/Form';
import { computed, ref, unref } from 'vue';
import { workerInfoInfo, workerInfoAdd, workerInfoUpdate } from '@/api/workPlace/workerInfoManage';
import { modalSchemas } from './workerInfo.data';
import { convertNumberToString } from '@/utils/tool';
import { maintenanceInfoList } from '@/api/basicInfo/maintenance';

defineOptions({ name: 'WorkerInfoModal' });

const emit = defineEmits(['register', 'reload']);

const isUpdate = ref<number>(1);
const title = computed<string>(() => {
  return isUpdate.value === 1
    ? '编辑工作人员管理'
    : isUpdate.value === 2
    ? '新增工作人员管理'
    : '查看工作人员管理';
});

const [registerInnerModal, { modalLoading, closeModal }] = useModalInner(
  async (data: { record?: Recordable; update: number }) => {
    modalLoading(true);
    const { record, update } = data;
    isUpdate.value = update;
    if (isUpdate.value === 3) {
      setProps({ disabled: true });
    } else {
      setProps({ disabled: false });
    }
    await initHouseTree();
    if ((update === 1 || update === 3) && record) {
      const ret = await workerInfoInfo(record.id);
      ret.unitType = convertNumberToString(ret.unitType);
      ret.employeeType = convertNumberToString(ret.employeeType);
      ret.relevancyId = ret.relevancyId * 1;
      ret.credential = ret.credential?[ret.credential]:[];
      ret.photo =ret.photo ? [ret.photo]:[];
      await setFieldsValue(ret);
    }
    modalLoading(false);
  },
);
async function initHouseTree() {
  await updateSchema([
    {
      field: 'relevancyId',
      component: 'ApiSelect',
      componentProps: ({ formModel }) => ({
        api: maintenanceInfoList,
        labelInValue: true,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择关联单位',
        afterFetch: (res) => {
          return res.rows;
        },
        onSelect: (e: any) => {
          console.log(e, 'e');
          setFieldsValue({ relevancyId: e.value, relevancyUnit: e.label });
        },
      }),
    },
  ]);
}
const [registerForm, { setFieldsValue, resetForm, validate, setProps, updateSchema }] = useForm({
  labelWidth: 160,
  showActionButtonGroup: false,
  baseColProps: { span: 12 },
  schemas: modalSchemas,
});
function handleCancle() {
  closeModal();
  resetForm();
}
async function handleSubmit() {
  try {
    modalLoading(true);
    const data = await validate();
    data.credential = data.credential[0];
    data.photo = data.photo[0];
    if (isUpdate.value === 1) {
      await workerInfoUpdate(data);
    } else {
      await workerInfoAdd(data);
    }
    emit('reload');
    closeModal();
    await resetForm();
  } catch (e) {
    console.warn(e);
  } finally {
    modalLoading(false);
  }
}
</script>

<style scoped></style>
