<template>
  <!-- 销售订单页面 -->
  <div class="salePlan">
    <div class="top">
      <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-input
        placeholder="请输入内容"
        v-model="input"
        clearable
        style="width:250px"
      >
      </el-input>
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>
      </el-button>
      <el-button type="primary" @click="btnclick(0)">
        <i class="el-icon-plus"></i>钢材订单新增
      </el-button>
      <el-button type="primary" @click="refresh">
        <i class="el-icon-refresh"></i>
      </el-button>
      <el-button type="primary" @click="batchReport">
        <i class="upload2"></i>批量上传
      </el-button>
      <el-button
        type="primary"
        @click="exportData()"
        v-if="activeName == 'first'"
        ><i class="el-icon-download"></i>Excel</el-button
      >
    </div>
    <div class="table">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <!-- 未上报 -->
        <el-tab-pane label="未上报" name="first">
          <mergeRowTable
            v-bind.sync="option"
            ref="excelDom"
            @selection-change="selectionChange"
            :pageSize="200"
          >
            <el-table-column
              fixed="right"
              label="操作"
              align="center"
              width="200"
            >
              <template slot-scope="scope">
                <el-button
                  @click="uploadclick(scope.row.saleOrderId)"
                  type="text"
                  size="small"
                  >上传</el-button
                >
                <el-button
                  @click="click(scope.row.saleOrderId)"
                  type="text"
                  size="small"
                  >修改</el-button
                >
                <el-button
                  type="text"
                  size="small"
                  @click="deleteclick(scope.row.saleOrderId)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </mergeRowTable>
        </el-tab-pane>
        <!-- 已审批 -->
        <el-tab-pane label="已审批" name="four">
          <mergeRowTable v-bind.sync="option4" ref="table" :pageSize="200">
            <el-table-column
              fixed="right"
              label="操作"
              align="center"
              width="200"
            >
              <template slot-scope="scope">
                <el-button
                  @click="getOrderDetails(scope.row.saleOrderId)"
                  type="text"
                  size="small"
                  >运单</el-button
                >
                <el-button
                  @click="deleteSaleOrder(scope.row)"
                  type="text"
                  size="small"
                  >关闭订单</el-button
                >
                <el-button
                  @click="updateSaleOrder(scope.row)"
                  type="text"
                  size="small"
                  >异常处理</el-button
                >
              </template>
            </el-table-column>
          </mergeRowTable>
        </el-tab-pane>
        <el-tab-pane label="反审批" name="five">
          <mergeRowTable v-bind.sync="option5" ref="table2" :pageSize="200">
            <el-table-column
              fixed="right"
              label="操作"
              align="center"
              width="200"
            >
              <template slot-scope="scope">
                <el-button
                  @click="updateSaleOrder(scope.row)"
                  type="text"
                  size="small"
                  >异常处理</el-button
                >
              </template>
            </el-table-column></mergeRowTable
          >
        </el-tab-pane>
        <!-- 已上报 -->
        <el-tab-pane label="已上报" name="second">
          <mergeRowTable v-bind.sync="option2" ref="table2" :pageSize="200">
          </mergeRowTable>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>
