<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="t('routes.corporation.expirationTime')"
    :minHeight="350"
    @visible-change="handleVisibleChange"
    @ok="handleSubmit"
    @cancel="resetFields"
  >
    <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 dayjs from 'dayjs';
  // import { checkUserAddAble } from '/@/api/corporation/modal';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { updateCompnayApi } from '/@/api/corporation/list';
  const { t } = useI18n();
  const schemas: FormSchema[] = [
    {
      field: 'id',
      component: 'Input',
      label: 'id',
      show: false,
    },
    {
      field: 'expirationTime',
      component: 'DatePicker',
      label: t('routes.corporation.expirationTime'),
      required: true,
      colProps: {
        span: 22,
      },
      componentProps: {
        valueFormat: 'YYYY-MM-DD',
        disabledDate: (current) => {
          return current && current < dayjs().endOf('day');
        },
      },
    },
  ];
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['register', 'submit'],
    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) {
        if (dayjs(data.expirationTime) > dayjs()) {
          setFieldsValue({ ...data });
        } else {
          setFieldsValue({ id: data.id });
        }
      }
      const handleSubmit = async () => {
        const values = await validate();
        console.log('insertData', values);
        //TODO hack parameter
        const res = await updateCompnayApi({
          id: values.id,
          expirationTime: values.expirationTime,
        });
        // let res = await checkUserAddAble({ phoneNum: values.managerPhone });
        console.log('res', res);
        emit('submit');
        closeModal();
        resetFields();
        createMessage.success(t('common.optSuccess'));
        // createMessage.success(t('routes.corporation.optSuccess'));
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

      return {
        t,
        register,
        schemas,
        registerForm,
        model: modelRef,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        closeModal,
        resetFields,
        // nextTick,
      };
    },
  });
</script>
