<template>
  <!-- 燃料运输预约 -->
  <div class="homeworkPath">
    <div class="top">
      <el-input
        class="input"
        placeholder="请输入内容"
        v-model="input"
        clearable
      >
      </el-input>
      <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-button
        type="primary"
        class="btn"
        @click="issue"
        v-if="activeName == 'first'"
      >
        <i class="el-icon-plus"></i>下发
      </el-button>
    </div>
    <el-tabs v-model="activeName">
      <!-- 未下发 -->
      <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"> </dilTable>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getCookie } from "@/utils/util.js";
export default {
  name: "inplantTMS",
  data() {
    return {
      input: "",
      Time: "",
      activeName: "first",
      option1: {
        // 表格请求数据的地址
        requestUrl: "",
        selectionType: "select",
        mapList: [],
      },
      option2: {
        // 表格请求数据的地址
        requestUrl: "",
      },
    };
  },
  created() {
    //判断是否是承运商
    if (getCookie("orgCode") == "chengyunshang") {
      this.option1.requestUrl =
        "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&fuelOrder=1&carrierSSOId=" +
        getCookie("userId");
      this.option2.requestUrl =
        "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&fuelOrder=1&carrierSSOId=" +
        getCookie("userId");
    } else {
      this.option1.requestUrl =
        "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&fuelOrder=1&carrierSSOId=" +
        null;
      this.option2.requestUrl =
        "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&fuelOrder=1&carrierSSOId=" +
        null;
    }
  },
  methods: {
    onclick() {
      if (this.activeName == "first") {
        //判断是否是承运商
        if (getCookie("orgCode") == "chengyunshang") {
          this.option1.requestUrl =
            "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&fuelOrder=1&carrierSSOId=" +
            getCookie("userId") +
            "&con=" +
            this.input;
        } else {
          this.option1.requestUrl =
            "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&fuelOrder=1&carrierSSOId=" +
            null +
            "&con=" +
            this.input;
        }
      } else {
        //判断是否是承运商
        if (getCookie("orgCode") == "chengyunshang") {
          this.option1.requestUrl =
            "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&fuelOrder=1&carrierSSOId=" +
            getCookie("userId") +
            "&con=" +
            this.input;
        } else {
          this.option1.requestUrl =
            "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&fuelOrder=1&carrierSSOId=" +
            null +
            "&con=" +
            this.input;
        }
      }
    },
    selectionChange(selection) {
      this.mapList = selection;
    },
    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&fuelOrder=1&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.option1.requestUrl =
              "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&fuelOrder=1&i=" +
              new Date();
          });
        })
        .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.$router.go(0);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消删除!",
          });
        });
    },
    //新增
    insertClick() {
      this.$router.push("/transportReserveRanAdd");
    },
    //修改
    updateClick(orderId) {
      this.$router.push("/transportReserveRanEdit/" + orderId);
    },
  },
};
</script>
<style lang='scss' scoped>
.homeworkPath {
  .top {
    padding: 40px;
    .input {
      width: 250px;
      margin-right: 20px;
    }
    .btn {
      margin-right: 10px;
    }
  }
}
</style>