<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=='edit'||pageType=='update' ">
          <a-button style="margin-right: 10px" type="primary" @click="checkBtn('save')">
            <slot name="icon"><save-outlined /></slot>保存
          </a-button>
          <a-button style="margin-right: 10px" @click="checkBtn('submit')">
            <slot name="icon"><send-outlined /></slot>保存并提交
          </a-button>
        </template>
      </div>
    
      <Card :title="title" :bordered="false" v-if="pageType=='compare'&&titleNew">
        <basicForm :formObj="formStateNew" :changeList="diffResultList" :list="dataListNew" :disable="true"></basicForm>
      </Card>
      <Card :title="title" :bordered="false" v-if="title">
        <basicForm ref="formRef" :formObj="formState" :list="dataList" :disable="pageType=='view' || pageType=='compare'"></basicForm>
      </Card>
    
    </div>
  </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 { addLngPngDemand, submitLngPngDemand,getLngPngDemand,getLngPngDemandUpdate,getLngPngDemandCompare } from '/@/api/dayPlan/Demand';
    import dayjs from 'dayjs';
    import { getAppEnvConfig } from '/@/utils/env';
    import { message } from 'ant-design-vue';
    import { useUserStore } from '/@/store/modules/user';
    import basicForm from './basicForm.vue'

    const userStore = useUserStore();
    const userInfo = userStore.getUserInfo;
    const formRef = ref();
    const { bus, FORM_LIST_MODIFIED } = useEventBus();

    const router = useRouter();
    const { currentRoute } = router

    const tabStore = useMultipleTabStore();
    const formProps = ref(null);
    const pageId = ref(currentRoute.value.query?.id)
    const pageType = ref(currentRoute.value.query?.type)
    
    const spinning = ref(false);
    const { notification } = useMessage();
    const { t } = useI18n();
    const formState = reactive({
      datePlan: null
    });
    const formStateNew = reactive({})
    const [register, { openModal:openModal}] = useModal();
  
    const title = ref('管道气需求')
    const titleNew = ref('')
     const dataListNew = ref([])
    const basicFormRef = ref()
    const diffResultList = ref([])
    const dataList = ref([])
    onMounted(() => {
      if (pageId.value) {
        if (pageType.value=='compare') {
          getCompareInfo(pageId.value)
          return
        }
        getInfo(pageId.value)
      }
   
    });
    async function getCompareInfo(id) {
       spinning.value = true
      try {
        let data = await getLngPngDemandCompare(id) || {}
        spinning.value = false
        diffResultList.value = data.diffResultList || []
        let obj = changeData(data.oldBean || {})
        Object.assign(formState, {...obj.params})
        Object.assign(dataList.value, obj.list || [])
        title.value = formState.verNo? ('版本V'+ formState.verNo) : ''

        let obj1 = changeData(data.newBean || {})
        Object.assign(formStateNew, {...obj1.params})
        Object.assign(dataListNew.value, obj1.list || [])
        titleNew.value = formStateNew.verNo ? ('版本V'+ formStateNew.verNo) : ''

      } catch (error) {
        console.log(error,'error555555')
        spinning.value = false
      }

    }
    async function getInfo(id)  {
      spinning.value = true
      try {
        let data = {}
        if (pageType.value == 'update') {
          data = await getLngPngDemandUpdate(id)
        } else {
          data = await getLngPngDemand(id)
        }
        spinning.value = false
        let obj = changeData(data)
        Object.assign(formState, {...obj.params})
        Object.assign(dataList.value, obj.list || [{}])

      } catch (error) {
        console.log(error, 'error')
        spinning.value = false
      }
    }
    const changeData = (obj) => {
      let arr = obj.lngPngDemandPurList || []
      obj.datePlan = obj.datePlan ? dayjs(obj.datePlan) : null
      obj.qtyContractM3 = Number(obj.qtyContractM3)/10000
      obj.qtyPlanM3 = Number(obj.qtyPlanM3)/10000
      obj.qtyDemandM3 = Number(obj.qtyDemandM3)/10000
      obj.qtySalesM3 = Number(obj.qtySalesM3)/10000
      
      arr.length && arr.forEach(v => {
        v.qtyDemandM3 = Number(v.qtyDemandM3)/10000
        v.qtySalesM3 = Number(v.qtySalesM3)/10000

        v.qtyDemandM3 = Number(v.qtyDemandM3) ? Number(v.qtyDemandM3).toFixed(5) : ''
        v.qtySalesM3 = Number(v.qtySalesM3) ? Number(v.qtySalesM3).toFixed(5) : ''
      });

      obj.qtyContractM3 = Number(obj.qtyContractM3) ? Number(obj.qtyContractM3).toFixed(5) : ''
      obj.qtyPlanM3 = Number(obj.qtyPlanM3) ? Number(obj.qtyPlanM3).toFixed(5) : ''
      obj.qtyDemandM3 = Number(obj.qtyDemandM3) ? Number(obj.qtyDemandM3).toFixed(5) : ''
      obj.qtySalesM3 = Number(obj.qtySalesM3) ? Number(obj.qtySalesM3).toFixed(5) : ''

      return {
        list : arr,
        params: obj
      }
    } 
    function close() {
      tabStore.closeTab(currentRoute.value, router);
    }
    async function checkBtn(type) {
      try {
        const data = await formRef.value.getFormValue();
        let arr = JSON.parse(JSON.stringify(data.list))
        arr.forEach(v=> {
          v.qtyDemandM3 = Number(v.qtyDemandM3)*10000
          v.qtySalesM3 = Number(v.qtySalesM3)*10000
        })
        let obj = {
          ...data.formInfo,
          datePlan: dayjs(data.formInfo.datePlan).format('YYYY-MM-DD HH:mm:ss'),
          qtyContractM3: Number(data.formInfo.qtyContractM3)*10000,
          qtyPlanM3: Number(data.formInfo.qtyPlanM3)*10000,
          qtyDemandM3: Number(data.formInfo.qtyDemandM3)*10000,
          qtySalesM3: Number(data.formInfo.qtySalesM3)*10000,
          lngPngDemandPurList:arr
        }
        spinning.value = true;
        let request = ''
        request = type == 'save'? addLngPngDemand : submitLngPngDemand
        await request(obj)
        spinning.value = false;
        notification.success({
          message: 'Tip',
          description: type == 'save' ? '保存成功':'已提交'
        }); //提示消息
        setTimeout(() => {
          bus.emit(FORM_LIST_MODIFIED, {});
          close();
        }, 500);

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

      
  }


</script>

<style lang="less" scoped>
    .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;
    }
    .dot {
      margin-right: 10px;
      display: flex;
      align-items: center;
      i{
        padding: 5px;
        font-style: normal;
      }
      span{
        width: 10px !important;
        height: 10px !important;
        border-radius: 50%;
        background: red;
        display: block;
      }
    }
    
</style>
