<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="新增人员"
    @cancel="resetFields"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm">
        <template #text="{ model, field }">
          {{ model[field] }}
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, onMounted, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { getinnerByRyId, userShareAdd } from '/@/api/operate';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['ok'],
    setup(_, { emit }) {
      const modelRef = ref(false);
      const fileFlow = reactive({
        file: null,
      });
      const { createMessage } = useMessage();
      const optionsName = ref([]);
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          show: false,
          label: 'id',
          defaultValue: '111',
          required: false,
        },
        {
          field: 'ryNo',
          component: 'Input',
          label: '人员编号',
          required: true,
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                let myData = await getFieldsValue();
                if (!value) {
                  return Promise.reject('请输入人员编号');
                }
                let res = await getinnerByRyId({ ryNo: value });
                console.log('value', value, res);
                if (res && !res.data) {
                  return Promise.reject('人员编号不存在');
                }
                Promise.resolve();
                let ryNickName = res && res.data && res.data.ryNickName;
                if(myData.ryNickName != ryNickName || !myData.ryNickName){
                  setFieldsValue({
                    ryNickName: res && res.data ? res.data.ryNickName : '',
                    id: res && res.data ? res.data.id : '',
                  });
                }
              },
              trigger: 'blur',
            },
          ],
          colProps: {
            span: 20,
          },
        },
        // {
        //   field: 'ryNo',
        //   component: 'AutoComplete',
        //   label: '人员编号',
        //   required: true,
        //   componentProps: {
        //     filterOption: onFilterOption,
        //     onSearch: onSearch,
        //     onChange: (data) => {
        //       setTimeout(() => {
        //         const item = optionsName.value && optionsName.value.find((ele) => ele.ryNo == data);
        //         setFieldsValue({
        //           ryNickName: item && item.ryNickName ? item.ryNickName : '',
        //           id: item && item.id ? item.id : '',
        //         });
        //       }, 100);
        //     },
        //   },
        //   colProps: {
        //     span: 20,
        //   },
        // },
        {
          field: 'ryNickName',
          component: 'Input',
          label: '姓名',
          // rules: [
          //   {
          //     required: true,
          //     // @ts-ignore
          //     validator: async (rule, value) => {
          //       if (!value) {
          //         return Promise.reject('请输入正确的人员编号');
          //       }
          //       return Promise.resolve();
          //     },
          //     trigger: 'change',
          //   },
          // ],
          colProps: {
            span: 20,
          },
          componentProps: {
            disabled: true,
          },
        },
      ];
      const [
        registerForm,
        { validate, resetFields, setFieldsValue, getFieldsValue, updateSchema },
      ] = useForm({
        labelWidth: 110,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner(async (data) => {
        console.log('option', data);
      });
      function onFilterOption(inputText: string, option) {
        console.log('option', inputText, option.value);
        if (option.value) {
          return option.value.indexOf(inputText) >= 0;
        }
        // return option.value.indexOf(inputText.toUpperCase()) >= 0;
      }
      async function onSearch(searchText) {
        const res = await getinnerByRyId({ ryNo: searchText });
        console.log('res', res.data);
        if (!res.data) return;
        optionsName.value = res.data || {};
        updateSchema({
          field: 'ryNo',
          componentProps: {
            options: [optionsName.value],
          },
        });
      }
      const handleSubmit = async () => {
        const params = await validate();
        try {
          console.log('params', params);
          await userShareAdd(params);
          closeModal();
          resetFields();
          emit('ok');
          createMessage.success('操作成功');
        } catch (error) {
          console.log('not passing', error);
        }
      };

      return {
        register,
        schemas,
        registerForm,
        modelRef,
        fileFlow,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
