<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="fileFlow.title"

    @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, useForm } from '/@/components/Form/index';
  import { TableImg } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { modelingConfigSaveOrUpdate } from '/@/api/parameter';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { InvoiceSchemas } from './data';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm, TableImg },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const fileFlow = reactive({
        title: '新增设备类型',
      })
      const { createMessage } = useMessage();

      const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({
        labelWidth: 120,
        schemas:InvoiceSchemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      async function onDataReceive(data) {
        resetFields();
        fileFlow.title = data.id ? '修改设备类型' : '新增设备类型';
        setFieldsValue(data);
      }
      const handleSubmit = async () => {
        const values = await validate();
        await modelingConfigSaveOrUpdate(values);
        closeModal();
        emit('update', true);
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        registerForm,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
