// 接收运单
<template>
  <div class="steel_inbound">
    <div class="sache">
      <el-input placeholder="请输入内容" v-model="inputText" 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="batchCloseOrder">
        <i></i>批量关闭订单
      </el-button>
      <el-button type="primary" @click="refresh">
        <i class="el-icon-refresh"></i>刷新
      </el-button>
    </div>
    <el-dialog title="车辆信息" :visible.sync="dialogTableVisible" :before-close="handleClose">
    <el-input placeholder="请输入内容" v-model="inputTruck" clearable style="width:200px">
      </el-input>
      <el-button type="primary" class="btn" @click="onClickTruck">
        <i class="el-icon-search"></i>查询
      </el-button>
    <dilTable
          v-bind.sync="truck"
          @radio-change="currentRadioChange1"
        >
    </dilTable>
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogTableVisible = false">取 消</el-button>
    <el-button type="primary" @click="updateTruckCapacity()">确 定</el-button>
    </span>
    </el-dialog>
        <el-tabs v-model="activeName">
          <!-- 未接收 -->
          <el-tab-pane label="未接收" name="third">
            <dilTable v-bind.sync="option3" @selection-change="selectionChange" ref="table2">
              <el-table-column fixed="right" label="操作" width="180">
                <template slot-scope="scope">
                  <el-button
                    @click="updateCapacity(scope.row)"
                    type="text"
                    size="small"
                  >
                    修改运力
                  </el-button>
                  <el-button
                    @click="detailclick2(scope.row)"
                    type="text"
                    size="small"
                  >
                    物资详情
                  </el-button>
                  <el-button
                    @click="closeOrder(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="tableData2" 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="first">
            <dilTable v-bind.sync="option1" ref="table">
              <el-table-column fixed="right" label="操作" width="50">
                <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="option2" ref="table1">
              <el-table-column fixed="right" label="操作" width="50">
                <template slot-scope="scope">
                  <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>
