<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="重命名"
    :okText="okText"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    :confirmLoading="loading"
    min-height="50"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm">
        <template #text="{ model, field }"> {{ model[field] }}{{ fileFlow.type }} </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, nextTick, onMounted, reactive, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { InvoiceRegister } from '/@/api/order';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { uploadApi } from '/@/api/product/index';
  import { ResultEnum } from '/@/enums/httpEnum';
  import { caseOverviewRename, caseTabulationRename } from '/@/api/operate/case';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      tableType: { type: Number, default: 0 },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const fileFlow = reactive({
        file: null,
        type: 2, //2-普通发票，3-专用发票
      });
      const okText = ref('确定');
      const loading = ref(false);
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'overviewId',
          component: 'Input',
          show: false,
          label: 'overviewId',
        },
        {
          field: 'tabulationId',
          component: 'Input',
          show: false,
          label: 'tabulationId',
        },
        {
          field: 'newTitle',
          component: 'Input',
          label: '名称',
          componentProps: {
            maxlength: 100,
            showCount: true,
          },
          required: true,
          colProps: {
            span: 22,
          },
        },
      ];

      const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({
        labelWidth: 120,
        schemas: schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      function onDataReceive(data) {
        resetFields();
        setFieldsValue(data);
      }
      const handleSubmit = async () => {
        loading.value = true;
        try {
          let url = props.tableType === 1 ? caseOverviewRename : caseTabulationRename;
          const params = await validate();
          const apiData = params;
          console.log('res', apiData, params);
          await url(apiData);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('update');
          loading.value = false;
        } catch (error) {
          loading.value = false;
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        // v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        registerForm,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        okText,
        loading,
        t,
      };
    },
  });
</script>
