<template>
  <a-spin :spinning="spinning" tip="加载中...">
    
    <div class="page-bg-wrap formViewStyle pdcss">
      <div class="top-toolbar" >
        <a-button style="margin-right: 10px" @click="close">
          <slot name="icon"><close-outlined /></slot>关闭
        </a-button>
        <template v-if="pageType!=='view'">
          <a-button style="margin-right: 10px" type="primary" @click="handleSubmit">
            <slot name="icon"><save-outlined /></slot>保存
          </a-button>
        </template>
      </div>
      <a-form ref="formRef" :model="formState" :rules="rules" v-bind="layout">
        <Card title="基础信息" :bordered="false" >
          <a-row>
            <a-col :span="8">
              <a-form-item label="船期编号" name="ssNo">
                <a-input-search v-model:value="formState.ssNo" :disabled="Boolean(isDisable || pageType )" placeholder="请选择船期"  readonly @search="onSearchShip"/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="交易主体" name="comName">
                <a-input v-model:value="formState.comName" disabled/>
              </a-form-item>
            </a-col>
    
            <a-col :span="8">
              <a-form-item label="合同" name="kName">
                <a-input v-model:value="formState.kName" disabled />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="供应商" name="suName">
                <a-input v-model:value="formState.suName" disabled/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="接收站" name="staName">
                <a-input-search v-model:value="formState.staName" disabled/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="卸港ETA" name="dateEta">
                <a-date-picker v-model:value="formState.dateEta" style="width: 100%" disabled />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="计量类型" name="typeCode">
                <a-select v-model:value="formState.typeCode" :disabled="isDisable" placeholder="请选择" style="width: 100%" allow-clear>
                  <a-select-option v-for="item in optionSelect.typeCodeList" :key="item.code" :value="item.code">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="计量时间" name="dateMea">
                <a-date-picker v-model:value="formState.dateMea" style="width: 100%" placeholder="请选择" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="商检公司" name="inspName">
                <a-input v-model:value="formState.inspName" style="width: 100%" :disabled="isDisable" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="热值(MMBtu)" name="qtyMmbtu">
                <input-number v-model:value="formState.qtyMmbtu" :disabled="isDisable" :digits="3" :min="0" style="width: 100%" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="热值(GJ)" name="qtyGj">
                <input-number v-model:value="formState.qtyGj" :disabled="isDisable" :digits="3" :min="0" style="width: 100%" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="重量(吨)" name="qtyTon">
                <input-number v-model:value="formState.qtyTon" :disabled="isDisable" :digits="3" :min="0" style="width: 100%" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="液态体积(方)" name="qtyM3L">
                <input-number v-model:value="formState.qtyM3L" :disabled="isDisable" :digits="3" :min="0" style="width: 100%" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="气态体积(方)" name="qtyM3">
                <input-number v-model:value="formState.qtyM3" :disabled="isDisable" :digits="3" :min="0" style="width: 100%" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="密度(吨/方)" name="rateTonM3L">
                <input-number v-model:value="formState.rateTonM3L" :disabled="isDisable" :digits="3" :min="0" style="width: 100%" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="热值比(吨/GJ)" name="rateTonGj">
                <input-number v-model:value="formState.rateTonGj" :disabled="isDisable" :digits="3" :min="0" style="width: 100%" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="热值比(方/GJ)" name="rateM3Gj">
                <input-number v-model:value="formState.rateM3Gj" :disabled="isDisable" :digits="3" :min="0" style="width: 100%" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="气化率(吨/方)" name="rateTonM3">
                <input-number v-model:value="formState.rateTonM3" :disabled="isDisable" :digits="3" :min="0" style="width: 100%" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :span="22">
              <a-form-item label="备注" name="note" :label-col="{ span: 3 }" :wrapper-col="{ span: 24 }">
                <a-textarea  v-model:value="formState.note" :disabled="isDisable"  placeholder="请输入备注，最多200字" :maxlength="200"  :auto-size="{ minRows: 2, maxRows: 5 }"/>
              </a-form-item>
            </a-col>
          </a-row>
        </Card>
        <Card title="附件信息" :bordered="false" >
          <UploadList :disabled="isDisable" :list="dataFile" :value="formState.filePath" :tableName="tableName" :columnName="columnName" @change="uploadListChange"/>
        </Card>
      </a-form>
    </div>
    <OpsPurIntListModal @register="registerShip" @success="handleSuccessShip" />
  </a-spin>
</template>

