<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="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="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="scope">
            <template v-if="item.slot">
              <template v-if="item.prop == 'orderMaterialNumber'">
                <el-input v-model.number="scope.row.orderMaterialNumber"></el-input>
              </template>
            </template>
            <template v-else>
              <span>{{ scope.row[item.prop] }}</span>
            </template>
          </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: 10px; margin-left: 20px"
        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 == 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>
    </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 {
      // 车辆表格表头
      tableTop: [
        {
          prop: "capacityNumber",
          label: "车牌号",
          width: "140",
        },
        {
          prop: "carrierName",
          label: "承运商",
          width: "140",
        },
        {
          prop: "driverName",
          label: "司机",
          width: "140",
        },
        {
          prop: "orderMaterialNumber",
          label: "件数",
          slot: true,
        },
      ],
      //车辆表格数据
      selectionList: [],
      // 表单
      form1: {},
      purchaseOrderId: "",
      materialName: "",
      supplierName: "",
      supplierId: null,
      materialId: null,
      drawer: false,
      a: 1,
      direction: "rtl",
      input: "",
      frist: {
        requestUrl: "/api/v1/uc/queryMaterialByLike?apiId=244&startNum=010",
        selectionType: "radio",
        mapList1: [],
      },
      secend: {
        requestUrl: "/api/v1/uc/querySupplierByLike?apiId=247",
        selectionType: "radio",
        mapList2: [],
      },
      third: {
        requestUrl: "/api/v1/uc/getAllCapacityByCarrierLike?apiId=248",
        selectionType: "select",
        mapList3: [],
      },
    };
  },
  methods: {
    getPurchaseOrderNo() {
      console.log(this.materialId, "materialId");
      console.log(this.supplierId, "supplierId");
      if (this.materialId && this.supplierId) {
        this.axios
          .post("/api/v1/oms/selectNewOrderId", {
            materialId: this.materialId,
            supplierId: this.supplierId,
          })
          .then((res) => {
            console.log(res);
            if (res.data.code == "200") {
              if (res.data.data == null) {
                this.$message({
                  message: "没有匹配的采购订单号!",
                  type: "warning",
                });
              } else {
                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/queryMaterialByLike?apiId=244&startNum=010&index=" + this.input;
      } else if (a == 2) {
        this.secend.requestUrl =
          "/api/v1/uc/querySupplierByLike?apiId=247&index=" + this.input;
      } else {
        this.third.requestUrl =
          "/api/v1/uc/getAllCapacityByCarrierLike?apiId=248&index=" +
          this.input;
      }
    },
    ondrawer(num) {
      this.drawer = true;
      this.a = num;
    },
    // 返回
    onClickCancel() {
      this.$router.go(-1);
    },
    currentRadioChange1(selection) {
      this.materialName = selection.materialName;
      this.materialId = selection.materialId;
      this.getPurchaseOrderNo();
    },
    currentRadioChange2(selection) {
      this.supplierName = selection.supplierName;
      this.supplierId = selection.supplierId;
      this.getPurchaseOrderNo();
    },
    currentRadioChange3(selection) {
      console.log(selection,"selection");
      this.selectionList = [];
      this.selectionList = selection;
    },
    // 确认
    onClickConfirm() {
      var state = 0;
      if (
        this.materialId &&
        this.supplierId &&
        this.form1.purchaseOrderId &&
        this.form1.unloadPointId &&
        sjTime(this.form1.orderEntryTime)
      ) {
        state = 1;
      } else {
        if (!this.materialId) {
          this.$message({
            message: "请选择物资!",
            type: "warning",
          });
          if (!this.supplierId) {
            this.$message({
              message: "请选择发货单位!",
              type: "warning",
            });
            if (!this.form1.purchaseOrderId) {
              this.$message({
                message: "没有匹配的采购订单号!",
                type: "warning",
              });
              if (!this.form1.unloadPointId) {
                this.$message({
                  message: "请选择卸货地点!",
                  type: "warning",
                });
                if (!sjTime(this.form1.orderEntryTime)) {
                  this.$message({
                    message: "请选择预估进厂时间!",
                    type: "warning",
                  });
                }
              }
            }
          }
        }
      }
      this.selectionList.forEach((e) => {
        if (e.orderMaterialNumber) {
          if (isNumber(e.orderMaterialNumber)) {
            if (isIntegerNumber(e.orderMaterialNumber)) {
              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.form1.unloadPointId,
          orderEntryTime: sjTime(this.form1.orderEntryTime),
          driverCapacityIdList: this.selectionList,
          orderType: 5
        };
        // 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.$store.go(-1);
          }
        });
      }
    },
  },
};
</script>

<style lang="scss">
.contractDetails {
  width: 100%;
}
.from {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 5px;
  margin-bottom: 20px;
}
.material {
  .text {
    display: inline-block;
    width: 160px;
    text-align: right;
  }
}
.forwardingUnit {
  .text {
    display: inline-block;
    width: 160px;
    text-align: right;
  }
}
.truckList1 {
  width: 100%;
  .truckList {
    width: 96%;
    .text {
      display: inline-block;
      width: 150px;
      text-align: right;
    }
    .truckBtn {
      width: 280px;
    }
  }
}
.button_box {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100px;
}
</style>