<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" :height="310" @ok="handleSubmit">
      <BasicForm @register="registerForm" name="BaseShipArchiveRelateCustomerForm" />
      
       <!-- 子表单区域 -->
      
      <JVxeTable v-if="isUpdate"
        keep-source
        resizable
        :loading="demoFieldDefValSubTable.loading"
        :columns="demoFieldDefValSubTable.columns"
        :dataSource="demoFieldDefValSubTable.dataSource"
        :maxHeight="340"
        :rowNumber="false"
        :rowSelection="false"
        :toolbar="false"
        
        />
      
  </BasicModal>
</template>

<script lang="ts" setup>
    import {ref, computed, unref,reactive} from 'vue';
    import {BasicModal, useModalInner} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {formSchema,getFIle,columns} from '../BaseShipArchiveRelateCustomer.data';
    import {saveOrUpdate,list} from '../BaseShipArchiveRelateCustomer.api';
    import { JVxeTable } from '/@/components/jeecg/JVxeTable'
    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);
    const isDetail = ref(false);
    const demoFieldDefValSubTable = reactive({
          loading: false,
          dataSource: [],
          columns:columns
    })
    
    //表单配置
    const [registerForm, { setProps,resetFields, setFieldsValue, validate, scrollToField }] = useForm({
        labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 24}
    });
    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        
        //重置表单
        await resetFields();
        setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
        isDetail.value = !!data?.showFooter;
        
          await list({headId:data.record.headId,status:1}).then(function (response) {
            // console.log(response.records); 
            response.records.forEach(item => {
              data.record.name = item.name;
              data.record.customerId = item.customerId;
              data.record.startDate = item.startDate;
            });
          }).catch(function (error) {
            console.log(error);
          });
         
          // console.log("==============================="+data.record.headId);
         
         console.log("==============================="+unref(isUpdate));
                  
          if (unref(isUpdate)) {
            
            getFIle("1")
            
            await list({headId:data.record.headId}).then(function (response) {
              // console.log("=========================");
              // console.log(response.records);
              // response.records.forEach(item => {
              //   console.log(item);
              // });
              
              // console.log(response.records); 
                demoFieldDefValSubTable.dataSource = response.records;
               
            }).catch(function (error) {
              console.log(error);
            });
            
          }else{
            getFIle();
          }
            
          //表单赋值
          await setFieldsValue({
              ...data.record,
          });
        
        // 隐藏底部时禁用整个表单
       setProps({ disabled: !data?.showFooter })
    });
    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '关联客户' : !unref(isDetail) ? '历史客户' : '关联客户'));
    //表单提交事件
    async function handleSubmit(v) {
        try {
            let values = await validate();
            setModalProps({confirmLoading: true});
            //提交表单
            await saveOrUpdate(values, isUpdate.value);
            //关闭弹窗
            closeModal();
            //刷新列表
            emit('success');
        } catch ({ errorFields }) {
           if (errorFields) {
             const firstField = errorFields[0];
             if (firstField) {
               scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
             }
           }
           return Promise.reject(errorFields);
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
  :deep(.ant-input-number) {
    width: 100%;
  }

  :deep(.ant-calendar-picker) {
    width: 100%;
  }
</style>
