// 定向派单
<template>
  <div class="steel_inbound">
    <div class="sache">
      <el-input placeholder="请输入内容" v-model="inputText" clearable>
      </el-input>
      <el-button type="primary" class="btn">
        <i class="el-icon-search"></i>查询
      </el-button>
      <el-button
        type="primary"
        class="btn"
        @click="table = true"
        v-if="activeName == 'first'"
      >
        <i class="el-icon-download"></i>下发
      </el-button>
    </div>
    <template>
      <div>
        <el-tabs v-model="activeName">
          <el-tab-pane label="未下发" name="first">
            <dilTable v-bind.sync="first" ref="table" @selection-change="selectionChange">
              <el-table-column fixed="right" label="操作" width="70">
            <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="100">
                <template slot-scope="scope">
                  <el-button
                    @click="clickAddDetail(scope.row.orderMaterialId)"
                    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>
    </template>

    <!-- 承运商模态框 -->
    <el-drawer
      title="选择承运商"
      :visible.sync="table"
      direction="rtl"
      size="35%"
      :show-close="false"
    >
      <el-input placeholder="请输入内容" v-model="index"  
      style="margin-top: 10px; margin-left: 20px;width:160px" clearable> </el-input
      ><el-button type="primary" class="btn" @click="onclick" style="margin-left: 4px;">
        <i class="el-icon-search"></i>查询
      </el-button>
      <!-- <el-button @click="open">取消</el-button> -->
      <el-button type="primary" @click="makeSure1">确定</el-button>
      <div class="tablecls">
        <!-- 查询所有的承运商 -->
        <dilTable v-bind.sync="option" @radio-change="currentRadioChange1">
        </dilTable>
      </div>
    </el-drawer>
  </div>
</template>
<script>
export default {
  data() {
    return {
      inputText: "",
      index: "",
      carrierId: 0,
      table:false,
      first: {
        // first请求数据的地址
        requestUrl: "/api/v1/ams/getTruckNoList?apiId=411&issueStatus=1",
        selectionType: "select",
        mapList: [],
      },
      second: {
        // second请求数据的地址
        requestUrl: "/api/v1/ams/getSaleOrderList?apiId=245&status=0",
      },
      activeName: "first",
      option: {
        requestUrl: "/api/v1/uc/getCarrierListByLike?apiId=412",
        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: 150,
        },
      ],
      tableData: [],
      tableData1: [],
    };
  },
  methods: {
    clickAddDetail(index) {
      console.log(index);
      this.$confirm("是否公开", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        })
          .then(() => {
            this.axios
              .post(
                "/api/v1/ams/addOpenAmsDispatchSaleOrder/" + index
              )
              .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: "取消公开!",
            });
          });
    },
    selectionChange(selection) {
      this.first.mapList = selection;
    },
    //点击下发事件
    ondrawer() {
      // this.row = row;
      // this.row.index = index;
      this.drawer = true;
    },
    //监听模态框点击事件
    currentRadioChange1(row) {
      this.carrierId = row.carrierId;
      console.log("this.carrierId",this.carrierId)
    },
    //模糊查询承运商
    onclick(){
      this.option.requestUrl =
      "/api/v1/uc/getCarrierListByLike?apiId=412&index=" + this.index;
    },
    // 返回主界面并下发
    makeSure1() {
      if(this.first.mapList.length==0){
        this.$message({
            type: "warning",
            message: "请先选择订单!",
          });
      }else{
      //初始化maplist
      var mapList = [];
      this.first.mapList.forEach((item,i) => {
        //初始化mapItem
        var mapItem={
                //销售订单物资中间表id
                saleOrderMaterialId : 0,
            };
        mapItem.saleOrderMaterialId=item.saleOrderMaterialId;
        mapList.push(mapItem);
      });
      //初始化
      let map = {
        //承运商ID
        carrierId : this.carrierId,
        //销售订单物资中间表id的mapList
        mapList : mapList
      }
      this.axios.post("/api/v1/ams/addDispatchSaleOrder", map).then((res) => {
          if (res.data.code == "200") {
            this.$message({
            type: "success",
            message: "下发成功!",
          });
          this.first.requestUrl = "/api/v1/ams/getTruckNoList?apiId=411&issueStatus=1&i=" + new Date();
          this.table = false;
          }
        });
      }
      
    },
    // -------查看物资详情(未下发)
    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.saleOrderMaterialId);
    },
    // 根据销售订单物资中间表id查询物资信息
    getMaterial(saleOrderMaterialId) {
      this.axios
        .post("/api/v1/ams/getTruckNoMaterialList?saleOrderMaterialId=" +saleOrderMaterialId)
        .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.oldRow1);
          } 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;
        });
    },

  },
};
</script>
<style lang="scss" scode>
.steel_inbound {
  .sache {
    padding: 1.25rem 0.375rem;
    .el-input {
      width: 20%;
      margin-right: 1.25rem;
    }
  }
}
</style>