</template>
<script>
import { getCookie } from "@/utils/util.js";
export default {
  data() {
    return {
      inputTruck:null,
      inputText: "",
      option1: {
        // option1请求数据的地址，承运商(运输订单状态为已接收，接收状态为已接收)
        requestUrl:
          "/api/v1/oms/getDriverInfoForSale?apiId=226&orderType=2&orderStatus=5&carrierSsoId=" +
          getCookie("userId"),
      },
      option2: {
        // option2请求数据的地址，承运商暂时设置为默认（运输订单状态为）
        requestUrl:
          "/api/v1/oms/getDriverInfoForSale?apiId=226&orderType=2&orderStatus=6&carrierSsoId=" +
          getCookie("userId"),
      },
      option3: {
        // option3请求数据的地址
        requestUrl:
          "/api/v1/oms/getDriverInfoForSale?apiId=226&orderType=2&orderStatus=4&carrierSsoId=" +
          getCookie("userId"),
        selectionType: "select",
        mapList: [],
      },
      truck: {
        requestUrl:
          "",
        selectionType:"radio"
      },
      activeName: "third",
      //记录旧的row对象(已接收)
      oldRow: "",
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态(已接收)
      oldRowCount: 1,
      //记录旧的row对象(已拒绝)
      oldRow1: "",
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态(已拒绝)
      oldRowCount1: 1,
      //记录旧的row对象(未接收)
      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: [],
      capacityId:null,
      dialogTableVisible:false,
      orderId:null
    };
  },
  methods: {
    onClickTruck(){
        if (getCookie("orgCode") == "chengyunshang") {
        this.truck.requestUrl =
          "/api/v1/uc/getAllCapacityByCarrierLike?apiId=248&carrierSsoId=" +
          getCookie("userId") + "&index="+this.inputTruck;
      } else {
        this.truck.requestUrl =
          "/api/v1/uc/getAllCapacityByCarrierLike?apiId=248&carrierSsoId=" + null + "&index="+this.inputTruck;
      }
    },
    getRequestUrl(){
        this.option1.requestUrl = "/api/v1/oms/getDriverInfoForSale?apiId=226&orderType=2&orderStatus=5&carrierSsoId=" +
          getCookie("userId") + "&t=" + new Date(),
        this.option2.requestUrl = "/api/v1/oms/getDriverInfoForSale?apiId=226&orderType=2&orderStatus=6&carrierSsoId=" +
          getCookie("userId") + "&t=" + new Date(),
        this.option3.requestUrl = "/api/v1/oms/getDriverInfoForSale?apiId=226&orderType=2&orderStatus=4&carrierSsoId=" +
          getCookie("userId") + "&t=" + new Date()
    },
    selectionChange(selection) {
      this.option3.mapList = selection;
    },
    batchCloseOrder(){
      if (this.option3.mapList.length == 0) {
        this.$message({
          type: "warning",
          message: "请先选择要关闭的订单!",
        });
      } else {
        this.$confirm("是否关闭这些订单", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        })
          .then(() => {
            //初始化maplist
            var mapList = [];
            this.option3.mapList.forEach((item) => {
              //初始化mapItem
              var mapItem = {
                //销售订单物资中间表id
                orderId: 0,
              };
              mapItem.orderId = item.orderId;
              mapList.push(mapItem);
            });
            this.$message({
              type: "success",
              message: "关闭成功!",
            });
            this.axios.post("/api/v1/oms/batchCloseOrder", mapList).then((res) => {
              if (res.data.code == "200") {
                this.$router.go(0);
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "取消关闭!",
            });
          });
      }
    },
    refresh(){
      this.$router.go(0);
    },
    handleClose(done){
      done()
      this.$message.info("取消修改运力")
    },
    updateCapacity(row){
      console.log("更新车牌号")
      this.getTruckRequestUrl()
      this.orderId = row.orderId
      this.dialogTableVisible = true
      console.log(row.orderId)
    },
    getTruckRequestUrl(){
        if (getCookie("orgCode") == "chengyunshang") {
      this.truck.requestUrl =
        "/api/v1/uc/getAllCapacityByCarrierLike?apiId=429&carrierSsoId=" +
        getCookie("userId") + "&test=" + new Date();
      } else {
      this.truck.requestUrl =
        "/api/v1/uc/getAllCapacityByCarrierLike?apiId=429&carrierSsoId=" + null +  "&test=" + new Date();
      }
    },
    updateClick(scope){
      this.getTruckRequestUrl()
      this.orderId = scope.row.orderId
      this.dialogTableVisible = true
      console.log(scope.row.orderId)
    },
    currentRadioChange1(row){
      console.log(row)
      this.capacityId = row.capacityId
    },
    updateTruckCapacity(){
        if(this.capacityId == null){
            this.$message.error("未选中运力")
            return
        }
        this.axios.post("/api/v1/oms/updateCapacityId",{
          capacityId:this.capacityId,
          orderId :this.orderId
        }).then((res) =>{
          if(res.data.code == "200"){
            this.$message.success("修改运力成功")
            this.getRequestUrl()
            this.dialogTableVisible = false
          }else{
            this.$message.error("修改运力失败")
          }
        })
    },
    onClick() {
      if (this.activeName == "first") {
        this.option1.requestUrl =
          "/api/v1/oms/getDriverInfoForSale?apiId=226&orderType=1&orderStatus=5&carrierSsoId=" +
          getCookie("userId") +
          "&con=" +
          this.inputText;
      } else if (this.activeName == "second") {
        this.option1.requestUrl =
          "/api/v1/oms/getDriverInfoForSale?apiId=226&orderType=1&carrierSsoId=" +
          getCookie("userId") +
          "&con=" +
          this.inputText;
      } else {
        this.option1.requestUrl =
          "/api/v1/oms/getDriverInfoForSale?apiId=226&orderType=1&orderStatus=4&carrierSsoId=" +
          getCookie("userId") +
          "&con=" +
          this.inputText;
      }
    },
    // -------查看物资详情 (已接收)
    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;
// 根据销售订单id查询物资信息
      this.axios
      .post("/api/v1/oms/getOrderMaterialList?orderId=" + row.orderId)
        .then((res) => {
          console.log("res.data.data",res.data.data)
          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.oldRow1);
          } else {
            this.oldRowCount1 = 1;
          }
        } else {
          this.oldRow1 = null;
          return;
        }
      }
      // 重置上一个点击对象
      this.oldRow1 = row;
      // 根据销售订单物资中间表id查询物资信息
      this.axios
        .post("/api/v1/oms/getOrderMaterialList?orderId=" + row.orderId)
        .then((res) => {
          console.log("res.data.data",res.data.data)
          this.tableData1 = res.data.data;
        });
    },

    // -------查看物资详情(未接收)
    detailclick2(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;
      // 根据销售订单物资中间表id查询物资信息
      this.axios
        .post("/api/v1/oms/getOrderMaterialList?orderId=" + row.orderId)
        .then((res) => {
          this.tableData2 = res.data.data;
        });
    },
    //关闭选中行的订单
    closeOrder(row){
      this.$confirm("是否关闭", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "关闭成功!",
          });
          this.axios
            .post("/api/v1/oms/closeOrder?orderId=" + row.orderId)
            .then((res) => {
              if(res.data.code == '200'){
                this.$router.go(0);
              }
            });
        })
        .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;
    }
  }
}
</style>
