<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="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,
        id: '',
      });
      const { createMessage } = useMessage();
      const optionsName = ref([]);
      const title = 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 });
                fileFlow.id = res && res.data ? res.data.id : '';
                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: 'ryNickName',
          component: 'Input',
          label: '姓名',
          colProps: {
            span: 20,
          },
          componentProps: {
            disabled: true,
          },
        },
        {
          field: 'shareTypes',
          component: 'CheckboxGroup',
          required: true,
          label: '共享内容',
          componentProps: {
            options: [
              { label: '查看实景三维', value: 'scene' },
              { label: '查看多元融合', value: 'fusion' },
            ],
          },
          colProps: {
            span: 24,
          },
        },
      ];
      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);
        fileFlow.id = data.id;
        title.value = data.id ? '编辑人员' : '新增人员';
        // let shareTypeObj = {
        //   0: [0],
        //   1: [1],
        //   2: [0, 1],
        // }
        setFieldsValue({
          ...data,
          // shareType: data.shareType && shareTypeObj[data.shareType]||[]
        })
      });
      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);
          // let shareType = params.shareType.length == 2 ? 2 : params.shareType[0];
          await userShareAdd({
            ...params,
            id: fileFlow.id,
            // shareType: shareType,
          });
          closeModal();
          resetFields();
          emit('ok');
          createMessage.success('操作成功');
        } catch (error) {
          console.log('not passing', error);
        }
      };

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