<template>
  <BasicModal
    v-bind="$attrs"
    @cancel="resetFields"
    @register="register"
    :title="title"
    :min-height="200"
    height="500"
    @ok="handleOk"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, reactive, computed } from 'vue';
  import { checkUserApi, saveApi, updateApi, getRoleListByParam } from '/@/api/staff/list'; //roleLIstApi
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useUserStore } from '/@/store/modules/user';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['ok'],
    setup(_, context) {
      const modelRef = reactive({
        id: '',
        createUserId: '',
        createUserName: '',
        dingAccount: '',
        nickName: '',
        roleId: '',
        roleName: '',
        userName: '',
        status: '',
        createTime: '',
        pendingStatus: '',
      });
      const userStore = useUserStore();
      const userinfo = computed(() => userStore.getUserInfo);
      const preventAutoFill = ref(true);
      const { companyId } = userinfo.value;
      console.log('companyId', companyId);
      const permListOptions = computed(() => {
        return [
          {
            label: '带看',
            value: '1',
          },
          {
            label: '拍摄',
            value: '2',
          },
        ];
      });
      const schemas: FormSchema[] = [
        {
          field: 'userName',
          component: 'Input',
          label: '账号',
          colProps: {
            span: 20,
          },
          componentProps: {
            disabled: true,
          },
        },
        {
          field: 'roleId',
          component: 'ApiSelect',
          label: '角色',
          required: true,
          itemProps: {
            validateTrigger: 'blur',
          },
          colProps: {
            span: 20,
          },
          componentProps: {
            listHeight: 120,
            api: getRoleListByParam,
            labelField: 'roleName',
            valueField: 'id',
            params: {
              type: 1,
            },
          },
        },
        {
          field: 'dingAccount',
          component: 'Input',
          label: '钉钉账号',
          colProps: {
            span: 20,
          },
        },
        {
          field: 'id',
          component: 'Input',
          label: 'id',
          show: false,
        },
      ];
      const title = ref('编辑账号');
      const { createMessage } = useMessage();
      const [registerForm, { setFieldsValue, validate, updateSchema, resetFields }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      function onDataReceive(data) {
        // 方式1;
        console.log('useModalInner', data);
        // preventAutoFill.value = false
        modelRef.id = data.id;
        modelRef.createTime = data.createTime;
        modelRef.createUserId = data.createUserId;
        modelRef.createUserName = data.createUserName;
        modelRef.dingAccount = data.dingAccount;
        modelRef.nickName = data.nickName;
        modelRef.pendingStatus = data.pendingStatus;
        modelRef.roleName = data.roleName;
        modelRef.status = data.status;
        modelRef.userName = data.userName;
        setTimeout(() => {
          setFieldsValue({
            ...data,
          });
        }, 200);
      }
      function companyIdChange(companyId) {
        // resetFields(['permList'])
        updateSchema([
          {
            field: 'permList',
            componentProps: {
              params: {
                companyId: companyId,
              },
            },
          },
        ]);
        setFieldsValue({
          permList: [],
        });
      }
      async function handleOk() {
        let data = await validate();
        console.log('modelRef', modelRef);
        const requestApi = data.id ? updateApi : saveApi;
        let res = await requestApi({
          ...modelRef,
          ...data,
          // userName: data.phone,
          phone: data.phone,
          // nickName: data.nickName,
        });
        context && context.emit('ok', res);
        createMessage.success(t('common.optSuccess'));
        closeModal();
        resetFields();
      }

      return {
        register,
        title,
        preventAutoFill,
        schemas,
        registerForm,
        modelRef,
        handleOk,
        resetFields,
        permListOptions,
      };
    },
  });
</script>
