//  销售订单排车
<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="Issue()">
        <i class="el-icon-download"></i>下发
      </el-button>
    </div>
    <template>
      <div>
        <dilTable v-bind.sync="first" @selection-change="selectionChange" ref="table">
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                @click="lookclick(scope.row.saleOrderId)"
                type="text"
                size="small"
                >查看修改地址</el-button
              >
              <el-button
                @click="passclick(scope.row.saleOrderId)"
                type="text"
                size="small"
                >修改日志</el-button
              >
              <el-button
                @click="materialclick(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>
      </div>
    </template>
  </div>
</template>
<script>
export default {
  data() {
    return {
      inputText: "",
      first: {
        // first请求数据的地址
        requestUrl: "/api/v1/ams/getTruckNoList?apiId=411&issueStatus=0",
        selectionType: "select",
        mapList: [],
      },
      activeName: "first",
      //记录旧的row对象
      oldRow: "",
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态
      oldRowCount: 1,
      tableHead: [
        {
          prop: "materialName",
          label: "物资名称",
          width: 150,
        },
        {
          prop: "specificationModel",
          label: "规格型号",
          width: 150,
        },
        {
          prop: "materialNumber",
          label: "物资件数",
          width: 100,
        },
        {
          prop: "materialWeight",
          label: "物资重量",
          width: 100,
        },
      ],
      tableData: [],
    };
    
  },
  methods: {
    Issue() {
      if (this.first.mapList.length == 0) {
        this.$message({
          type: "warning",
          message: "请先选择订单!",
        });
      } else {
        this.$confirm("是否下发", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        })
          .then(() => {
            //初始化maplist
            var mapList = [];
            this.first.mapList.forEach((item) => {
              //初始化mapItem
              var mapItem = {
                //销售订单物资中间表id
                saleOrderMaterialId: 0,
              };
              mapItem.saleOrderMaterialId = item.saleOrderMaterialId;
              mapList.push(mapItem);
            });
            this.$message({
              type: "success",
              message: "下发成功!",
            });
            this.axios.post("/api/v1/ams/issueTruckNo", mapList).then((res) => {
              if (res.data.code == "200") {
                this.$router.go(0);
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "取消下发!",
            });
          });
      }
    },
    lookclick(saleOrderId) {
      this.$router.push("/saleOrderDetailApproved/" + saleOrderId);
    },
    passclick(saleOrderId) {
      this.$router.push("/saleOrderUpdateLog/" + saleOrderId);
    },
    materialclick(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;
        });
    },
    selectionChange(selection) {
      this.first.mapList = selection;
    },
  },
};
</script>
<style lang="scss" scode>
.steel_inbound {
  .sache {
    padding: 1.25rem 0.375rem;
    .el-input {
      width: 20%;
      margin-right: 1.25rem;
    }
  }
}
</style>
