<template>
  <!-- 新增运输作业页面 -->
  <div id="contractDetails">
    <page-title>运输预约</page-title>

    <div class="main">
      <el-divider content-position="left">新增运输预约</el-divider>
    </div>

    <div class="material from">
      <span class="text">物资：</span>
      <el-input v-model="materialName" disabled> </el-input>
      <el-button type="primary" @click="ondrawer(1)">浏览</el-button>
    </div>

    <div class="forwardingUnit from">
      <span class="text">发货单位：</span>
      <el-input v-model="supplierName" disabled> </el-input>
      <el-button type="primary" @click="ondrawer(2)">浏览</el-button>
    </div>
    <div class="remark from">
      <span class="text">船名：</span>
      <el-input v-model="remark" @blur="onBlur"> </el-input>
      <span class="span"></span>
    </div>
    <div class="forwardingUnit from">
      <span class="text">卸货点：</span>
      <el-input v-model="unloadPointName" disabled> </el-input>
      <el-button type="primary" @click="ondrawer(4)">浏览</el-button>
    </div>
    <div class="lineId from">
      <span class="text">选择路线：</span>
      <el-input v-model="lineName" disabled> </el-input>
      <el-button type="primary" @click="ondrawer(5)">浏览</el-button>
    </div>
    <div class="contractTitle from">
      <dil-form :formId="219" v-model="form1"></dil-form>
    </div>
    <div class="truckList1">
      <div class="truckList from">
        <span class="text">选择车辆：</span>
        <el-button type="primary" @click="ondrawer(3)" class="truckBtn"
          >浏览</el-button
        >
      </div>
    </div>
    <!-- 车辆表格 -->
    <div class="switch" v-if="selectionList.length > 0">
      <el-tooltip placement="top">
        <div slot="content">下面表格已有数据，不能进行更改！！！</div>
        <el-switch
          v-model="value"
          active-text="重量"
          inactive-text="件数"
          disabled
        >
        </el-switch>
      </el-tooltip>
    </div>
    <div class="switch" v-else>
      <el-tooltip placement="top">
        <div slot="content">当下面表格有数据之后，将不能进行更改！！！</div>
        <el-switch v-model="value" active-text="重量" inactive-text="件数">
        </el-switch>
      </el-tooltip>
    </div>
    <div class="truckListTable">
      <el-table
        :data="selectionList"
        border
        style="width: 100%"
        highlight-current-row
      >
        <el-table-column type="index" width="50" label="序号" align="center">
        </el-table-column>
        <el-table-column
          v-for="(item, i) in tableTop"
          :key="i"
          :prop="item.prop"
          :label="item.label"
          align="center"
          show-overflow-tooltip
        >
          <template slot="scope" v-if="item.th_name !== '件数'">
            <span>{{ item.label }}</span>
          </template>
          <template slot="scope" v-if="item.th_name !== '重量'">
            <span>{{ item.label }}</span>
          </template>
          <!-- 插入输入框 -->
          <template slot-scope="scope">
            <template v-if="item.slot">
              <template v-if="item.prop == 'orderMaterialNumber'">
                <el-input
                  v-model.number="scope.row.orderMaterialNumber"
                ></el-input>
              </template>
              <template v-if="item.prop == 'orderMaterialWeight'">
                <el-input
                  v-model.number="scope.row.orderMaterialWeight"
                ></el-input>
              </template>
            </template>
            <template v-else>
              <span>{{ scope.row[item.prop] }}</span>
            </template>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="onClick(scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态窗口 -->
    <el-drawer :visible.sync="drawer" :direction="direction" size="30%">
      <el-input
        placeholder="请输入内容"
        v-model="input"
        style="margin-top: 0.625rem; margin-left: 1.25rem"
        clearable
      ></el-input>
      <el-button
        type="primary"
        class="btn"
        @click="onclick(a)"
        style="margin-bottom: 0.9375rem"
      >
        <i class="el-icon-search"></i>查询
      </el-button>
      <div v-show="a == 1">
        <dilTable
          v-bind.sync="frist"
          @radio-change="currentRadioChange1"
        ></dilTable>
      </div>
      <div v-show="a == 2">
        <dilTable
          v-bind.sync="secend"
          @radio-change="currentRadioChange2"
        ></dilTable>
      </div>
      <div v-show="a == 3">
        <dilTable
          v-bind.sync="third"
          @selection-change="currentRadioChange3"
        ></dilTable>
      </div>
      <div v-show="a == 4">
        <dilTable
          v-bind.sync="unloadPoint"
          @radio-change="currentRadioChange4"
        ></dilTable>
      </div>
      <div v-show="a == 5">
        <dilTable
          v-bind.sync="line"
          @radio-change="currentRadioChange5"
        ></dilTable>
      </div>
    </el-drawer>
    <div class="button_box">
      <el-button @click="onClickCancel">返回</el-button>
      <el-button type="primary" @click="onClickConfirm">确认</el-button>
    </div>
  </div>