<script>
import { getCookie } from "@/utils/util.js";
import { sjTime } from "@/utils/sharedJsFile";
export default {
  name: "saleOrder",
  data() {
    return {
      startTime: null,
      endTime: null,
      batchReportList: [],
      activeName: "first",
      tableTitle: "未上传销售订单报表",
      drawer: false,
      input: "",
      index: "",
      carrierId: null,
      carrierName: null,
      option: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/ams/getSaleOrderInfoes?apiId=408",
        selectionType: "select",
        comparison: "saleNumber",
        columnIndexs: [0, 1, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 16]
      },
      option2: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/ams/getSaleOrderReportedes?apiId=408",
        comparison: "saleNumber",
        columnIndexs: [0, 1, 2, 3, 7, 8, 9, 10, 11, 12, 13, 14, 15]
      },
      option4: {
        // 表格请求数据的地址
        requestUrl:
          "/api/v1/ams/getSaleOrderListBySaleCompanyes?apiId=409&shipperId=1&saleType=1",
        comparison: "saleNumber",
        columnIndexs: [
          0,
          1,
          2,
          3,
          7,
          8,
          9,
          10,
          11,
          12,
          13,
          14,
          15,
          16,
          17,
          18,
          20,
          21
        ]
      },
      option5: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/ams/getSteelOrderDeletedList?apiId=409",
        comparison: "saleNumber",
        columnIndexs: [
          0,
          1,
          2,
          3,
          7,
          8,
          9,
          10,
          11,
          12,
          13,
          14,
          15,
          16,
          17,
          18,
          20,
          21
        ]
      },
      mapList: [],
      mapItemList: []
    };
  },
  created() {
    if (getCookie("orgCode") == "shouhuokehu") {
      this.option.requestUrl =
        "/api/v1/ams/getSaleOrderInfoes?apiId=408&consigneeSsoId=" +
        getCookie("userId");
      this.option2.requestUrl =
        "/api/v1/ams/getSaleOrderReportedes?apiId=408&consigneeSsoId=" +
        getCookie("userId");
      this.option4.requestUrl =
        "/api/v1/ams/getSaleOrderListBySaleCompanyes?apiId=409&shipperId=1&saleType=1&consigneeSsoId=" +
        getCookie("userId");
      this.option5.requestUrl =
        "/api/v1/ams/getSteelOrderDeletedList?apiId=409&consigneeSsoId=" +
        getCookie("userId");
    } else {
      this.option.requestUrl =
        "/api/v1/ams/getSaleOrderInfoes?apiId=408&consigneeSsoId=" + null;
      this.option2.requestUrl =
        "/api/v1/ams/getSaleOrderReportedes?apiId=408&consigneeSsoId=" + null;
      this.option4.requestUrl =
        "/api/v1/ams/getSaleOrderListBySaleCompanyes?apiId=409&shipperId=1&saleType=1&consigneeSsoId=" +
        null;
      this.option5.requestUrl =
        "/api/v1/ams/getSteelOrderDeletedList?apiId=409&consigneeSsoId=" + null;
    }
  },
  methods: {
    deleteSaleOrder(row) {
      this.$prompt("此操作将关闭订单且下面的待运输订单，是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPlaceholder: "请输入关闭原因-必填！！！",
        center: true,
        type: "warning"
      })
        .then(({ value }) => {
          if (value != null) {
            let mapValue = {
              number: row.saleNumber,
              saleOrderId: row.saleOrderId,
              reason: value
            };
            this.axios
              .post("/api/v1/ams/deleteSaleOrderSame", mapValue)
              .then(res => {
                if (res.data.code == 200) {
                  this.$message.success("关闭成功");
                  this.getRequestUrl();
                }
              });
          } else {
            this.$message.error("请输入理由");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入"
          });
        });
    },
    updateSaleOrder(row) {
      console.log(row);
      this.$router.push(
        "updateSaleOrderSteel/" + row.saleOrderId + "/" + row.deleted
      );
    },
    getOrderDetails(saleOrderId) {
      console.log(saleOrderId);
      this.$router.push("saleOrderOODetails/" + saleOrderId);
    },
    getRequestUrl() {
      if (getCookie("orgCode") == "shouhuokehu") {
        this.option.requestUrl =
          "/api/v1/ams/getSaleOrderInfoes?apiId=408&consigneeSsoId=" +
          getCookie("userId") +
          "&i=" +
          new Date();
        this.option2.requestUrl =
          "/api/v1/ams/getSaleOrderReportedes?apiId=408&consigneeSsoId=" +
          getCookie("userId") +
          "&i=" +
          new Date();
        this.option4.requestUrl =
          "/api/v1/ams/getSaleOrderListBySaleCompanyes?apiId=409&shipperId=1&saleType=1&consigneeSsoId=" +
          getCookie("userId") +
          "&i=" +
          new Date();
        this.option5.requestUrl =
          "/api/v1/ams/getSteelOrderDeletedList?apiId=409&consigneeSsoId=" +
          getCookie("userId") +
          "&i=" +
          new Date();
      } else {
        this.option.requestUrl =
          "/api/v1/ams/getSaleOrderInfoes?apiId=408&consigneeSsoId=" +
          null +
          "&i=" +
          new Date();
        this.option2.requestUrl =
          "/api/v1/ams/getSaleOrderReportedes?apiId=408&consigneeSsoId=" +
          null +
          "&i=" +
          new Date();
        this.option4.requestUrl =
          "/api/v1/ams/getSaleOrderListBySaleCompanyes?apiId=409&shipperId=1&saleType=1&consigneeSsoId=" +
          null +
          "&i=" +
          new Date();
        this.option5.requestUrl =
          "/api/v1/ams/getSteelOrderDeletedList?apiId=409&consigneeSsoId=" +
          null +
          "&i=" +
          new Date();
      }
    },
    batchReport() {
      if (this.batchReportList.length === 0) {
        this.$message.error("请先选择需要批量上传的销售订单");
        return;
      }
      this.axios
        .post("/api/v1/ams/uploadSaleOrderList", this.batchReportList)
        .then(res => {
          if (res.data.code == "200") {
            this.$message.success("上传成功");
            this.getRequestUrl();
          } else {
            this.$message.error("上传失败");
          }
        });
    },
    selectionChange(selection) {
      console.log(selection);
      this.batchReportList = [];
      this.batchReportList = selection;
    },
    refresh() {
      this.$router.go(0);
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    onclick() {
      var startTime = null;
      var endTime = null;
      if (this.startTime && this.endTime) {
        startTime = sjTime(this.startTime);
        endTime = sjTime(this.endTime);
      }
      if (getCookie("orgCode") == "shouhuokehu") {
        if (this.activeName == "first") {
          this.option.requestUrl =
            "/api/v1/ams/getSaleOrderInfoes?apiId=408&saleType=1&con=" +
            this.input +
            "&consigneeSsoId=" +
            getCookie("userId") +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&i=" +
            new Date();
        } else if (this.activeName == "second") {
          this.option2.requestUrl =
            "/api/v1/ams/getSaleOrderReportedes?apiId=408&saleType=1&con=" +
            this.input +
            "&consigneeSsoId=" +
            getCookie("userId") +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&i=" +
            new Date();
        } else if (this.activeName == "four") {
          this.option4.requestUrl =
            "/api/v1/ams/getSaleOrderListBySaleCompanyes?apiId=409&saleType=1&shipperId=1&con=" +
            this.input +
            "&consigneeSsoId=" +
            getCookie("userId") +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&i=" +
            new Date();
        }
      } else {
        if (this.activeName == "first") {
          this.option.requestUrl =
            "/api/v1/ams/getSaleOrderInfoes?apiId=408&saleType=1&con=" +
            this.input +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&i=" +
            new Date();
        } else if (this.activeName == "second") {
          this.option2.requestUrl =
            "/api/v1/ams/getSaleOrderReportedes?apiId=408&saleType=1&con=" +
            this.input +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&i=" +
            new Date();
        } else if (this.activeName == "four") {
          this.option4.requestUrl =
            "/api/v1/ams/getSaleOrderListBySaleCompanyes?apiId=409&saleType=1&shipperId=1&con=" +
            this.input +
            "&startTime=" +
            startTime +
            "&endTime=" +
            endTime +
            "&i=" +
            new Date();
        }
      }
    },
    handleClose() {},
    seeclick(saleOrderId) {
      this.$router.push("/saleOrderDetail/" + saleOrderId);
    },
    btnclick() {
      this.$router.push("/addSaleOrder");
    },
    click(saleOrderId) {
      this.$router.push("/editSaleOrder/" + saleOrderId);
    },

    // 上传
    uploadclick(saleOrderId) {
      this.$confirm("是否上传", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          this.axios
            .post("/api/v1/ams/uploadSaleOrder?saleOrderId=" + saleOrderId)
            .then(res => {
              if (res.data.code == "200") {
                this.$message.success("上传成功！");
                this.getRequestUrl();
              } else {
                this.$message.success("上传失败！");
                this.getRequestUrl();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消上传!"
          });
        });
    },
    //删除
    deleteclick(scope) {
      let saleOrderId = scope;
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          this.axios
            .post(
              "/api/v1/ams/deleteAmsSaleOrderBySaleOrderId?saleOrderId=" +
                saleOrderId
            )
            .then(res => {
              if (res.data.code == "200") {
                this.$message.success("删除成功");
                this.getRequestUrl();
              } else {
                this.$message.success("删除失败");
                this.getRequestUrl();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消删除!"
          });
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.salePlan {
  .top {
    margin-left: 20px;
    margin-top: 20px;
  }
  .table {
    margin-left: 20px;
    margin-top: 20px;
  }
}
</style>
