<template>
  <!-- 采购辅料运输预约 -->
  <div class="homeworkPath">
    <div class="top">
      <el-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" @click="insertClick" >
        <i class="el-icon-plus"></i>新增
      </el-button>
       <el-button type="primary" @click="sendClick(0)" >
        <i class="el-icon-bottom"></i>下发
      </el-button>
    </div>
    <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(1,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">
           <el-table-column fixed="right" align="center" label="操作" width="120">
            <template slot-scope="scope">
              <el-button @click="CloseClick(scope.row.orderId)"  type="text" size="small">
               关闭
              </el-button>
            </template>
          </el-table-column>
        </dilTable>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
  name: "inplantTMS",
  data() {
    return {
      input: "",
      Time: "",
      activeName: "first",
      option1: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5",
        // 控制显示多选列
        selectionType: "select",
      },
       option2: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=4&orderType=5",
      },
      selection:[],
    };
  },
  methods: {
    //获取选中的订单
    selectionChange(selection){
      this.selection = [];
      selection.forEach(e => {
       this.selection.push({orderId:e.orderId});
      });
    },
    onclick() {
      if(this.activeName == "first"){
        this.option1.requestUrl = "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&con=" + this.input;
      }else{
        this.option2.requestUrl = "/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=4&orderType=5&con=" + this.input;
      }
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
     // 下发
    CloseClick(orderId) {
      this.$confirm("是否关闭", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
         this.$axios
            .post(
              "/api/v1/oms/CloseOrder",{
                orderId: orderId
              }
            )
            .then(() => {
              this.$router.go(0);
            })
        .then(() => {
          this.$message({
            type: "success",
            message: "关闭成功!",
          });
          // console.log(this.arr[0].text_prop);
   
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消关闭!",
          });
        });
    },


    //关闭

    sendClick(index,orderId) {
      this.$confirm("是否下发", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          let orderList = [];
          if(index == 0){
            if(this.selection.length > 0){
              orderList=this.selection;
            }else{
            this.$message({
              type: "warning",
              message: "请选择要下发的订单!",
            });
            }
          }else if(index == 1){
            orderList.push({orderId:orderId})
          }
          this.$axios
            .post(
              "/api/v1/oms/dispatchOrder",{
                mapList:orderList
              }
            )
            .then(() => {
              this.option1.requestUrl = "/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&i=0"
            });
            this.$message({
              type: "success",
              message: "下发成功!",
            });
        })
        .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("/importedIngredients/transportReserveFuAdd/");
    },
    //修改
    updateClick(orderId) {
      this.$router.push("/importedIngredients/transportReserveFuEdit/" + orderId);
    },

  },
};
</script>
<style lang='scss' scoped>
.homeworkPath {
   .top {
    padding: 1.25rem 1.875rem;
  }
}
</style>