</template>
<script>
import PageTitle from "@/components/Page/Title";
export default {
  components: { PageTitle },
  data() {
    return {
      // 线路名称
      lineName: "",
      //线路id
      lineId: null,
      // 件数与重量的开关
      value: false,
      //船名
      remark: null,
      // 车辆表格表头
      tableTop: [
        {
          prop: "capacityNumber",
          label: "车牌号",
          width: "140",
        },
        {
          prop: "carrierName",
          label: "承运商",
          width: "140",
        },
        {
          prop: "orderMaterialNumber",
          label: "件数",
          slot: true,
        },
      ],
      //车辆表格数据
      selectionList: [],
      // 表单
      form1: {},
      //采购订单id
      purchaseOrderId: "",
      //物资名称
      materialName: "",
      //发货单位名称
      supplierName: "",
      //发货单位id
      supplierId: null,
      //物资id
      materialId: null,
      //是否需要打开模态窗口
      drawer: false,
      //卸货点id
      unloadPointId: "",
      //卸货点名称
      unloadPointName: "",
      a: 1,
      direction: "rtl",
      input: "",
      frist: {
        requestUrl: "/api/v1/uc/queryAPOMaterialByLike?apiId=244&startNum=050",
        selectionType: "radio",
        mapList1: [],
      },
      secend: {
        requestUrl:
          "/api/v1/uc/getSupplierMesByMaterialId?apiId=247&materialId=",
        selectionType: "radio",
        mapList2: [],
      },
      third: {
        requestUrl: "/api/v1/uc/getAllCapacityByCarrierLike?apiId=248",
        selectionType: "select",
        mapList3: [],
      },
      unloadPoint: {
        requestUrl: "/api/v1/uc/getUnloadingMesByLike?apiId=374",
        selectionType: "radio",
      },
      line: {
        requestUrl: "/api/v1/rms/getAllLineDesk?apiId=249&lineType=3",
        selectionType: "radio",
      },
    };
  },
  watch: {
    value() {
      if (this.value) {
        this.tableTop.splice(this.tableTop.length - 1, 1);
        this.tableTop.push({
          prop: "orderMaterialWeight",
          label: "重量",
          slot: true,
        });
      } else {
        this.tableTop.splice(this.tableTop.length - 1, 1);
        this.tableTop.push({
          prop: "orderMaterialNumber",
          label: "件数",
          slot: true,
        });
      }
    },
  },
  methods: {
    onClick(index) {
      this.selectionList.splice(index, 1);
    },
    onBlur() {
      this.getPurchaseOrderNo();
    },
    getPurchaseOrderNo() {
      let map = {
        materialId: this.materialId,
        supplierId: this.supplierId,
      };
      if (this.remark) {
        map.remark = this.remark;
      }
      console.log(this.map);
      if (this.materialId && this.supplierId) {
        this.$axios.post("/api/v1/oms/selectNewOrderId", map).then((res) => {
          console.log(res);
          if (res.data.code == "200") {
            if (res.data.data == null) {
              this.$message({
                message: "没有匹配的采购订单号!",
                type: "warning",
              });
              this.form1 = [];
            } else {
              this.remark = res.data.data.remark;
              this.purchaseOrderId = res.data.data.purchaseOrderId;
              this.form1 = { purchaseOrderNo: res.data.data.purchaseOrderNo };
            }
          } else {
            this.$message.error("请求失败");
          }
        });
      }
    },
    onclick(a) {
      if (a == 1) {
        this.frist.requestUrl =
          "/api/v1/uc/queryAPOMaterialByLike?apiId=244&startNum=050&index=" +
          this.input;
      } else if (a == 2) {
        this.secend.requestUrl =
          "/api/v1/uc/getSupplierMesByMaterialId?apiId=247&materialId=" +
          this.materialId +
          "&index=" +
          this.input;
      } else if (a == 3) {
        this.third.requestUrl =
          "/api/v1/uc/getAllCapacityByCarrierLike?apiId=248&index=" +
          this.input;
      } else if (a == 4) {
        this.third.requestUrl =
          "/api/v1/uc/getUnloadingMesByLike?apiId=347&index=" + this.input;
      } else if (a == 5) {
        this.option.requestUrl =
          "/api/v1/rms/getAllLineDesk?apiId=249&lineType=3&con=" + this.input;
      }
    },
    ondrawer(num) {
      this.drawer = true;
      this.a = num;
      if (num == 2) {
        this.secend.requestUrl =
          "/api/v1/uc/getSupplierMesByMaterialId?apiId=247&materialId=" +
          this.materialId;
      }
    },
    // 返回
    onClickCancel() {
      this.$router.push("/importedIngredients/transportReserveFu");
    },
    currentRadioChange1(selection) {
      this.input = null;
      this.materialName = selection.materialName;
      this.materialId = selection.materialId;
      this.getPurchaseOrderNo();
    },
    currentRadioChange2(selection) {
      this.supplierName = selection.supplierName;
      this.supplierId = selection.supplierId;
      this.getPurchaseOrderNo();
    },
    currentRadioChange4(selection) {
      this.unloadPointId = selection.warehouseId;
      this.unloadPointName = selection.warehouseName;
    },
    currentRadioChange5(selection) {
      this.lineName = selection.lineName;
      this.lineId = selection.lineId;
    },
    currentRadioChange3(selection) {
      this.selectionList = [];
      this.selectionList = selection;
    },
    // 确认
    onClickConfirm() {
      var state = 0;
      if (
        this.materialId &&
        this.supplierId &&
        this.form1.purchaseOrderId &&
        this.unloadPointId &&
        sjTime(this.form1.orderEntryTime) &&
        this.lineId
      ) {
        state = 1;
      } else {
        if (!this.materialId) {
          this.$message({
            message: "请选择物资!",
            type: "warning",
          });
        } else if (!this.supplierId) {
          this.$message({
            message: "请选择发货单位!",
            type: "warning",
          });
        } else if (!this.purchaseOrderId) {
          this.$message({
            message: "没有匹配的采购订单号!",
            type: "warning",
          });
        } else if (!this.unloadPointId) {
          this.$message({
            message: "请选择卸货地点!",
            type: "warning",
          });
        } else if (!this.lineId) {
          this.$message({
            message: "请选择运输路线!",
            type: "warning",
          });
        } else if (!sjTime(this.form1.orderEntryTime)) {
          this.$message({
            message: "请选择预估进厂时间!",
            type: "warning",
          });
        }
      }
      if (this.selectionList.length == 0) {
        this.$message({
          message: "请选择车辆!",
          type: "warning",
        });
        state = 0;
      } else {
        this.selectionList.forEach((e) => {
          if (e.orderMaterialNumber || e.orderMaterialWeight) {
            if (
              isNumber(e.orderMaterialNumber) ||
              isNumber(e.orderMaterialWeight)
            ) {
              if (
                isIntegerNumber(e.orderMaterialNumber) ||
                isIntegerNumber(e.orderMaterialWeight)
              ) {
                state = 1;
              } else {
                this.$message({
                  message: "件数(重量)只能是整数!",
                  type: "warning",
                });
                state = 0;
              }
            } else {
              this.$message({
                message: "件数(重量)只能是数字!",
                type: "warning",
              });
              state = 0;
            }
          } else {
            this.$message({
              message: "件数(重量)不能为空!",
              type: "warning",
            });
            state = 0;
          }
        });
      }

      if (state == 1) {
        let map = {
          materialId: this.materialId,
          supplierId: this.supplierId,
          purchaseOrderId: this.purchaseOrderId,
          unloadPointId: this.unloadPointId,
          orderEntryTime: sjTime(this.form1.orderEntryTime),
          driverCapacityIdList: this.selectionList,
          orderType: 5,
          lineId: this.lineId,
        };
        console.log(map, map);
        this.$axios.post("/api/v1/oms/addPurOrder", map).then((res) => {
          if (res.data.code == "200") {
            this.$message({
              message: "添加成功!",
              type: "success",
            });
            this.$router.push("/importedIngredients/transportReserveFu");
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" >
.contractDetails {
  width: 100%;
}
.from {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 0.3125rem;
  margin-bottom: 1.25rem;
}
.remark {
  .text {
    display: inline-block;
    width: 5.625rem;
    text-align: right;
  }
}
.lineId {
  .text {
    display: inline-block;
    width: 10rem;
    text-align: right;
  }
}
.material {
  .text {
    display: inline-block;
    width: 10rem;
    text-align: right;
  }
}
.switch {
  width: 100%;
  height: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 6.875rem;
}
.forwardingUnit {
  .text {
    display: inline-block;
    width: 10rem;
    text-align: right;
  }
}
.truckList1 {
  width: 100%;
  .truckList {
    width: 96%;
    .text {
      display: inline-block;
      width: 9.375rem;
      text-align: right;
    }
    .truckBtn {
      width: 17.5rem;
    }
  }
}
.button_box {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 6.25rem;
}
</style>