<template>
  <!-- 辅料运输预约 -->
  <div class="homeworkPath">
    <div class="top">
      <el-input
        class="input"
        placeholder="请输入内容"
        v-model="input"
        clearable
      >
      </el-input>
      <!-- 时间查询框 -->
      <span class="text" v-if="activeName == 'second'">下发时间：</span>
      <el-date-picker
        v-if="activeName == 'second'"
        v-model="startTime"
        type="datetime"
        placeholder="选择日期"
      >
      </el-date-picker>
      <span class="text" v-if="activeName == 'second'">至</span>
      <el-date-picker
        v-if="activeName == 'second'"
        v-model="endTime"
        type="datetime"
        placeholder="选择日期"
      >
      </el-date-picker>
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>查询
      </el-button>
      <el-button type="primary" class="btn" @click="insertClick">
        <i class="el-icon-plus"></i>新增
      </el-button>
      <el-input
        class="input"
        placeholder="请从表格复制采购订单号"
        v-model="inputPurOrder"
        clearable
        v-if="activeName == 'second'"
      >
      </el-input>
      <el-button
        type="primary"
        class="btn"
        @click="closePurOrder"
        v-if="activeName == 'second'"
      >
        关闭一车多趟
      </el-button>
      <el-button
        type="primary"
        class="btn"
        @click="issue"
        v-if="activeName == 'first'"
      >
        <i class="el-icon-plus"></i>下发
      </el-button>
    </div>
    <el-dialog title="提醒" :visible.sync="dialogVisible" width="30%">
      <div v-if="this.isMoreTrips != 1" style="color: red">
        <span>该订单不是一车多趟的订单</span>
      </div>
      <div>
        <span>采购订单号:</span>
        <span>{{ inputPurOrder }}</span>
      </div>
      <div>
        <span>发货单位:</span>
        <span>{{ supplierName }}</span>
      </div>
      <div>
        <span>物资名称:</span>
        <span>{{ materialName }}</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="closePurOrder1"
          v-if="this.isMoreTrips == 1"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-tabs v-model="activeName" tab-click="handleClick">
      <!-- 未下发 -->
      <el-tab-pane label="未下发" name="first">
        <dilTable
          v-bind.sync="option1"
          ref="table"
          @selection-change="selectionChange"
        >
          <el-table-column
            fixed="right"
            align="center"
            label="操作"
            width="120"
          >
            <template slot-scope="scope">
              <el-button
                @click="sendClick(scope.row.orderId)"
                type="text"
                size="small"
              >
                下发
              </el-button>
              <el-button
                @click="updateClick(scope.row.orderId)"
                type="text"
                size="small"
              >
                修改
              </el-button>
              <el-button
                @click="
                  deleteClick(scope.row.orderId, scope.row.capacityNumber)
                "
                type="text"
                size="small"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </dilTable>
      </el-tab-pane>
      <!-- 已下发 -->
      <el-tab-pane label="已下发" name="second">
        <dilTable v-bind.sync="option2" @func="func"> </dilTable>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getCookie } from "@/utils/util.js";
