<template>
  <!-- 销售订单页面 -->
  <div class="salePlan">
    <div class="top">
      <span class="text">订单时间：</span>
      <el-date-picker
        v-model="startTime"
        type="datetime"
        placeholder="选择日期"
        style="width:200px"
      >
      </el-date-picker>
      <span class="text">至</span>
      <el-date-picker
        v-model="endTime"
        type="datetime"
        placeholder="选择日期"
        style="width:200px"
      >
      </el-date-picker>
      <el-input
        placeholder="请输入内容"
        v-model="input"
        clearable
        style="width:200px"
      >
      </el-input>
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>
      </el-button>
      <el-button type="primary" class="btn" @click="empower">
        <i class="el-icon-check"></i>销售订单授权承运商
      </el-button>
      <el-button type="primary" @click="refresh">
        <i class="el-icon-refresh"></i>
      </el-button>
    </div>
    <div class="table">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <!-- 已审批 -->
        <el-tab-pane label="已审批" name="four">
          <mergeRowTable
            v-bind.sync="option4"
            @selection-change="selectionChange"
            ref="table"
            :pageSize="200"
          >
            <el-table-column fixed="right" label="操作" width="200">
              <template slot-scope="scope">
                <el-button
                  @click="coproductSendClick(scope.row.saleOrderId)"
                  type="text"
                  size="small"
                  >副产品派车</el-button
                >
                <el-button
                  @click="checkSaleOrderDetails(scope.row.saleOrderId)"
                  type="text"
                  size="small"
                  >运单详情</el-button
                >
                <el-button
                  @click="deleteSaleOrder(scope.row)"
                  type="text"
                  size="small"
                  >关闭订单</el-button
                >
              </template>
            </el-table-column>
          </mergeRowTable>
        </el-tab-pane>
        <el-tab-pane label="已关闭" name="first">
          <mergeRowTable v-bind.sync="option" ref="table" :isPagination="false">
          </mergeRowTable>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 承运商模态框 -->
    <el-drawer
      title="选择承运商"
      :visible.sync="drawer"
      direction="rtl"
      size="40%"
      :show-close="false"
    >
      <el-input
        placeholder="请输入内容"
        v-model="index"
        style="margin-top: 10px; margin-left: 20px;width:250px"
        clearable
      >
      </el-input
      ><el-button
        type="primary"
        class="btn"
        @click="onclickCarrier"
        style="margin-left: 4px;"
      >
        <i class="el-icon-search"></i>查询
      </el-button>
      <!-- <el-button @click="open">取消</el-button> -->
      <el-button type="primary" @click="selectMakeSure">确定</el-button>
      <div class="tablecls">
        <!-- 查询所有的承运商 -->
        <dilTable
          v-bind.sync="carrier"
          @radio-change="currentRadioChange"
          :drawer="drawer"
        >
        </dilTable>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { sjTime } from "@/utils/sharedJsFile";
export default {
  name: "saleOrder",
  data() {
    return {
      startTime: null,
      endTime: null,
      activeName: "four",
      drawer: false,
      input: "",
      index: "",
      carrierId: null,
      carrierName: null,
      option4: {
        // 表格请求数据的地址
        requestUrl:
          "/api/v1/ams/getFuSaleOrderList?apiId=481" + "&deleted=" + Number(0),
        selectionType: "select",
        comparison: "saleNumber",
        columnIndexs: [0, 1, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15]
      },
      option: {
        // 表格请求数据的地址
        requestUrl:
          "/api/v1/ams/getFuSaleOrderList?apiId=481&deleted=" + Number(2),
        comparison: "saleNumber",
        columnIndexs: [0, 1, 2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15]
      },
      carrier: {
        requestUrl: "",
        selectionType: "radio"
      },
      mapList: [],
      mapItemList: []
    };
  },
  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.refresh();
                }
              });
          } else {
            this.$message.error("请输入理由");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入"
          });
        });
    },
    checkSaleOrderDetails(saleOrderId) {
      console.log(saleOrderId);
      this.$router.push("saleOrderOODetails/" + saleOrderId);
    },
    refresh() {
      this.option4.requestUrl =
        "/api/v1/ams/getFuSaleOrderList?apiId=481&i=" +
        new Date() +
        "&deleted=" +
        Number(0);
      this.option.requestUrl =
        "/api/v1/ams/getFuSaleOrderList?apiId=481&i=" +
        new Date() +
        "&deleted=" +
        Number(2);
    },
    handleClick(tab, event) {},
    onclick() {
      let startTime = null;
      let endTime = null;
      if (this.startTime && this.endTime) {
        startTime = sjTime(this.startTime);
        endTime = sjTime(this.endTime);
      }
      if (this.activeName == "four") {
        this.option4.requestUrl =
          "/api/v1/ams/getFuSaleOrderList?apiId=481&con=" +
          this.input +
          "&deleted=" +
          Number(0) +
          "&startTime=" +
          startTime +
          "&endTime=" +
          endTime +
          "&i=" +
          new Date();
      } else {
        this.option.requestUrl =
          "/api/v1/ams/getFuSaleOrderList?apiId=481&con=" +
          this.input +
          "&deleted=" +
          Number(2) +
          "&startTime=" +
          startTime +
          "&endTime=" +
          endTime +
          "&i=" +
          new Date();
      }
    },
    //模糊查询承运商
    onclickCarrier() {
      this.carrier.requestUrl =
        "/api/v1/uc/getCarrierListByLike?apiId=412&index=" +
        this.index +
        "&i=" +
        new Date();
    },
    selectionChange(selection) {
      this.mapList = [];
      this.mapList = selection;
    },
    currentRadioChange(row) {
      (this.carrierId = row.carrierId), (this.carrierName = row.carrierName);
    },
    empower() {
      if (this.mapList.length == 0) {
        this.$message.warning("请先选择至少一个销售订单");
      } else {
        this.carrier.requestUrl =
          "/api/v1/uc/getCarrierListByLike?apiId=412&i=" + new Date();
        this.drawer = true;
      }
    },
    selectMakeSure() {
      this.index = null;
      this.drawer = false;
      this.$confirm(
        "是否确认授权选中的销售订单给承运商:(" + this.carrierName + ")",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        }
      )
        .then(() => {
          this.mapList.forEach(item => {
            //初始化mapItem
            var mapItem = {
              //销售订单id
              saleOrderId: 0
            };
            mapItem.saleOrderId = item.saleOrderId;
            this.mapItemList = [];
            this.mapItemList.push(mapItem);
          });
          //初始化传递的参数
          let mapValue = {
            saleOrderIds: this.mapItemList,
            carrierId: this.carrierId
          };
          this.axios
            .post("/api/v1/ams/saleOrderEmpowerCarrier", mapValue)
            .then(res => {
              if (res.data.code == "0") {
                this.$message({ type: "success", message: "授权成功!" });
                this.refresh();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消授权!"
          });
        });
    },
    seeclick(saleOrderId) {
      this.$router.push("/saleOrderDetail/" + saleOrderId);
    },
    click(saleOrderId) {
      this.$router.push("/editSaleOrder/" + saleOrderId);
    },
    coproductSendClick(saleOrderId) {
      this.$router.push("/addSaleOrderCoproductSend/" + saleOrderId);
    }
  }
};
</script>
<style lang="scss" scoped>
.salePlan {
  .top {
    margin-left: 20px;
    margin-top: 20px;
  }
  .table {
    margin-left: 20px;
    margin-top: 20px;
  }
}
</style>
