<template>
  <!-- 添加二程船水运费1-->
  <div class="addWagonLoad">
    <PageTitle>返回</PageTitle>
    <div class="form-box">
      <!-- <dil-form :formId="271" v-model="form1" ref="from1"></dil-form> -->
      <el-form v-model="form">
        <div class="searchSelect">
          <span class="text">江船</span>
          <el-input class="input" v-model="batchName" disabled> </el-input>
          <el-button class="button" type="primary" @click="ondrawer(5)"
            >浏览</el-button
          >
        </div>
        <div class="switch">
          <el-switch v-model="value1" active-text="拼装" inactive-text="不拼装">
          </el-switch>
        </div>
        <div class="searchSelect">
          <span class="text">单价</span>
          <el-input
            class="input"
            v-model="form.unitPrice"
            disabled
            @change="calculate"
          >
          </el-input>
          <el-button class="button" type="primary" @click="ondrawer(6)"
            >浏览</el-button
          >
        </div>
        <div class="preview-group">
          <el-form-item label="港口数" v-if="spell == 'true'">
            <el-input v-model="form.portNum" type="number"></el-input>
          </el-form-item>
          <el-form-item label="船数" v-if="noSpell == 'false'">
            <el-input v-model="form.shipNum" type="number"></el-input>
          </el-form-item>
          <el-form-item label="开票日期">
            <el-date-picker
              v-model="form.makeTime"
              type="datetime"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <!-- 模态框 -->
    <el-drawer
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
      size="50%"
    >
      <el-input
        placeholder="请输入内容"
        v-model="input"
        style="margin-top: 10px; margin-left: 20px; width: 250px"
        clearable
      >
      </el-input>
      <el-button
        type="primary"
        class="btn"
        @click="onclick(a)"
        style="margin-bottom: 15px"
      >
        <i class="el-icon-search"></i>查询
      </el-button>
      <div v-show="a == 5">
        <dilTable
          v-bind.sync="batchOption"
          @radio-change="batchChange"
          :isKuang="isKuang"
        ></dilTable>
      </div>
      <div v-show="a == 6">
        <dilTable
          v-bind.sync="contractPriceOption"
          @radio-change="priceChange"
          :isKuang="isKuang"
        ></dilTable>
      </div>
    </el-drawer>
    <!-- 底部按钮 -->
    <div class="button-box">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="makeSure" :loading="isLoading"
        >确定</el-button
      >
    </div>
  </div>
</template>

<script>
import PageTitle from "@/components/Page/Title";
import { sjTime } from "@/utils/sharedJsFile";
import { getCookie } from "@/utils/util.js";
import { BigNumber } from "bignumber.js";
export default {
  components: { PageTitle },
  data() {
    return {
      isKuang: false,

      spell: false,
      noSpell: "false",
      value1: false,
      form: {},
      portId: null,
      ports: [],
      isLoading: false,
      drawer: false,
      direction: "rtl",
      a: 0,
      input: null,
      batchId: null,
      purchaseOrderId: null,
      batchName: null,
      cargoPictureUrl: "",
      batchOption: {
        requestUrl: "/api/v1/bms/getShipResult?apiId=112",
        selectionType: "radio",
        map: []
      },
      contractPriceOption: {
        requestUrl: "/api/v1/rms/selectbmsshipContractPriceList?apiId=500",
        selectionType: "radio",
        map: []
      }
    };
  },
  mounted() {
    //this.getPorts();
  },
  computed: {
    portNum() {
      return this.form.portNum;
    }
  },
  watch: {
    portNum() {
      console.log(this.form.portNum);
    },
    value1() {
      if (this.value1) {
        this.spell = "true";
        this.noSpell = "true";
      } else {
        this.spell = "false";
        this.noSpell = "false";
      }
    }
  },
  methods: {
    //右侧选中框
    ondrawer(num) {
      this.drawer = true;
      this.isKuang = true;
      this.a = num;
      if (num == 5) {
        this.batchOption.requestUrl =
          "/api/v1/bms/getShipResult?apiId=112" + "&i=" + new Date();
      } else if (num == 6) {
        this.contractPriceOption.requestUrl =
          "/api/v1/rms/selectbmsshipContractPriceList?apiId=500" +
          "&i=" +
          new Date();
      }
      // 清空当前输入框中的数据
      this.input = "";
    },
    handleClose(done) {
      done();
    },
    onclick(a) {
      if (a == 5) {
        this.batchOption.requestUrl =
          "/api/v1/bms/getShipResult?apiId=112&index=" + this.input;
      } else if (a == 6) {
        this.contractPriceOption.requestUrl =
          "/api/v1/rms/selectbmsshipContractPriceList?apiId=500&con=" +
          this.input;
      }
    },
    //修改选中
    batchChange(selection) {
      console.log(selection);
      this.batchId = selection.batchId;
      this.purchaseOrderId = selection.purchaseOrderId;
      this.batchName = selection.materialName + selection.resultForeignShipName;
    },
    priceChange(selection) {
      this.form.unitPriceId = selection.resultId;
      this.form.unitPrice = selection.unitPrice;
      this.calculate();
    },
    //计算水运费
    calculate() {
      console.log(
        "能否计算水运费：",
        this.form.realTonnage && this.form.unitPrice
      );
      if (this.form.realTonnage && this.form.unitPrice) {
        let fee = new BigNumber(this.form.realTonnage);
        this.$set(
          this.form,
          "fee",
          fee.multipliedBy(this.form.unitPrice).toNumber()
        );
        console.log(this.form.fee);
      }
    },
    makeSure() {
      this.isLoading=true;
      let map = {
        batchId: this.batchId,

        purchaseOrderId: this.purchaseOrderId,
        unitPrice: this.form.unitPrice,
        unitPriceId: this.form.unitPriceId,
        shipNum: this.form.shipNum,
        portNum: this.form.portNum,
        makeTime: sjTime(this.form.makeTime),
        feeType: 5,
        userId: getCookie("userId")
      };
      console.log(map);
      if (
        map.batchId == null ||
        map.purchaseOrderId == null ||
        map.unitPrice == null ||
        map.unitPriceId == null ||
        map.makeTime == null
      ) {
        this.$message.error("存在空值！");
        this.isLoading = false;
      } else {
        this.axios
          .post("/api/v1/bms/addwaterStatement", map)
          .then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "新增成功!"
              });
              this.cancel();
            } else {
              this.$message.error(res.data.data);
              this.isLoading = false;
            }
          })
          .then(() => {
            this.isLoading = false;
          });
      }
    },
    // 取消
    cancel() {
      this.$router.go(-1);
    },
    //查询港口id
    getPorts() {
      this.axios.post("/api/v1/rms/getPortName?index=").then(res => {
        if (res.data.code == 200) {
          this.ports = res.data.data;
          console.log(this.ports);
        } else {
          this.$message.error(res.data.data);
        }
      });
    },

    upCLick(val) {
      this.num = val;
    }
  }
};
</script>
<style lang="scss">
.searchSelect {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 5px;
  .text {
    width: 40px;
  }
  .input {
    width: 200px;
  }
  .button {
    margin-left: 6px;
  }
}
.switch {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.button-box {
  display: flex;
  justify-content: center;
  margin: 20px;
  .el-button {
    width: 80px;
    margin: 10px;
  }
}
.form-box {
  display: flex;
  justify-content: center;
  .el-form-item {
    display: flex;
    justify-content: center;
    .el-form-item__label {
      display: flex;
      align-items: center;
    }
    .el-form-item__content {
      .el-input {
        .el-input__inner {
          width: 250px;
        }
      }
    }
  }
}
</style>
