<template>
  <!-- 修改位置作业 -->
    <div class="shipTransport">
    <page-title>返回</page-title>
    <div class="form">
      <div class="form_box">
        <dil-form :formId="136" v-model="form1" ref="from1"></dil-form>
        <el-form :inline="true">
          <el-form-item label="水运单价">
            <el-input type="number" placeholder="水运单价" v-model="form1.waterUnitPrice" disabled
            ></el-input>
          </el-form-item> 
          <el-form-item label="是否拼装" >
            <el-radio v-model="form1.isNeedAssemble" label="否" style="margin-left :10px">否</el-radio>
            <el-radio v-model="form1.isNeedAssemble" label="是">是</el-radio>
          </el-form-item>
          <el-form-item label="水分检测费">
             <el-input type="number" placeholder="水分检测费" v-model="form1.resultInspectionFees"></el-input>
          </el-form-item>
          <!-- <el-form-item label="计划日期">
            <el-date-picker
            v-model="form1.planDate"
            type="datetime"
            placeholder="计划日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="预计放货日期">
            <el-date-picker
            v-model="form1.planDeliveryDate"
            type="datetime"
            placeholder="预计放货日期">
            </el-date-picker>
          </el-form-item> -->
          <el-form-item label="含水率(%)">
             <el-input type="number" placeholder="含水率" v-model="form1.resultMoistureContent"
             @change="pipeiContarct"></el-input>
          </el-form-item>
          <el-form-item label="合同水分值">
             <el-input
             type="number"
              placeholder=""
              v-model="form1.contractMoistureValue"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="水分吨位">
            <el-input
            type="number"
              placeholder=""
              v-model="form1.moistureTonnage"
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-form> 
      </div> 
    </div>
    <!-- 确定和取消 -->
    <div class="button_box">
      <el-button type="primary" @click="makeSure">确认</el-button>
      <el-button @click="onClickCancel">返回</el-button>
    </div>
  </div>
</template>

