// 提出需求
<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="toInsert2"
        v-if="activeName == 'first'"
      >
        <i class="el-icon-plus"></i>新增
      </el-button>
      <el-button
        type="primary"
        class="btn"
        @click="Issue"
        v-if="activeName == 'first'"
      >
        <i class="el-icon-download"></i>下发
      </el-button>
    </div>
    <template>
      <div>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="未下发" name="first">
            <dilTable v-bind.sync="first" @selection-change="selectionChange" ref="table1">
              <el-table-column fixed="right" label="操作" width="120">
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    size="mini"
                    @click="getRequirementMaterial1(scope.row)"
                  >
                    物资详情
                  </el-button>
                  <el-button
                    type="text"
                    size="mini"
                    @click="deleteRequirement(scope)"
                  >
                    删除
                  </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-tab-pane label="已下发" name="second">
            <dilTable v-bind.sync="second" ref="table">
            <el-table-column fixed="right" label="操作" width="80">
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    size="mini"
                    @click="getRequirementMaterial(scope.row)"
                  >
                    物资详情
                  </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-tabs>
      </div>
    </template>
  </div>
</template>
<script>
import { getCookie } from "@/utils/util.js";
export default {
  data() {
    return {
      inputText: "",
      first: {
        // first请求数据的地址
        requestUrl:
          "",
        selectionType: "select",
        mapList: [],
      },
      second: {
        // second请求数据的地址
        requestUrl:
          "",
      },
      //记录旧的row对象(未下发)
      oldRow: "",
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态(未下发)
      oldRowCount: 1,
      //记录旧的row对象(已下发)
      oldRow1: "",
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态(已下发)
      oldRowCount1: 1,
      tableHead: [
        {
          prop: "materialName",
          label: "物资名称",
          width: 150,
        },
        {
          prop:"loadName",
          label:"装货点",
          width:150
        },
        {
          prop:"unloadName",
          label:"卸货点",
          width:150
        },
        {
          prop: "materialWeight",
          label: "物资重量",
          width: 150,
        },
        {
          prop: "materialCount",
          label: "物资数量",
          width: 150,
        },
      ],
      tableData: [],
      tableData1: [
        {
          materialCount : 100
        }
      ],
      activeName: "first",
    };
  },
  created(){
    if(getCookie("orgCode") == "dagangadmin"||getCookie("orgCode") == "zidonghuabu"){
      this.first.requestUrl = "/api/v1/ams/getTruckRequirementList?apiId=207&requirementStatus=0"
      this.second.requestUrl = "/api/v1/ams/getTruckRequirementList?apiId=207&requirementStatus=1"
    }else{
      this.first.requestUrl = "/api/v1/ams/getTruckRequirementList?apiId=207&requirementStatus=0&orgCode=" + getCookie("orgCode")
      this.second.requestUrl = "/api/v1/ams/getTruckRequirementList?apiId=207&requirementStatus=1&orgCode=" + getCookie("orgCode")
    }
  },
  
  methods: {
    getRequirementMaterial(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.requirementId);
    },
    getRequirementMaterial1(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;
      this.getMaterial(row.requirementId);
    },
    handleClick(tab, event) {
      this.getRequestUrl()
      this.getRequirementMaterial()      
    },
    getMaterial(requirementId){
      console.log(requirementId)
      this.axios.post("/api/v1/ams/getRequirementMaterial/" + requirementId).then((res) => {
        this.tableData = res.data.data
        this.tableData1 = res.data.data
        console.log(res.data.data)
      })
    },
    getRequestUrl(){
      if(getCookie("orgCode") == "dagangadmin"||getCookie("orgCode") == "zidonghuabu"){
      this.first.requestUrl = "/api/v1/ams/getTruckRequirementList?apiId=207&requirementStatus=0&i=" + new Date()
      this.second.requestUrl = "/api/v1/ams/getTruckRequirementList?apiId=207&requirementStatus=1&i=" + new Date()
      }else{
      this.first.requestUrl = "/api/v1/ams/getTruckRequirementList?apiId=207&requirementStatus=0&orgCode=" + getCookie("orgCode") + "&i=" + new Date()
      this.second.requestUrl = "/api/v1/ams/getTruckRequirementList?apiId=207&requirementStatus=1&orgCode=" + getCookie("orgCode") + "&i=" + new Date()
      }
    },
    selectionChange(selection) {
      this.first.mapList = selection;
    },
    toInsert2() {
      this.$router.push("/addRequirement2");
    },
    updateRequirement(scope) {
      this.$router.push(
        "/updateRequirement/" + scope.row.requirementId
      );
    },
    deleteRequirement(scope) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.axios
            .post(
              "/api/v1/ams/deleteTruckRequirement/" + scope.row.requirementId
            )
            .then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.getRequestUrl()
              } else {
                this.$message({
                  message: "删除失败",
                  type: "warning",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "删除操作已取消!",
          });
        });
    },
    Issue() {
      console.log(this.first.mapList);
      if(this.first.mapList.length == 0){
        this.$message.warning("请选择需求")
        return
      }
      this.$confirm("是否下发", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.axios
            .post("/api/v1/ams/downRequirement", this.first.mapList)
            .then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "下发成功!",
                });
              this.getRequestUrl()
              this.activeName = 'second'
              } else {
                this.$message({
                  message: "下发失败",
                  type: "warning",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消下发!",
          });
        });
    },
  },
};
</script>
<style lang="scss">
.steel_inbound{
  .sache{
    padding: 1.25rem 0.375rem;
    .el-input {
      width: 20%;
      margin-right: 1.25rem;
    }
  }
  }
</style>
