<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="t('routes.equity.renew')"
    height="500"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm">
        <template #text="{ model, field }">
          {{ model[field]  }}
        </template>
        <template #img="{ model }">
          <TableImg v-if="model.invoiceUrl" :size="200" :adaptations="true" :simpleShow="true" :imgList="[model.invoiceUrl]" />
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, nextTick, onMounted, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { TableImg } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { InvoiceRegister, InvoiceDetail } from '/@/api/order';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { renewApi } from '/@/api/equity/index';
  import dayjs from 'dayjs';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm, TableImg },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const fileFlow = reactive({
        file:null,
        type:2,//2-普通发票，3-专用发票
      })
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
          {
            field: 'userName',
            component: 'Input',
            label: t('routes.equity.userName'),
            slot: 'text',
            colProps: {
              span: 24,
            },
          },{
            field: 'id',
            component: 'Input',
            label: t('routes.equity.id'),
            slot: 'text',
            colProps: {
              span: 24,
            },
          },{
            field: 'incrementEndTime',
            component: 'DatePicker',
            label: t('routes.equity.expiryTime'),
            required: true,
            defaultValue:dayjs().add(1,'year').format('YYYY-MM-DD'),
            componentProps: {
              disabled:true,
              valueFormat: 'YYYY-MM-DD',
              format: 'YYYY-MM-DD',
              disabledDate: (current) => {
                return current && current < dayjs().endOf('day');
              },
            },
            colProps: {
              span: 22,
            },
          }
      ];

      const [registerForm, { validate, resetFields, setFieldsValue, updateSchema }] = useForm({
        labelWidth: 120,
        schemas:schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      async function onDataReceive(data) {
        resetFields();
        // let detail = await InvoiceDetail({id:data.id})
        console.log('InvoiceDetail',data)
        fileFlow.type = data.type
        setFieldsValue({
          ...data,
          incrementEndTime:dayjs(data.isExpired == 1?new Date():data.incrementEndTime).add(1,data.validTimeType == 0?'year':'month').format('YYYY-MM-DD')
        });
      }
      const handleSubmit = async () => {
          const params = await validate();
          await renewApi(params);
          createMessage.success(t('common.optSuccess'));
          closeModal();
          emit('update',true);
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        registerForm,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
