<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="备注"
    @cancel="resetFields"
    :confirmLoading="loading"
    @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, 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 { updateRemark } from '/@/api/spares';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { uploadApi } from '/@/api/product/index';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const fileFlow = reactive({
        file:null,
        type:2,//2-普通发票，3-专用发票
      })
      const loading = ref(false)
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
          {
            field: 'id',
            component: 'Input',
            show:false,
            label: '发票编号',
          },
          {
            field: 'repairId',
            component: 'Input',
            label: '维修单号',
            slot: 'text',
            colProps: {
              span: 24,
            },
          },{
            field: 'deviceType',
            component: 'Input',
            label: '设备信息',
            slot: 'text',
            colProps: {
              span: 24,
            },
          },{
            field: 'remark',
            component: 'InputTextArea',
            required: true,
            label: '备注',
            componentProps: {
              maxLength: 500,
              rows:4,
              placeholder: '请填写备注',
            },
            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);
      });
      function onDataReceive(data) {
        resetFields();
        fileFlow.type = data.type
        setFieldsValue({
          ...data,
          deviceType:t(`routes.scene.tableType.${data.cameraType || 0}`) +' '+ data.cameraSnCode
        });
      }
      const handleSubmit = async () => {
        loading.value = true
        try {
          const params = await validate();
          await updateRemark(params);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('update');
          loading.value = false
        } catch (error) {
          loading.value = false
          console.log('not passing', error);
        }
      };
      return {
        register,
        registerForm,
        fileFlow,
        handleSubmit,
        addListFunc,
        resetFields,
        loading,
        t,
      };
    },
  });
</script>
