// 采购内转需求
<template>
  <div >
    <div style="margin:10px">
     <el-select v-model="condition1" placeholder="请选择筛选条件"  style="width:150px" clearable>
          <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          </el-option>
      </el-select>
      <el-input
          placeholder="请输入内容"
          v-model="input1"
          style="width:200px"
          clearable
      >
      </el-input>
      <el-select v-model="condition2" placeholder="请选择筛选条件" style="width:150px" clearable>
          <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          </el-option>
      </el-select>
      <el-input
          placeholder="请输入内容"
          v-model="input2"
          style="width:200px"
          clearable
      >
      </el-input>
      <span>需求时间：</span>
      <el-date-picker
        v-model="startTime"
        type="datetime"
        placeholder="选择日期"
        style="width:220px"
      >
      </el-date-picker>
      <span>至</span>
      <el-date-picker
        v-model="endTime"
        type="datetime"
        placeholder="选择日期"
        style="width:220px"
      >
      </el-date-picker>
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>查询
      </el-button>
      <el-button
        type="primary"
        class="btn"
        @click="toInsert"
        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>
    <div class="table">
      <el-tabs v-model="activeName">
        <el-tab-pane label="未下发" name="first">
          <dilTable v-bind.sync="first" @selection-change="selectionChange" max-height="300">
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="mini"
                  @click="deleteRequirement(scope)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </dilTable>
        </el-tab-pane>
        <el-tab-pane label="已下发" name="second">
          <dilTable v-bind.sync="second" @func="func" max-height="300">
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="mini"
                  @click="stopRequirement(scope)"
                >
                  {{ startStop }}
                </el-button>
              </template>
            </el-table-column>
          </dilTable>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>
<script>
import { getCookie } from "@/utils/util.js";
import { sjTime } from "@/utils/sharedJsFile";
export default {
  data() {
    return {
      condition1:null,
      condition2:null,
      input1:null,
      input2:null,
      options:[
          {
          label:"采购订单号",
          value:"purchaseOrderNo"
          },
          {
          label:"发货单位",
          value:"supplierName"
          },
          {
          label:"卸货点",
          value:"warehouseName"
          },
          {
          label:"船名",
          value:"shipName"
          }
      ],
      endTime: null,
      startTime: null,
      startStop: null,
      startStopValue: null,
      inputText: null,
      first: {
        // first请求数据的地址
        requestUrl: "",
        selectionType: "select",
        mapList: []
      },
      second: {
        // second请求数据的地址
        requestUrl: ""
      },
      activeName: "first"
    };
  },
  created() {
    if (
      getCookie("orgCode") == "dagangadmin" ||
      getCookie("orgCode") == "zidonghuabu"
    ) {
      this.first.requestUrl =
        "/api/v1/ams/getPurRequirementList?apiId=431&requirementStatus=0";
      this.second.requestUrl =
        "/api/v1/ams/getPurRequirementList?apiId=431&requirementStatus=1";
    } else {
      this.first.requestUrl =
        "/api/v1/ams/getPurRequirementList?apiId=431&requirementStatus=0&userId=" +
        getCookie("orgCode");
      this.second.requestUrl =
        "/api/v1/ams/getPurRequirementList?apiId=431&requirementStatus=1&userId=" +
        getCookie("orgCode");
    }
  },
  methods: {
    func(res) {
      res.list.forEach(e => {
        if (e.enable == "未启用") {
          this.startStop = "停用";
        } else {
          this.startStop = "停用";
        }
      });
    },
    //查询方法，支持查采购订单号、发货单位、卸货点、物资名称、船名
    onclick() {
       let requestQuery={}
      if(this.condition1 == this.condition2){
            requestQuery[this.condition1]=[this.input1,this.input2]
        }else{
            requestQuery[this.condition1]=[this.input1+'']
            requestQuery[this.condition2]=[this.input2+'']
        }
      console.log(this.inputText);
      let orgCode = null;
      let startTime = null;
      let endTime = null;
      if (this.startTime && this.endTime) {
        startTime = sjTime(this.startTime);
        endTime = sjTime(this.endTime);
      }
      if (
        getCookie("orgCode") != "dagangadmin" &&
        getCookie("orgCode") != "zidonghuabu"
      ) {
        orgCode = getCookie("orgCode");
      }
      if (this.activeName == "first") {
        this.first.requestUrl =
          "/api/v1/ams/getPurRequirementList?apiId=431&requirementStatus=0&orgCode=" +
          orgCode +
          "&startTime=" +
          startTime +
          "&endTime=" +
          endTime +
          "&i=" +
          new Date();
           this.first.requestQuery=requestQuery;
      } else {
        this.second.requestUrl =
          "/api/v1/ams/getPurRequirementList?apiId=431&requirementStatus=1&orgCode=" +
          orgCode +
          "&startTime=" +
          startTime +
          "&endTime=" +
          endTime +
          "&i=" +
          new Date();
           this.second.requestQuery=requestQuery;
      }
      this.$forceUpdate();
    },
    getRequestUrl() {
      if (
        getCookie("orgCode") == "dagangadmin" ||
        getCookie("orgCode") == "zidonghuabu"
      ) {
        this.first.requestUrl =
          "/api/v1/ams/getPurRequirementList?apiId=431&requirementStatus=0" +
          "&t=" +
          new Date();
        this.second.requestUrl =
          "/api/v1/ams/getPurRequirementList?apiId=431&requirementStatus=1" +
          "&t=" +
          new Date();
      } else {
        this.first.requestUrl =
          "/api/v1/ams/getPurRequirementList?apiId=431&requirementStatus=0&userId=" +
          getCookie("orgCode") +
          "&t=" +
          new Date();
        this.second.requestUrl =
          "/api/v1/ams/getPurRequirementList?apiId=431&requirementStatus=1&userId=" +
          getCookie("orgCode") +
          "&t=" +
          new Date();
      }
    },
    selectionChange(selection) {
      this.first.mapList = selection;
    },
    toInsert() {
      this.$router.push("/addRequirement");
    },
    deleteRequirement(scope) {
      console.log(scope.row.requirementId);
      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: "删除操作已取消!"
          });
        });
    },
    stopRequirement(scope) {
      this.$confirm("是否停用", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          this.axios
            .post("/api/v1/ams/changePlanStatus/" + scope.row.planId)
            .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() {
      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 {
    margin-left: 20px;
    margin-top: 20px;
    .el-input {
      margin-right: 1.25rem;
    }
  }
  .table {
    margin-left: 20px;
    margin-top: 20px;
  }
}
</style>
