<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="t('routes.corporation.addSubId')"
    @visible-change="handleVisibleChange"
    @ok="handleSubmit"
    @cancel="handleClose"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm" :model="model" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { checkUserAddAble } from '/@/api/corporation/modal';
  import { useI18n } from '/@/hooks/web/useI18n';

  const { t } = useI18n();
  const schemas: FormSchema[] = [
    {
      field: 'companyName',
      component: 'Input',
      label: t('common.fullName'),
      labelWidth: 100,
      colProps: {
        span: 20,
      },
      componentProps: {
        maxLength: 25,
      },
    },
    {
      field: 'managerPhone',
      component: 'Input',
      labelWidth: 100,
      label: t('sys.login.email'),
      rules: [
        {
          required: true,
          // @ts-ignore
          validator: async (rule, value) => {
            // var reg_tel =
            //   /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
            var reg = /\S+@\S+\.\S+/;
            if (!value) {
              /* eslint-disable-next-line */

              return Promise.reject(t('sys.login.fillMail'));
            }
            if (!reg.test(value)) {
              /* eslint-disable-next-line */
              return Promise.reject(t('sys.login.fillMailCorrect'));
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ],
      colProps: {
        span: 20,
      },
    },
  ];
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['register', 'submit', 'tablelist'],
    setup(props, { emit }) {
      const modelRef = ref({});
      const { createMessage } = useMessage();
      const [
        registerForm,
        {
          // getFieldsValue,
          // setFieldsValue,
          // setProps
          resetFields,
          validate,
        },
      ] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        console.log('Data Received', data);
      }
      const handleSubmit = async () => {
        // console.log('getFieldsValue()', getFieldsValue());
        // var reg_tel =
        //   /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
        // let { phone, memoName } = getFieldsValue();
        // if (phone == '') {
        //   createMessage.error('请填写您的手机号码！');
        //   return false;
        // } else if (!reg_tel.test(phone)) {
        //   createMessage.error('请正确填写您的手机号码！');
        //   return false;
        // }

        const values = await validate();
        let addTips = false;
        emit('tablelist', (list) => {
          addTips = list.some((ele) => ele.userName == values.managerPhone);
        });

        if (addTips) {
          return createMessage.error(t('routes.corporation.addError'));
        }
        //TODO hack parameter
        let res = await checkUserAddAble({ phoneNum: values.managerPhone });
        console.log('checkUserAddAble', res);
        if (res == false) {
          // addListFunc({ phone: values.phone, companyName: values.companyName });
          emit('submit', {
            managerPhone: values.managerPhone,
            companyName: values.companyName,
          });
          resetFields();
          closeModal();
        } else {
          createMessage.error(t('routes.corporation.addError'));
        }
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      function handleClose() {
        resetFields();
      }
      return {
        t,
        register,
        schemas,
        registerForm,
        model: modelRef,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        closeModal,
        handleClose,

        // nextTick,
      };
    },
  });
</script>
