<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="fileFlow.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 { InvoiceRegister } from '/@/api/order';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { partAddOrUpdate } from '/@/api/spares';
  import { useTabs } from '/@/hooks/web/useTabs';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const fileFlow = reactive({
        file:null,
        title:'新增备件类型',
        type:2,//2-普通发票，3-专用发票
      })
      const { refreshPage } = useTabs();
      const loading = ref(false)
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
            field: 'cameraType',
            label: '设备类型',
            component: 'Select',
            required:true,
            componentProps: {
              options: [
                {
                  label: t('routes.scene.tableType.0'),
                  value: 0,
                },{
                  label: t('routes.scene.tableType.1'),
                  value: 1,
                },{
                  label: t('routes.scene.tableType.2'),
                  value: 2,
                },
              ],
            },
            colProps: {
              span: 18,
            },
          },{
            field: 'partNum',
            component: 'Input',
            label: '备件编号',
            required:true,
            componentProps:{
              maxLength: 50,
            },
            colProps: {
              span: 18,
            },
          },{
            field: 'partName',
            component: 'Input',
            label: '备件名称',
            required:true,
            componentProps:{
              maxLength: 50,
            },
            colProps: {
              span: 18,
            },
          },{
            field: 'partUnit',
            component: 'Input',
            label: '单位',
            required:true,
            componentProps:{
              maxLength: 50,
            },
            colProps: {
              span: 18,
            },
          },{
            field: 'partPrice',
            component: 'InputNumber',
            label: '单价（元）',
            required:true,
            componentProps:{
              maxLength: 50,
              precision:2,
              min:0.01,
            },
            colProps: {
              span: 18,
            },
          },{
            field: 'discount',
            label: '折扣',
            component: 'Select',
            required: true,
            defaultValue: 0,
            componentProps: {
              options: [
                {
                  label: '有折扣',
                  value: 1,
                },{
                  label: '无折扣',
                  value: 0,
                }
              ],
              onChange:(value)=>{
                updateSchema({
                  field: 'partPriceDiscount',
                  ifShow: value == 1,
                });
              }
            },
            colProps: {
              span: 18,
            },
          },{
            field: 'partPriceDiscount',
            component: 'InputNumber',
            label: '折扣价（元）',
            required: true,
            ifShow: false,
            componentProps:{
              maxLength: 50,
              precision:2,
              min:0.01,
            },
            colProps: {
              span: 18,
            },
          },
          {
            field: 'partId',
            component: 'Input',
            label: 'id',
            show: false,
          },
      ];

      const [registerForm, { validate, resetFields, setFieldsValue, updateSchema }] = useForm({
        labelWidth: 120,
        schemas:schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        resetFields();
        data && onDataReceive(data);
      });
      function onDataReceive(data) {
        console.log('data',data)
        fileFlow.title ='新增备件类型'
        if(data.partId){
          setFieldsValue({
            ...data,
            discount:data.partPriceDiscount?1:0
          })
          fileFlow.title ='编辑备件类型'
        }
        updateSchema({
            field: 'partPriceDiscount',
            ifShow: Boolean(data.partPriceDiscount),
        });
        fileFlow.type = data.type
      }
      const handleSubmit = async () => {
        loading.value = true
        try {
          const params = await validate();
          await partAddOrUpdate(params);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          refreshPage();
          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>
