<template>
  <!-- 提货委托页面 -->
  <div class="shipTransport">
    <div class="top">
      <el-input
        class="el-input"
        placeholder="请输入内容"
        v-model="input"
        clearable
      >
      </el-input>
      <!-- <el-date-picker
        v-model="startTime"
        type="datetime"
        placeholder="选择日期"
      >
      </el-date-picker>
      <span>至</span>
      <el-date-picker v-model="endTime" type="datetime" placeholder="选择日期">
      </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="addClick">
        <i class="el-icon-circle-plus-outline"></i>新增
      </el-button>
       <el-button type="primary" @click="refresh()">
        <i class="el-icon-refresh"></i>刷新
      </el-button>
      <el-button
        type="primary"
        class="btn"
        @click="addPick"
        v-if="activeName === 'second'"
      >
        <i class="el-icon-circle-plus-outline"></i>生成提货委托书
      </el-button>
    </div>
    <el-tabs v-model="activeName">
      <!-- 未下发 -->
      <el-tab-pane label="未下发" name="first">
        <dilTable v-bind.sync="option1" ref="table">
          <el-table-column
            fixed="right"
            align="center"
            label="操作"
            width="120"
          >
            <template slot-scope="scope">
              <el-button
                @click="sendClick(scope.row.attorneyId)"
                type="text"
                size="small"
              >
                下发
              </el-button>
              <el-button
                @click="modifyClick(scope.row.attorneyId)"
                type="text"
                size="small"
              >
                修改
              </el-button>
              <el-button
                @click="deleteclick(scope.row.attorneyId)"
                type="text"
                size="small"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </dilTable>
      </el-tab-pane>
      <!-- 已下发 -->
      <el-tab-pane label="已下发" name="second">
        <dilTable v-bind.sync="option2" @selection-change="selectionChange">
          <el-table-column
            fixed="right"
            align="center"
            label="操作"
            width="120"
          >
            <!-- <template slot-scope="scope">
              <el-button
                @click="modifyClick(scope.row.attorneyId)"
                type="text"
                size="small"
                :disabled="scope.row.purchaseOrderMaterialNum && scope.row.realForeignShipName && scope.row.realForeignShipName != '' && scope.row.realForeignShipName != 'null'"
              >
                修改
              </el-button>
            </template> -->
          </el-table-column>
        </dilTable>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { sjTime } from "@/utils/sharedJsFile";
import { getCookie } from "@/utils/util.js";
export default {
  name: "homeworkPath",
  data() {
    return {
      input: "",
      activeName: "first",
      attorneyId: null,
      option1: {
        // 表格请求数据的地址
        requestUrl:
          "/api/v1/tms/getAmsshipDeliveryAttroneyList?apiId=73&status=0"
      },
      option2: {
        // 表格请求数据的地址
        requestUrl:
          "/api/v1/tms/getAmsshipDeliveryAttroneyList?apiId=73&status=1",
        selectionType: "select"
      },
      amsShipDeliveryList: [],
      startTime: null,
      endTime: null,
    };
  },

  methods: {
    selectionChange(selection) {
      this.amsShipDeliveryList = selection;
    },
    onclick() {
      let startTime = null;
      let endTime = null;
      if (this.startTime) {
        startTime = sjTime(this.startTime);
      }
      if (this.endTime) {
        endTime = sjTime(this.endTime);
      }
      if(startTime && endTime && startTime < endTime){
        if (this.activeName == "first") {
          this.option1.requestUrl =
            "/api/v1/tms/getAmsshipDeliveryAttroneyList?apiId=73&status=0&carrierSSOId=" +
            getCookie("userId") +
            "&con=" +
            this.input+
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime;  
        } else{
            this.option2.requestUrl =
              "/api/v1/tms/getAmsshipDeliveryAttroneyList?apiId=73&status=1&carrierSSOId=" +
              getCookie("userId") +
              "&con=" +
              this.input+
              "&startTime=" +
              startTime +
              "&endTime=" +
              endTime;  
        }
      }else{
        if (this.activeName == "first") {
            this.option1.requestUrl =
              "/api/v1/tms/getAmsshipDeliveryAttroneyList?apiId=73&status=0&carrierSSOId=" +
              getCookie("userId") +
              "&con=" +
              this.input;  
        } else{
            this.option2.requestUrl =
              "/api/v1/tms/getAmsshipDeliveryAttroneyList?apiId=73&status=1&carrierSSOId=" +
              getCookie("userId") +
              "&con=" +
              this.input;  
        }
      }

      
    },
    click(pathId) {
      this.$router.push("/path/addDeliveryNotice/" + pathId);
    },
    addClick() {
      this.$router.push("/addDeliveryAttorney/");
    },
    // 生成提货委托书
    addPick() {
      if (this.amsShipDeliveryList.length == 0) {
        this.$message.warning("请勾选已下发的提货委托,方可生成提货委托书");
        return;
      }
      let amsShipDeliveryNameList = {
        amsShipDeliveryNameList: this.amsShipDeliveryList
      
      };
        console.log("list",amsShipDeliveryNameList);
      let test = encodeURIComponent(JSON.stringify(amsShipDeliveryNameList));
      this.$router.push("/attorneytext/" + test);
    },
    //修改
    modifyClick(attorneyId) {
      this.$router.push("/modifyDeliveryAttroney/" + attorneyId);
    },

    deleteclick(scope) {
      let attorneyId = scope;
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          this.axios
            .post("/api/v1/tms/deleteByAttorneyId/" + attorneyId)
            .then(() => {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.option1.requestUrl =
                "/api/v1/tms/getAmsshipDeliveryAttroneyList?apiId=73&status=0&i=" +
                new Date();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消删除!"
          });
        });
    },
    sendClick(scope) {
      let attorneyId = scope;
      this.$confirm("是否下发", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "下发成功"
          });
          this.axios
            .post("/api/v1/tms/sendDeliveryAttorneyStatus/" + attorneyId)
            .then(() => {
              this.option1.requestUrl =
                "/api/v1/tms/getAmsshipDeliveryAttroneyList?apiId=73&status=0&i=" +
                new Date();
              this.option2.requestUrl =
                "/api/v1/tms/getAmsshipDeliveryAttroneyList?apiId=73&status=1&i=" +
                new Date();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "下发失败!"
          });
        });
    },
    refresh(){
      this.$router.go(0);
    }
  }
};
</script>
<style lang="scss" scoped>
.shipTransport {
  .top {
    padding: 1.25rem 0.375rem;
    .el-input {
      width: 20%;
      margin-right: 40rpx;
    }
  }
}
</style>