<script>
import PageTitle from "@/components/Page/Title";
import { sjTime } from "@/utils/sharedJsFile";
import { getCookie } from "@/utils/util.js";
export default {
  components: { PageTitle },
 data(){
     return {
      form1: {},
      waterQualiyResultId:null,
      outWharyResultId:null,
      unitPrices:[]
  }
 },
  mounted() {
    this.information();
  },
  watch:{
    "form1.resultOutPortTime":{
      handler(newVal,oldVal){
        this.findUnitPrice();
      },
      deep: true,
      immediate: true
    },
    "form1.isNeedAssemble":{
      handler(newVal,oldVal){
        this.isNeedAssemble();
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    getUnitPrice(){
      this.axios
            .post("/api/v1/rms/selectbmsshipContractPriceList?apiId=500")
            .then((res) => {
                this.unitPrices=res.data.data.list;
        }).then(()=>{
            this.findUnitPrice();
        });
    },
    findUnitPrice(){
        console.log("form1",this.form1);
      if(this.form1.sendPortId && this.form1.resultOutPortTime){
          let contract=this.unitPrices.find(item=>
          item.portId==this.form1.sendPortId
          && sjTime(this.form1.resultOutPortTime)>=sjTime(item.startTime)
          && sjTime(this.form1.resultOutPortTime)<=sjTime(item.endTime));
          if(contract){
             this.form1.waterUnitPrice=contract.unitPrice;
             this.form1.unitPriceId=contract.resultId;
             console.log("contract:",contract);
             this.$forceUpdate();
          }
      }else{
        console.log("null");
      }
    },
    isNeedAssemble(){
        if(this.form1.isNeedAssemble){
            if(this.form1.isNeedAssemble=="是" && this.form1.sendPortId!=this.form1.startPortId){
                this.form1.resultInspectionFees=4000;
            }else{
                this.form1.resultInspectionFees=2000;
            }
        }else{
            console.log("是否拼装为空");
        }
    },
    information() {
      //编辑位置作业
      this.axios
        .post("/api/v1/tms/getLoadShip/" + this.$route.params.resultId)
        .then((res) => {
          res.data.data.forEach((e) => {
            this.form1 = e;
            this.waterQualiyResultId=e.waterQualiyResultId;
            this.outWharyResultId=e.outWharyResultId;
            console.log("form1:",e);
          });
        }).then(()=>{
            this.getUnitPrice();
        });
    },
    handleCheckedCitiesChange(value) {
      console.log(value);
    },
    // 返回
    onClickCancel() {
      this.$router.go(-1);
    },
    currentRadioChange(row) {
      this.aaaa = row;
    },
    pipeiContarct() {
      if (
        this.form1.resultMoistureContent >= 0
      ) {
        let map = {
          batchInfactoryId: this.form1.batchId,
          resultMoistureContent: this.form1.resultMoistureContent/100,
          resultActualInstallations:this.form1.resultActualInstallations,
        };
        this.axios
          .post("/api/v1/tms/getMoistureAndInstallations", map)
          .then(res => {
            if (res.data.code == 200) {
              console.log("res:",res);
              this.form1.contractMoistureValue = res.data.data.purchaseContractMoisture;
              this.form1.moistureTonnage = res.data.data.resultWaterTonnage;
              this.$forceUpdate();
            } else {
              this.$message.warning(res.data.data);
            }
          });
      }
    },
    // 确认
    makeSure() {
      let mapVal = {
        resultId: this.$route.params.resultId,
        waterQualiyResultId:this.waterQualiyResultId,
        resultActualInstallations: this.form1.resultActualInstallations,
        outWharyResultId:this.outWharyResultId,
        // rwarehouseMaterialId: this.form1.rwarehouseMaterialId,
        resultArrivalPortTime: sjTime(
          this.form1.resultArrivalPortTime
        ),
        resultOutPortTime: sjTime(
          this.form1.resultOutPortTime
        ),
        resultLoadShipDate: sjTime(
          this.form1.resultLoadShipDate
        ),
        resultIsClear: this.form1.resultIsClear,
        resultOutPortName:this.form1.resultOutPortName,
        // planDate:sjTime(this.form1.planDate),
        // planDeliveryDate:sjTime(this.form1.planDeliveryDate),
        isNeedAssemble:this.form1.isNeedAssemble,
        resultInspectionFees:this.form1.resultInspectionFees,
        moistureTonnage:this.form1.moistureTonnage,
        waterUnitPrice:this.form1.waterUnitPrice,
        unitPriceId:this.form1.unitPriceId,
        resultHeavyWeight:this.form1.resultHeavyWeight,
        resultMoistureContent:this.form1.resultMoistureContent,
        userId:getCookie("userId")
      };
              //判断放货数量是否为数字
       function isNumber() {
        var value = mapVal.resultActualInstallations;
        //验证是否为数字
        var patrn = /^(-)?\d+(\.\d+)?$/;
        if (patrn.exec(value) == null || value == "") {
          return false;
        } else {
          return true;
        }
      }
      var val=this.value;
    
    console.log("map:",mapVal);
      this.axios
        .post(
          "/api/v1/tms/updateThreeSectionDetails",
          mapVal
        )
        .then((res) => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
            // this.$refs.table.refreshData();
            this.$router.go(-1);
          } else {
            this.$message.error("修改失败！");
          }
          this.$refs['table'].resetField();
        });
    },
  },
};
</script>
<style lang="scss">
.shipTransport{
  .form{
    display: flex;
    .form_box{
      width: 320px;
      margin-left: 35%;
      margin-top: 30px;
      margin-right: 20px;
      .el-form{
        .preview-group{
          .el-form-item{
            .el-form-item__label{
              display: inline-block;
              width: 70px !important;
            }
            .el-form-item__content{
              .el-select{
                width: 250px;
              }
              .el-input{
                width: 250px;
              }
            }
          }
        }
      }
    }
  }
  .button_box{
    margin-left: 45%;
  }
}
</style>
