//运输预约展现
<template>
  <!-- 零星物资运输预约 -->
  <div class="homeworkPath">
    <div class="top">
      <span class="text" v-if="activeName != 'first'">订单时间：</span>
      <el-date-picker
        v-model="startTime"
        type="datetime"
        placeholder="选择日期"
        style="width:200px"
        v-if="activeName != 'first'"
      >
      </el-date-picker>
      <span class="text" v-if="activeName != 'first'">至</span>
      <el-date-picker
        v-model="endTime"
        type="datetime"
        placeholder="选择日期"
        style="width:200px"
        v-if="activeName != 'first'"
      >
      </el-date-picker>
      <el-input
        placeholder="请输入内容"
        v-model="input"
        clearable
        style="width:200px"
      >
      </el-input>
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>
      </el-button>
      <el-button type="primary" @click="insertClick">
        <i class="el-icon-plus"></i>新增
      </el-button>
      <el-button
        type="primary"
        @click="sendClick()"
        v-if="this.activeName == 'first'"
      >
        <i class="el-icon-bottom"></i>下发
      </el-button>
    </div>
    <div class="table">
      <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" label="操作" width="120">
              <template slot-scope="scope">
                <el-button @click="deleteClick(scope)" type="text" size="small">
                  删除
                </el-button>
                <el-button
                  @click="getMatrialDetails1(scope.row)"
                  type="text"
                  size="small"
                >
                  物资详情
                </el-button>
              </template>
            </el-table-column>
            //物资详情抽屉
            <el-table-column type="expand" width="1">
              <template slot-scope="props">
                <el-form
                  label-position="center"
                  inline
                  class="demo-table-expand"
                >
                  <div v-if="false">{{ props }}</div>
                  <div>
                    <el-table :data="tableData" border>
                      <el-table-column
                        v-for="(item, i) in tableHead"
                        :key="i"
                        :prop="item.prop"
                        :label="item.label"
                        :width="item.width"
                      ></el-table-column>
                    </el-table>
                  </div>
                </el-form>
              </template>
            </el-table-column>
          </dilTable>
        </el-tab-pane>
        <!-- 已下发 -->
        <el-tab-pane label="已下发" name="second">
          <dilTable v-bind.sync="option2" ref="table1">
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button
                  @click="getMatrialDetails2(scope.row)"
                  type="text"
                  size="small"
                >
                  物资详情
                </el-button>
                <el-button
                  @click="stopSpoOrder(scope)"
                  type="text"
                  size="small"
                >
                  停止
                </el-button>
              </template>
            </el-table-column>
            //物资详情抽屉
            <el-table-column type="expand" width="1">
              <template slot-scope="props">
                <el-form
                  label-position="center"
                  inline
                  class="demo-table-expand"
                >
                  <div v-if="false">{{ props }}</div>
                  <div>
                    <el-table :data="tableData1" border>
                      <el-table-column
                        v-for="(item, i) in tableHead"
                        :key="i"
                        :prop="item.prop"
                        :label="item.label"
                        :width="item.width"
                      ></el-table-column>
                    </el-table>
                  </div>
                </el-form>
              </template>
            </el-table-column>
          </dilTable>
        </el-tab-pane>
        <!-- 已接单 -->
        <el-tab-pane label="已接单" name="third">
          <dilTable v-bind.sync="option3" ref="table2">
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button
                  @click="getMatrialDetails3(scope.row)"
                  type="text"
                  size="small"
                >
                  物资详情
                </el-button>
              </template>
            </el-table-column>
            //物资详情抽屉
            <el-table-column type="expand" width="1">
              <template slot-scope="props">
                <el-form
                  label-position="center"
                  inline
                  class="demo-table-expand"
                >
                  <div v-if="false">{{ props }}</div>
                  <div>
                    <el-table :data="tableData1" border>
                      <el-table-column
                        v-for="(item, i) in tableHead"
                        :key="i"
                        :prop="item.prop"
                        :label="item.label"
                        :width="item.width"
                      ></el-table-column>
                    </el-table>
                  </div>
                </el-form>
              </template>
            </el-table-column>
          </dilTable>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getCookie } from "@/utils/util.js";