<script lang="ts" setup>
    import { Card } from 'ant-design-vue';
    import { useRouter } from 'vue-router';
    import { FromPageType, RecordType } from '/@/enums/workflowEnum';
    import { ref, computed, onMounted, onBeforeMount, nextTick, defineAsyncComponent, reactive, defineComponent, watch} from 'vue';
    import { SendOutlined, SaveOutlined, CloseOutlined, } from '@ant-design/icons-vue';
    import { useMessage } from '/@/hooks/web/useMessage';
    import { useI18n } from '/@/hooks/web/useI18n';
    import { useMultipleTabStore } from '/@/store/modules/multipleTab';
    import useEventBus from '/@/hooks/event/useEventBus';
    import type { Rule } from 'ant-design-vue/es/form';
    import { getDictionary } from '/@/api/sales/Customer';
    import { useModal } from '/@/components/Modal';
    import { addLngMeaPurInt,updateLngMeaPurInt, getLngMeaPurInt} from '/@/api/ship/MeaPurInt';
    import dayjs from 'dayjs';
    import { getAppEnvConfig } from '/@/utils/env';
    import { message } from 'ant-design-vue';
    import UploadList from '/@/components/Form/src/components/UploadList.vue';
    import OpsPurIntListModal from '/@/components/common/OpsPurIntListModal.vue';
    import { useUserStore } from '/@/store/modules/user';
    import { getLngOpsPurInt} from '/@/api/ship/OpsPurInt';
    const userStore = useUserStore();
    const userInfo = userStore.getUserInfo;

    const tableName = 'MeaPurInt';
    const columnName = 'MeaPurInt'

    const formType = ref('2'); // 0 新建 1 修改 2 查看
    const formRef = ref();
    const props = defineProps({
      disabled: false,
      id: ''
    
    });
    const { bus, FORM_LIST_MODIFIED } = useEventBus();

    const router = useRouter();
    const { currentRoute } = router;
    const isDisable = ref(false);
    const { formPath } = currentRoute.value.query;
    const pathArr = [];

    const tabStore = useMultipleTabStore();
    const formProps = ref(null);
    const formId = ref(currentRoute.value?.params?.id);
    const pageType = ref(currentRoute.value.query?.type);
    const pageId = ref(currentRoute.value.query?.id)
    
    const spinning = ref(false);
    const { notification } = useMessage();
    const { t } = useI18n()

    const formState = reactive({
      approCode: 'WTJ'
    });
    const [registerShip, { openModal:openModalShip}] = useModal();
    
    const rules= reactive({
      ssNo: [{ required: true, message: "该项为必填项", trigger: 'change' }],
      typeCode: [{ required: true, message: "该项为必填项", trigger: 'change' }],
      dateMea: [{ required: true, message: "该项为必填项", trigger: 'change' }],
      qtyMmbtu: [{ required: true, message: "该项为必填项", trigger: 'change' }],
      qtyGj: [{ required: true, message: "该项为必填项", trigger: 'change' }],
      qtyTon: [{ required: true, message: "该项为必填项", trigger: 'change' }],
    });
    const layout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
    }
    const dataFile = ref([]);
    let optionSelect= reactive({
      typeCodeList: [],
      comIdList: [],
    });
    watch(
      () => props.id,
      (val) => {
        if (val) {
          getInfo(val)
        }  
      },
      {
        immediate: true
      }
    );
    watch(
      () => props.disabled,
      (val) => {
        isDisable.value = val
      },
      {
        immediate: true
      }
    );
    onMounted(() => {
      isDisable.value = pageType.value == 'view'
      getOption()
      if (pageId.value) {
        getInfo(pageId.value)
      }
    });
    const uploadListChange = (val) => {
      dataFile.value = val
    }
    async function getInfo(id)  {
      spinning.value = true
      try {
      let data = await getLngMeaPurInt(id)
      spinning.value = false
      Object.assign(formState, {...data})
      Object.assign(dataFile.value, formState.lngFileUploadList || [])
      formState.dateEta = formState.dateEta ? dayjs(formState.dateEta) : null
      formState.dateMea = formState.dateMea ? dayjs(formState.dateMea) : null

      } catch (error) {
        spinning.value = false
      }
    }

    async function getOption() {
      optionSelect.typeCodeList = await getDictionary('LNG_MEA_I')
     
    }
    const onSearchShip = () => {
      openModalShip(true,{isUpdate: false})
    }
    const handleSuccessShip = (val) => {
      
      formState.ssNo = val[0].ssNo
      formState.opsId = val[0].id
      getLngShipInfo(val[0].id)
    }
    const getLngShipInfo = async (id) => {
      try {
        spinning.value = true
        let data = await getLngOpsPurInt(id)
        spinning.value = false
        formState.comId = data.comId
        formState.comName = data.comName
        formState.kId = data.kId
        formState.kName = data.kName
        formState.suCode = data.suCode
        formState.suName = data.suName
        formState.staCode = data.staCode
        formState.staName = data.staName
        formState.dateMea = data.dateEta ? dayjs(data.dateEta) : null
        formState.dateEta = data.dateEta ? dayjs(data.dateEta) : null
        formState.inspName = data.inspName
       
      } catch (error) {
        spinning.value = false
      }
    }
    function close() {
      tabStore.closeTab(currentRoute.value, router);
    }
    async function getFormValue() {
      return formState
    }
    async function handleSubmit(type) {
      try {
        await formRef.value.validateFields();
        let obj = {
          ...formState,
          salesAreaCode: formState.salesAreaCode ? formState.salesAreaCode[formState.salesAreaCode.length -1] : '',
          lngFileUploadList: dataFile.value,
        }
        spinning.value = true;
        let request = !formState.id ? addLngMeaPurInt :updateLngMeaPurInt

        try {
          const data = await request(obj);
          notification.success({
              message: '提示',
              description: data?.id ? t('新增成功！') : t('修改成功！')
          }); //提示消息
          
          setTimeout(() => {
            bus.emit(FORM_LIST_MODIFIED, {});
            close();
          }, 500);
          
        } finally {
          spinning.value = false;
        }

      } catch (errorInfo) {
         spinning.value = false;
         errorInfo?.errorFields?.length && notification.warning({
          message: '提示',
          description: '请完善信息'
        });
        return false
      }
    }

      defineExpose({
        handleSubmit,
        getFormValue
    });
</script>

<style lang="less" scoped>
  :deep(.ant-form-item .ant-form-item-label) {
    width: 135px !important;
    max-width: 135px !important;
  }
    .page-bg-wrap {
        background-color: #fff;
    }

    .top-toolbar {
        min-height: 44px;
        margin-bottom: 12px;
        border-bottom: 1px solid #eee;
    } 
    .pdcss {
      padding:0px 12px 6px 12px !important;
    }
    :deep(.formItemWarp .ant-form-item-label > label) {
      white-space: normal !important;
      word-break: break-word !important;
    }
    
</style>
