// 定向派单接单
<template>
  <div class="steel_inbound">
    <div class="sache">
      <el-input
        placeholder="请输入内容"
        v-model="inputText"
        clearable>
      </el-input>
      <el-button type="primary" class="btn" @click="onclick1">
        <i class="el-icon-search"></i>查询
      </el-button>
      <el-button v-if="activeName=='first'" type="primary" @click="receive">
        <i></i>批量接收订单
      </el-button>
      <el-button v-if="activeName=='first'" type="primary" @click="refuse">
        <i></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="first" ref="table" @selection-change="currentSelectChange">
             <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button @click="detailclick(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="second" ref="table1">
          <el-table-column fixed="right" label="操作" width="120">
            <template slot-scope="scope">
          <el-button @click="dispatch(scope)  " type="text" size="small">
            派单
          </el-button>
          <el-button @click="detailclick1(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";
export default {
  data(){
    return{
      drawer: false,
      ssoId:null,
			direction: 'rtl',
      activeName: "first",
      inputText:"",
      dispatchId:null,
      input:"",
      //所有选中的车序号订单
      selectTruckOrder: [],
      first:{
        // first请求数据的地址
        requestUrl: "",
        selectionType: "select",
      },
      second:{
        requestUrl:"",
      },
      third:{
        requestUrl:"/api/v1/oms/getCapacityAndDriverList?apiId=246",
        selectionType: "radio",
				mapList: []      
      },
      //记录旧的row对象(未接收)
      oldRow: "",
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态(未接收)
      oldRowCount: 1,
      //记录旧的row对象(已接收)
      oldRow1: "",
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态(已接收)
      oldRowCount1: 1,
      tableHead: [
        {
          prop: "materialName",
          label: "物资名称",
          width: 150,
        },
        {
          prop: "specificationModel",
          label: "规格型号",
          width: 150,
        },
        {
          prop: "materialNumber",
          label: "物资件数",
          width: 100,
        },
        {
          prop: "materialWeight",
          label: "物资重量",
          width: 100,
        },
      ],
      tableData: [],
      tableData1: [],
    }
  },
  created(){
      this.firstGetRequestUrl();
      this.secondGetRequestUrl();
  },
  methods:{
      handleClick(tab, event) {
      console.log(tab, event);
    },
    firstGetRequestUrl(){
    if(getCookie("orgCode") == "chengyunshang"){
      this.first.requestUrl = "/api/v1/ams/getPCDispatchSaleOrderList?apiId=242&dispatchType=0&carrierSsoId="+ getCookie('userId');
    }else{
     this.first.requestUrl = "/api/v1/ams/getPCDispatchSaleOrderList?apiId=242&dispatchType=0&carrierSsoId=" + this.ssoId;
    }

    },
    secondGetRequestUrl(){
    if(getCookie("orgCode") == "chengyunshang"){
      this.second.requestUrl = "/api/v1/ams/selectDispatchSaleOrderList?apiId=233&carrierSsoId="+ getCookie('userId');
    }else{
     this.second.requestUrl = "/api/v1/ams/selectDispatchSaleOrderList?apiId=233&carrierSsoId=" + this.ssoId;
    }

    },
    onclick1(){
       if(this.activeName == "first"){
         this.option1.requestUrl = "/api/v1/ams/getPCDispatchSaleOrderList?apiId=242&con=" + this.input;
      }else if
        (this.activeName == "second"){
        this.option2.requestUrl = "/api/v1/ams/selectDispatchSaleOrderList?apiId=233&carrierId=1&con=" + this.input;
    }
    },
    dispatch(scope){
      this.dispatchId = scope.row.dispatchId
      this.$router.push("/openDistributionSection/" + scope.row.dispatchId);
    },
    // handleBtnClick1(){
		// 		this.drawer=true
		// 	},
    //车序号订单表格
    currentSelectChange(select){
      this.selectTruckOrder = [];
      this.selectTruckOrder = select;
    },
    onclick() {
        this.third.requestUrl =
        "/api/v1/oms/getCapacityAndDriverList?apiId=246&con=" + this.input;
      },
      makeSure(){
        this.$confirm("是否派发","提示",{
          confirmButtonText:"确定",
          cancelButtonText:"取消",
          type:"warning",
          center:true,
        })
        .then(() => {
          //初始化承运商SsoId
          var carrierSsoId = '';
          if(getCookie("orgCode") == "chengyunshang"){
            carrierSsoId =  getCookie('userId');
          }else{
            carrierSsoId = null;
          }
        this.axios.post("/api/v1/oms/addAppTruckOrder?orderType=" + 1 + 
        "&carrierId="+ carrierSsoId +"&dispatchId=" + this.dispatchId 
        + "&capacityNumber=" + this.mapList.capacityNumber)
        .then((res) => {
          if(res.data.code == 200){
            this.$message({
              type:"success",
              message:"派单成功！",
            });
            this.$router.go(0);
          } else {
            this.$message({
                  message: "删除失败",
                  type: "warning",
                });
          }
        })
        }) .catch(() => {
          this.$message({
            type: "info",
            message: "派发操作已取消!",
          });
        });
       
      },

    // -------查看物资详情(未接收)
    detailclick(row) {
      // 记录重复点击次数
      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.getMaterial(row.dispatchId);
    },
    // 根据销售订单物资中间表id查询物资信息
    getMaterial(dispatchId) {
      this.axios
        .post("/api/v1/ams/getDispatchMaterialList?dispatchId="+dispatchId)
        .then((res) => {
          this.tableData = res.data.data;
        });
    },

    // -------查看物资详情(已接收)
    detailclick1(row) {
      // 记录重复点击次数
      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.oldRow);
          } else {
            this.oldRowCount1 = 1;
          }
        } else {
          this.oldRow1 = null;
          return;
        }
      }
      // 重置上一个点击对象
      this.oldRow1 = row;
      // 根据销售订单物资中间表id查询物资信息
      this.axios
        .post("/api/v1/ams/getDispatchMaterialList?dispatchId="+row.dispatchId)
        .then((res) => {
          this.tableData1 = res.data.data;
        });
    },

    //接收所有已选中的订单
    receive(){
      this.$confirm("是否接收已选中的订单", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          //初始化承运商SsoId
          var carrierSsoId = '';
          if(getCookie("orgCode") == "chengyunshang"){
            carrierSsoId =  getCookie('userId');
          }else{
            carrierSsoId = null;
          }
          //所有选中的分派Id
          var dispatchIdList = [];
          this.selectTruckOrder.forEach((item) => {
              //初始化mapItem
              var mapItem = {
                //分派Id
                dispatchId : 0,
              }
              mapItem.dispatchId = item.dispatchId;
              dispatchIdList.push(mapItem);
          })
          //保存承运商SsoId和所有选中的分派Id
          let map = {
            carrierSsoId : carrierSsoId,
            mapList : dispatchIdList
          }
          //请求后端
          this.axios
            .post("/api/v1/ams/lockDispatchSaleOrderList",map)
            .then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "接收成功!",
                });     
                if(getCookie("orgCode") == "chengyunshang"){
                  this.first.requestUrl = "/api/v1/ams/getPCDispatchSaleOrderList?apiId=242&dispatchType=0&carrierSsoId="+ getCookie('userId')+"&i="+new Date();
                }else{
                this.first.requestUrl = "/api/v1/ams/getPCDispatchSaleOrderList?apiId=242&dispatchType=0&carrierSsoId=" + this.ssoId+"&i="+new Date();
                }
              } else {
                this.$message({
                  message: "接单失败,请联系管理员!",
                  type: "warning",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "接单操作已取消!",
          });
        });
    },
    //拒绝所有已选中的订单
    refuse(){
      this.$confirm("是否拒绝已选中的订单", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          //所有选中的分派Id
          var mapList = [];
          this.selectTruckOrder.forEach((item) => {
              //初始化mapItem
              var mapItem = {
                //分派Id
                dispatchId : 0,
              }
              mapItem.dispatchId = item.dispatchId;
              mapList.push(mapItem);
          })
          //请求后端
          this.axios
            .post("/api/v1/ams/refuseDispatchSaleOrderList",mapList)
            .then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "拒绝成功!",
                });     
                if(getCookie("orgCode") == "chengyunshang"){
                  this.first.requestUrl = "/api/v1/ams/getPCDispatchSaleOrderList?apiId=242&dispatchType=0&carrierSsoId="+ getCookie('userId')+"&i="+new Date();
                }else{
                this.first.requestUrl = "/api/v1/ams/getPCDispatchSaleOrderList?apiId=242&dispatchType=0&carrierSsoId=" + this.ssoId+"&i="+new Date();
                }
              } else {
                this.$message({
                  message: "拒绝失败,请联系管理员!",
                  type: "error",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "拒绝操作已取消!",
          });
        });
    }

  }
}
</script>

<style lang="scss" scode>
.steel_inbound{
  .sache{
    padding: 1.25rem 0.375rem;
    .el-input {
      width: 20%;
      margin-right: 1.25rem;
    }
  }
}
.button-box{
  display: flex;
  text-align: center;
  align-items: center;
  justify-content: center;
  margin-top: 0.3125rem;
  margin-bottom: 1.25rem;
}
</style>