import { sjTime } from "@/utils/sharedJsFile";
export default {
  name: "inplantTMS",
  data() {
    return {
      startTime: null,
      endTime: null,
      input: null,
      Time: "",
      activeName: "first",
      option1: {
        // 表格请求数据的地址
        requestUrl: "",
        // 控制显示多选列
        selectionType: "select"
      },
      option2: {
        // 表格请求数据的地址
        requestUrl: ""
      },
      option3: {
        // 表格请求数据的地址
        requestUrl: ""
      },
      selection: [],
      maplist: [],
      //记录旧的row对象
      oldRow: "",
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态
      oldRowCount: 1,
      //记录旧的row对象
      oldRow1: "",
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态
      oldRowCount1: 1,
      oldRow2: "",
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态
      oldRowCount2: 1,
      tableHead: [
        {
          prop: "materialName",
          label: "物资名称",
          width: 150
        },
        {
          prop: "specificationModel",
          label: "规格型号",
          width: 150
        },
        {
          prop: "materialNumber",
          label: "物资件数",
          width: 100
        },
        {
          prop: "materialWeight",
          label: "物资重量",
          width: 100
        }
      ],
      tableData: [],
      tableData1: [],
      tableData2: []
    };
  },
  created() {
    if (
      getCookie("orgCode") == "dagangadmin" ||
      getCookie("orgCode") == "zidonghuabu"
    ) {
      this.option1.requestUrl =
        "/api/v1/ams/getSporadicOrdersList?apiId=439&issueStatus=0";
      this.option2.requestUrl =
        "/api/v1/ams/getSporadicOrdersList?apiId=439&issueStatus=1";
      this.option3.requestUrl =
        "/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&type1=1";
    } else {
      this.option1.requestUrl =
        "/api/v1/ams/getSporadicOrdersList?apiId=439&issueStatus=0&userId=" +
        getCookie("orgCode");
      this.option2.requestUrl =
        "/api/v1/ams/getSporadicOrdersList?apiId=439&issueStatus=1&userId=" +
        getCookie("orgCode");
      this.option3.requestUrl =
        "/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&type1=1&userId=" +
        getCookie("orgCode");
    }
  },
  methods: {
    getRequestUrl() {
      if (
        getCookie("orgCode") == "dagangadmin" ||
        getCookie("orgCode") == "zidonghuabu"
      ) {
        this.option1.requestUrl =
          "/api/v1/ams/getSporadicOrdersList?apiId=439&issueStatus=0&test=" +
          new Date();
        this.option2.requestUrl =
          "/api/v1/ams/getSporadicOrdersList?apiId=439&issueStatus=1&test=" +
          new Date();
        this.option3.requestUrl =
          "/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&type1=1&test=" +
          new Date();
      } else {
        this.option1.requestUrl =
          "/api/v1/ams/getSporadicOrdersList?apiId=439&issueStatus=0&userId=" +
          getCookie("orgCode") +
          "&test=" +
          new Date();
        this.option2.requestUrl =
          "/api/v1/ams/getSporadicOrdersList?apiId=439&issueStatus=1&userId=" +
          getCookie("orgCode") +
          "&test=" +
          new Date();
        this.option3.requestUrl =
          "/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&type1=1&userId=" +
          getCookie("orgCode") +
          "&test=" +
          new Date();
      }
    },
    stopSpoOrder(scope) {
      this.$confirm("是否停止", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          this.axios
            .post("/api/v1/ams/closeSporadicOrder", {
              sporadicOrderId: scope.row.sporadicOrderId
            })
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "成功停止!"
                });
                this.getRequestUrl();
              } else {
                this.$message({
                  message: "停止失败",
                  type: "warning"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "停止操作已取消!"
          });
        });
    },
    //获取选中的订单
    selectionChange(selection) {
      this.maplist = selection;
    },
    onclick() {
      let startTime = null;
      let endTime = null;
      if (this.startTime && this.endTime) {
        startTime = sjTime(this.startTime);
        endTime = sjTime(this.endTime);
      }
      if (
        getCookie("orgCode") == "dagangadmin" ||
        getCookie("orgCode") == "zidonghuabu"
      ) {
        if (this.activeName == "second") {
          this.option2.requestUrl =
            "/api/v1/ams/getSporadicOrdersList?apiId=439&issueStatus=1&orderType=12&con=" +
            this.input +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&i=" +
            new Date();
        } else {
          this.option3.requestUrl =
            "/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&type1=1&con=" +
            this.input +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&i=" +
            new Date();
        }
      } else {
        if (this.activeName == "second") {
          this.option2.requestUrl =
            "/api/v1/ams/getSporadicOrdersList?apiId=439&issueStatus=1&orderType=12&userId=" +
            getCookie("orgCode") +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&con=" +
            this.input +
            "&i=" +
            new Date();
        } else {
          this.option3.requestUrl =
            "/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&type1=1&userId=" +
            getCookie("orgCode") +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&con=" +
            this.input +
            "&i=" +
            new Date();
        }
      }
    },
    handleClick(tab, event) {
      this.getRequestUrl();
    },
    // 下发
    sendClick() {
      if (this.maplist.length == 0) {
        this.$message.warning("未选择零星订单");
        return;
      }
      this.axios
        .post("/api/v1/ams/issueSporadicOrders", this.maplist)
        .then(res => {
          if (res.data.code == "200") {
            this.getRequestUrl();
            // this.$router.go(0)
            this.$message({
              type: "success",
              message: "下发成功!"
            });
            this.maplist = [];
          }
        });
    },
    // 删除
    deleteClick(scope) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          this.axios
            .post(
              "/api/v1/ams/deleteSporadicOrders?sporadicOrderId=" +
                scope.row.sporadicOrderId
            )
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "成功删除!"
                });
                this.getRequestUrl();
              } else {
                this.$message({
                  message: "删除失败",
                  type: "warning"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "删除操作已取消!"
          });
        });
    },
    //新增
    insertClick() {
      this.$router.push("addOldSporadicAppoint");
    },
    //获取物资详情--未下发
    getMatrialDetails1(row) {
      console.log("wuzixq");
      // 记录重复点击次数
      if (this.oldRow === row) {
        this.oldRowCount += 1;
      }
      // 切换当前详情表
      this.$refs.table.toggleRowExpansion(row);
      // 打开前关闭上一个详情表
      if (this.oldRow != "") {
        if (this.oldRow != row) {
          if (this.oldRowCount % 2 === 1) {
            this.$refs.table.toggleRowExpansion(this.oldRow);
          } else {
            this.oldRowCount = 1;
          }
        } else {
          this.oldRow = null;
          return;
        }
      }
      // 重置上一个点击对象
      this.oldRow = row;
      this.axios
        .post("/api/v1/ams/getSporadic?sporadicId=" + row.sporadicOrderId)
        .then(res => {
          console.log(res.data.data);
          this.tableData = res.data.data;
        });
    },
    //获取物资详情--已下发

    getMatrialDetails2(row) {
      console.log("wuzixq");
      // 记录重复点击次数
      if (this.oldRow1 === row) {
        this.oldRowCount1 += 1;
      }
      // 切换当前详情表
      this.$refs.table1.toggleRowExpansion(row);
      // 打开前关闭上一个详情表
      if (this.oldRow1 != "") {
        if (this.oldRow1 != row) {
          if (this.oldRowCount1 % 2 === 1) {
            this.$refs.table1.toggleRowExpansion(this.oldRow1);
          } else {
            this.oldRowCount1 = 1;
          }
        } else {
          this.oldRow1 = null;
          return;
        }
      }
      // 重置上一个点击对象
      this.oldRow1 = row;
      this.axios
        .post("/api/v1/ams/getSporadic?sporadicId=" + row.sporadicOrderId)
        .then(res => {
          console.log(res.data.data);
          this.tableData1 = res.data.data;
        });
    },
    getMatrialDetails3(row) {
      // 记录重复点击次数
      if (this.oldRow2 === row) {
        this.oldRowCount2 += 1;
      }
      // 切换当前详情表
      this.$refs.table2.toggleRowExpansion(row);
      // 打开前关闭上一个详情表
      if (this.oldRow2 != "") {
        if (this.oldRow2 != row) {
          if (this.oldRowCount2 % 2 === 1) {
            this.$refs.table2.toggleRowExpansion(this.oldRow2);
          } else {
            this.oldRowCount2 = 1;
          }
        } else {
          this.oldRow2 = null;
          return;
        }
      }
      // 重置上一个点击对象
      this.oldRow2 = row;
      this.axios
        .post("/api/v1/ams/getSporadic?sporadicId=" + row.sporadicOrderId)
        .then(res => {
          console.log(res.data.data);
          this.tableData1 = res.data.data;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.homeworkPath {
  .top {
    margin-left: 20px;
    margin-top: 20px;
  }
  .table {
    margin-left: 20px;
    margin-top: 20px;
  }
}
</style>
