<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="备件入库"
    @cancel="resetFields"
    :min-height="230"
    :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 { partAllList, partInStock } 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: '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',
                },
              ],
              onChange:(value)=>{
                console.log('immediate',value)
                setFieldsValue({
                  partId:'',
                })
                updateSchema({
                    field: 'partId',
                    label: '备件类型',
                    component: 'ApiSelect',
                    required:true,
                    componentProps:{
                      filterOption: onFilterOption,
                      showSearch:true,
                      api:partAllList,
                      numberToString: true,
                      labelField: 'partName',
                      valueField: 'partId',
                      immediate: true,
                      listHeight:120,
                      disabled:false,
                      params: {
                        cameraType: value,
                      },
                    },
                  })
            }
            },
            colProps: {
              span: 18,
            },
          },{
            field: 'partId',
            label: '备件类型',
            component: 'ApiSelect',
            required:true,
            componentProps: {
              filterOption: onFilterOption,
              showSearch:true,
              api:partAllList,
              numberToString: true,
              labelField: 'partName',
              valueField: 'partId',
              immediate: false,
              listHeight:120,
              disabled:true,
            },
            colProps: {
              span: 18,
            },
          },{
            field: 'partStock',
            component: 'InputNumber',
            label: '入库数量',
            required:true,
            componentProps:{
              maxLength: 50,
              min:0,
            },
            colProps: {
              span: 18,
            },
          },
      ];

      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
      }
      const handleSubmit = async () => {
        loading.value = true
        try {
          const params = await validate();
          await partInStock(params);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('update');
          loading.value = false
        } catch (error) {
          loading.value = false
          console.log('not passing', error);
        }
      };
      function onFilterOption(inputText: string, option: Option) {
        if(option.label){
          return option.label.toUpperCase().indexOf(inputText.toUpperCase()) >= 0;
        }
        return option.value.toUpperCase().indexOf(inputText.toUpperCase()) >= 0;
      }
      return {
        register,
        registerForm,
        fileFlow,
        handleSubmit,
        addListFunc,
        resetFields,
        loading,
        t,
      };
    },
  });
</script>