import { sjTime } from "@/utils/sharedJsFile";
export default {
  name: "inplantTMS",
  data() {
    return {
      input: "",
      Time: "",
      startTime: null,
      endTime: null,
      activeName: "first",
      dialogVisible: false,
      materialName: null,
      supplierName: null,
      inputPurOrder: null,
      option1: {
        // 表格请求数据的地址
        requestUrl: "",
        selectionType: "select",
        mapList: [],
      },
      isMoreTrips: null,
      option2: {
        // 表格请求数据的地址
        requestUrl: "",
      },
      purOrderList: [],
    };
  },
  created() {
    //判断是否是承运商
    if (getCookie("orgCode") == "chengyunshang") {
      this.option1.requestUrl =
        "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=" +
        getCookie("userId");
      this.option2.requestUrl =
        "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=" +
        getCookie("userId");
    } else {
      this.option1.requestUrl =
        "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=" +
        null;
      this.option2.requestUrl =
        "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=" +
        null;
    }
  },
  methods: {
    handleClick() {
      this.getRequestUrl();
    },
    getRequestUrl() {
      //判断是否是承运商
      if (getCookie("orgCode") == "chengyunshang") {
        this.option1.requestUrl =
          "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=" +
          getCookie("userId") +
          "&i=" +
          new Date();
        this.option2.requestUrl =
          "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=" +
          getCookie("userId") +
          "&i=" +
          new Date();
      } else {
        this.option1.requestUrl =
          "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=" +
          null +
          "&i=" +
          new Date();
        this.option2.requestUrl =
          "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=" +
          null +
          "&i=" +
          new Date();
      }
    },
    onclick() {
      if (this.activeName == "first") {
        //判断是否是承运商
        if (getCookie("orgCode") == "chengyunshang") {
          this.option1.requestUrl =
            "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=" +
            getCookie("userId") +
            "&con=" +
            this.input;
        } else {
          this.option1.requestUrl =
            "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=" +
            null +
            "&con=" +
            this.input;
        }
      } else {
        let startTime = null;
        let endTime = null;
        if (this.startTime) {
          startTime = sjTime(this.startTime);
        }
        if (this.endTime) {
          endTime = sjTime(this.endTime);
        }
        if (startTime && endTime) {
          if (startTime < endTime) {
            //判断是否是承运商
            if (getCookie("orgCode") == "chengyunshang") {
              this.option2.requestUrl =
                "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=" +
                getCookie("userId") +
                "&con=" +
                this.input +
                "&startTime=" +
                startTime +
                "&endTime=" +
                endTime +
                "&i=" +
                new Date();
            } else {
              this.option2.requestUrl =
                "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=" +
                null +
                "&con=" +
                this.input +
                "&startTime=" +
                startTime +
                "&endTime=" +
                endTime +
                "&i=" +
                new Date();
            }
          }
        } else {
          if (getCookie("orgCode") == "chengyunshang") {
            this.option2.requestUrl =
              "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=" +
              getCookie("userId") +
              "&con=" +
              this.input +
              "&i=" +
              new Date();
          } else {
            this.option2.requestUrl =
              "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=" +
              null +
              "&con=" +
              this.input +
              "&i=" +
              new Date();
          }
        }
      }
    },
    selectionChange(selection) {
      this.mapList = selection;
    },
    closePurOrder() {
      if (this.inputPurOrder == null) {
        this.$message.warning("请输入需要关闭一车多趟的采购订单号");
        return;
      }
      this.purOrderList.forEach((e) => {
        if (e.purchaseOrderNo == this.inputPurOrder) {
          this.materialName = e.materialName;
          this.supplierName = e.supplierName;
          this.isMoreTrips = e.isMoreTrips;
        }
      });
      this.dialogVisible = true;
    },
    closePurOrder1() {
      this.axios
        .post(
          "/api/v1/bp/closePurOrderMoreTrips?purchaseOrderNo=" +
            this.inputPurOrder
        )
        .then((res) => {
          console.log(res);
          if (res.data.code == "200") {
            this.$message.success("关闭成功");
          } else {
            this.$message.error(res.data.data);
          }
        });
      this.dialogVisible = false;
    },
    func(res) {
      this.purOrderList = res.list;
      console.log(res.list);
    },
    issue() {
      let mapvalue = {
        mapList: this.mapList,
      };
      this.axios.post("/api/v1/oms/dispatchOrder", mapvalue).then(() => {
        this.$message({
          type: "success",
          message: "下发成功!",
        });
        this.option1.requestUrl =
          "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&i=" +
          new Date();
      });
    },
    // 下发
    sendClick(orderId) {
      let mapvalue = {
        mapList: [{ orderId: orderId }],
      };
      this.$confirm("是否下发", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.axios.post("/api/v1/oms/dispatchOrder", mapvalue).then(() => {
            this.$message({
              type: "success",
              message: "下发成功!",
            });
            this.getRequestUrl();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消下发!",
          });
        });
    },
    // 删除
    deleteClick(orderId, capacityNumber) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.axios
            .post("/api/v1/oms/deleteOrder", {
              orderId: orderId,
              capacityNumber: capacityNumber,
            })
            .then(() => {
              this.getRequestUrl();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消删除!",
          });
        });
    },
    //新增
    insertClick() {
      this.$router.push("/transportReserveFuAddIngredients/");
    },
    //修改
    updateClick(orderId) {
      this.$router.push("/transportReserveFuEdit/" + orderId);
    },
  },
};
</script>
<style lang="scss" scoped>
.homeworkPath {
  .top {
    padding: 40px;
    .input {
      width: 250px;
      margin-right: 20px;
    }
    .btn {
      margin-right: 10px;
    }
  }
}
</style>
