<template>
    <a-form ref="formRef" :model="formState" :rules="rules" v-bind="layout">
      <a-row>
        <a-col :span="8">
          <a-form-item label="计划日期" name="datePlan">
            <div v-if="pageType=='compare'" :class="diffResultList.includes('datePlan')?'changeStyle':''">
              {{ formState.datePlan ? dayjs(formState.datePlan).format('YYYY-MM-DD'): null }}
            </div>
            <a-date-picker :inputReadOnly="true" v-else v-model:value="formState.datePlan" :disabled-date="disabledDateStart" style="width: 100%" :disabled="disable||pageType=='edit'" placeholder="请选择计划日期" @change="datePlanChange" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="合同" name="kName">
            <div v-if="pageType=='compare'" :class="diffResultList.includes('kName')?'changeStyle':''">{{ formState.kName }}</div>
            <a-input-search v-else v-model:value="formState.kName" :disabled="disable||pageType=='edit'" placeholder="请选择合同"  readonly @search="onSearch"/>
          </a-form-item>
        </a-col> 
        <a-col :span="8">
          <a-form-item label="气源地" name="staName">
            <div v-if="pageType=='compare'" :class="diffResultList.includes('staName')?'changeStyle':''">{{ formState.staName }}</div>
            <a-input-search v-else v-model:value="formState.staName" :disabled="disable||pageType=='edit'" placeholder="请选择气源地"  readonly @search="onSearchUpLoad"/>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="车头号" name="noTractor" :class="diffResultList.includes('noTractor')?'changeStyle':''">
            <a-input v-model:value="formState.noTractor" :disabled="disable" placeholder="请输入车头号" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="挂车号" name="noTrailer" :class="diffResultList.includes('noTrailer')?'changeStyle':''">
            <a-input v-model:value="formState.noTrailer" :disabled="disable" placeholder="请输入挂车号" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="承运商" name="carrName" :class="diffResultList.includes('carrName')?'changeStyle':''">
            <a-input v-model:value="formState.carrName" :disabled="disable" placeholder="请输入承运商" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="驾驶员身份证号" name="idNoDriver" :class="diffResultList.includes('idNoDriver')?'changeStyle':''">
            <a-input v-model:value="formState.idNoDriver" :disabled="disable" placeholder="请输入驾驶员身份证号" />
          </a-form-item>
        </a-col>
         <a-col :span="8">
          <a-form-item label="驾驶员姓名" name="nameDriver" :class="diffResultList.includes('nameDriver')?'changeStyle':''">
            <a-input v-model:value="formState.nameDriver" :disabled="disable" placeholder="请输入驾驶员姓名" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="驾驶员手机号" name="phoneDriver" :class="diffResultList.includes('phoneDriver')?'changeStyle':''">
            <a-input v-model:value="formState.phoneDriver" :disabled="disable" placeholder="驾驶员手机号" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="押运员身份证号" name="idNoEscort" :class="diffResultList.includes('idNoEscort')?'changeStyle':''">
            <a-input v-model:value="formState.idNoEscort" :disabled="disable" placeholder="请输入押运员身份证号" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="押运员姓名" name="nameEscort" :class="diffResultList.includes('nameEscort')?'changeStyle':''">
            <a-input v-model:value="formState.nameEscort" :disabled="disable" placeholder="请输入押运员姓名" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="押运员手机号" name="phoneEscort" :class="diffResultList.includes('phoneEscort')?'changeStyle':''">
            <a-input v-model:value="formState.phoneEscort" :disabled="disable" placeholder="请输入押运员手机号" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="卸货站点" name="unloadingName" :class="diffResultList.includes('unloadingName')?'changeStyle':''">
            <a-input-search v-model:value="formState.unloadingName" :disabled="disable" placeholder="请选择卸货站点" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="预约时间段" name="timePeriod" :class="diffResultList.includes('timePeriod')?'changeStyle':''">
            <a-select v-model:value="formState.timePeriod" :disabled="disable" placeholder="请选择预约时间段" style="width: 100%" allow-clear>
              <a-select-option v-for="item in optionSelect.timePeriodList" :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="timeUnloading">
            <div v-if="pageType=='compare'" :class="diffResultList.includes('timeUnloading')?'changeStyle':''">
              {{ formState.timeUnloading ? dayjs(formState.timeUnloading).format('YYYY-MM-DD'): null }}
            </div>
            <a-date-picker :inputReadOnly="true" showTime v-else v-model:value="formState.timeUnloading"  style="width: 100%" :disabled="disable" placeholder="请选择预计卸货时间"  />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="车辆类型" name="supplyCode" :class="diffResultList.includes('supplyCode')?'changeStyle':''">
            <a-select v-model:value="formState.supplyCode" :disabled="disable" placeholder="请选择车辆类型" style="width: 100%" allow-clear>
              <a-select-option v-for="item in optionSelect.supplyCodeList" :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="noTank" :class="diffResultList.includes('noTank')?'changeStyle':''">
            <a-input v-model:value="formState.noTank" :disabled="disable" placeholder="请输入压力容器登记证号" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="充装量(吨)" name="qtyTon" :class="diffResultList.includes('qtyTon')?'changeStyle':''">{{ formState.qtyTon }}</a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="供应商" name="comName" :class="diffResultList.includes('comName')?'changeStyle':''">{{ formState.comName }}</a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="版本号" name="verNo" :class="diffResultList.includes('verNo')?'changeStyle':''">{{ formState.verNo }}</a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="审批状态" name="approName" :class="diffResultList.includes('approName')?'changeStyle':''">{{ formState.approName }}</a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注" name="note" :label-col="{ span: 3 }" :wrapper-col="{ span: 24 }">
            <div v-if="pageType=='compare'" :class="diffResultList.includes('note')?'changeStyle':''">{{ formState.note }}</div>
            <a-textarea v-else v-model:value="formState.note" :disabled="disable"  placeholder="请输入备注"  :auto-size="{ minRows: 2, maxRows: 5 }"/>
          </a-form-item>
        </a-col>
            <a-col :span="24">
          <a-form-item label="批复意见" name="reply" :label-col="{ span: 3 }" :wrapper-col="{ span: 24 }">
            <div v-if="pageType=='compare'" :class="diffResultList.includes('reply')?'changeStyle':''">{{ formState.reply }}</div>
            <a-textarea v-else v-model:value="formState.reply" disabled  :auto-size="{ minRows: 1, maxRows: 5 }"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  <contractDemandListModal @register="registerContract" @success="handleSuccessContract" />
  <pointDelyDemandListModal @register="registerUpLoad" @success="handleSuccessUpLoad" />
</template>

<script lang="ts" setup>
    import { useI18n } from '/@/hooks/web/useI18n';
    import { useRouter } from 'vue-router';
    import { ref, computed, onMounted, onBeforeMount, nextTick, defineAsyncComponent, reactive, defineComponent, watch} from 'vue';
    import contractDemandListModal from '/@/components/common/contractDemandListModal.vue';
    import pointDelyDemandListModal from '/@/components/common/pointDelyDemandListModal.vue';
    import { useModal } from '/@/components/Modal';
    import { message } from 'ant-design-vue';
    import dayjs from 'dayjs';
    import { getCompDept } from '/@/api/approve/Appro';
    import { useUserStore } from '/@/store/modules/user';
    import { getDictionary } from '/@/api/sales/Customer';
    import { getLngLngDemandContractList, getLngLngDemandStationList } from '/@/api/dayPlan/LngDemand';
    
    const userStore = useUserStore();
    const userInfo = userStore.getUserInfo;
    const router = useRouter();
    const { currentRoute } = router;
    const pageId = ref(currentRoute.value.query?.id)
    const pageType = ref(currentRoute.value.query?.type)
    const formRef = ref()
    const rules= reactive({
      pointDelyName: [{ required: true, message: "该项为必填项", trigger: 'change' }],
      datePlan: [{ required: true, message: "该项为必填项", trigger: 'change' }],
      kName: [{ required: true, message: "该项为必填项", trigger: 'change' }],
    });
    const layout = {
      labelCol: { span: 9 },
      wrapperCol: { span: 18 },
    }
    const { t } = useI18n();
    const [registerContract, { openModal:openModalContractSales}] = useModal();
    const [registerUpLoad, { openModal:openModalUpLoad}] = useModal();
    const props = defineProps({
      formObj: {},
      disable: false,
      changeList: []
    });
    const optionSelect= reactive({
      timePeriodList: [],
      supplyCodeList: []
    })
    const diffResultList = ref([])
    const formState = ref({})
    const contractList = ref([])
    const pointDelyList = ref([])
    onMounted(async () =>{
      getOption()
      if (!pageType.value) {
        formState.value.lastVerSign = 'Y'
        formState.value.validSign = 'N'
        formState.value.alterSign = 'I'
        formState.value.approCode = 'WTJ'
        const res = await getCompDept(userInfo.id)
        formState.value.cuCode = res?.comp?.cuCode
        formState.value.comId = res?.comp?.id
        const res1 = await getLngPngDemandRate({}) || []
        formState.value.rateM3Gj = res1[0]?.rateM3Gj
      }
  
      
    })
    async function getOption() {
      optionSelect.timePeriodList = await getDictionary('LNG_TIME_P')
      optionSelect.supplyCodeList = await getDictionary('LNG_SUPPLY')
    }
    const datePlanChange = async (val) => {
      let obj = {
        cuCode:formState.value.cuCode, 
        datePlan: dayjs(formState.value.datePlan).format('YYYY-MM-DD')
      }
      let res = await getLngLngDemandContractList(obj)
      contractList.value = res || []
      if (contractList.value.length == 1) {
        formState.value.kName = contractList.value[0].kName
        formState.value.ksId = contractList.value[0].id
        getPointDely()
        getContractQty()
      }
    }
    const onSearch = ()=> {
      if (!formState.value.datePlan) {
        message.warn('请选择计划日期')
        return
      }
      openModalContractSales(true,{isUpdate: false, list: contractList.value})
    }
    const handleSuccessContract = (val) => {
      formState.value.kName = val[0].kName
      formState.value.ksId = val[0].id
      getPointDely()
      getContractQty()
      
    }
    const onSearchUpLoad = (val)=> {
      if (!formState.value.kName) {
        message.warn('请选择合同')
        return
      }
      openModalUpLoad(true,{isUpdate: false, list: pointDelyList.value})
    }
    const handleSuccessUpLoad = (val) => {
      formState.value.pointDelyName = val[0].pointDelyName
      formState.value.pointDelyCode = val[0].pointDelyCode
      formState.value.ksppId = val[0].id
      getPurList()
    }
    const getPointDely = async () => {
      let res = await getLngLngDemandStationList({kId: formState.value.ksId})
      pointDelyList.value = res || []
      if (pointDelyList.value.length == 1) {
        formState.value.pointDelyName = pointDelyList.value[0].pointDelyName
        formState.value.pointDelyCode = pointDelyList.value[0].pointDelyCode
        formState.value.ksppId = pointDelyList.value[0].id

      }
    }
    const getContractQty = async () => {
      let obj = {
        kId: formState.value.ksId, 
        datePlan: dayjs(formState.value.datePlan).format('YYYY-MM-DD')
      }
      let res = await getLngPngDemandContractQty(obj) || []
      formState.value.qtyContractGj = res[0]?.qtyContractGj
      formState.value.qtyContractM3 = res[0]?.qtyContractM3
      formState.value.qtyContractM3 =NP.divide(Number(formState.value.qtyContractM3), 10000)
    
    }
    const disabledDateStart = (current) => {
      const today = new Date();
      today.setHours(0, 0, 0, 0); // 清除时分秒，确保比较时不考虑时间部分
      // 获取明天的日期
      const tomorrow = new Date();
      tomorrow.setDate(tomorrow.getDate() + (pageType.value?1: 2));
      tomorrow.setHours(0, 0, 0, 0); // 清除时分秒
      // 当前日期小于今天或大于明天，则禁用
      return current < today || current > tomorrow;
    }
    async function getFormValue () {
      await formRef.value.validateFields();
      let obj = {
        formInfo: formState.value,
      }
      return obj
    }
    watch(
      () => props.formObj,
      (val) => {
        if (val) {
          formState.value = val
        }  
      },
      {
        immediate: true,
        deep: true,
      }
    );
    watch(
      () => props.changeList,
      (val) => {
        diffResultList.value = val || []
      },
      {
        immediate: true,
        deep: true,
      }
    );
    defineExpose({
      getFormValue
    });

</script>

<style lang="less" scoped>
 
    .dot {
      margin-right: 10px;
      display: flex;
      align-items: center;
      .ant-input-number{
        margin:0 5px;
        font-style: normal;
      }
      span{
        width: 10px !important;
        height: 10px !important;
        border-radius: 50%;
        background: red;
        display: block;
      }
    }
    .changeStyle {
      color: red !important;
    }
    :deep(.changeStyle .ant-input) {
      color: red !important;
    }
</style>
