//运输预约展现
<template>
  <!-- 零星物资运输预约 -->
  <div class="homeworkPath">
    <div class="top">
      <el-input placeholder="请输入内容" v-model="input" clearable> </el-input>
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>查询
      </el-button>
      <el-button
        type="primary"
        @click="sendClick()"
        v-if="this.activeName == 'first'"
      >
        <i class="el-icon-bottom"></i>下发
      </el-button>
    </div>
<el-dialog title="选择承运商" :visible.sync="dialogFormVisible">
    <el-form
        :inline="true"
        class="demo-form-inline"
        label-width="80px"
      >
        <el-form-item label="选择承运商">
         <el-autocomplete
          class="inline-input"
          v-model="stateCarrier"
          :fetch-suggestions="querySearchCarrier"
          placeholder="请输入承运商名称"
          :trigger-on-focus="false"
          @select="handleSelectCarrier"
        >
        <template slot-scope="{ item }">
          <div class="name">{{ item.carrierName }}</div>
        </template>
        </el-autocomplete>
        </el-form-item>
        </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="makeSure">确 定</el-button>
  </div>
</el-dialog>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <!-- 未下发 -->
      <el-tab-pane label="未下发" name="first">
        <dilTable
          v-bind.sync="option1"
          ref="table"
          @selection-change="selectionChange"
        >
          <el-table-column
            fixed="right"
            label="操作"
            width="120"
          >
            <template slot-scope="scope">
              <el-button @click="addClick(scope,1)" type="text" size="small">
                添加
              </el-button>
               <el-button @click="addClick(scope,2)" 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">
        </dilTable>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getCookie } from "@/utils/util.js";
export default {
  name: "inplantTMS",
  data() {
    return {
      index:null,
      likeCarrierLike:'',
      restaurantsCarrier: [],
      stateCarrier:null,
      dialogFormVisible: false,
      input: null,
      Time: "",
      sporadicOrderId:null,
      activeName: "first",
      option1: {
        // 表格请求数据的地址
        requestUrl:
          "",
        // 控制显示多选列
        selectionType: "select",
      },
      option2: {
        // 表格请求数据的地址
        requestUrl:
          "",
      },
      selection: [],
      maplist:[]
    };
  },
  created(){
        this.option1.requestUrl = "/api/v1/ams/getSporadicOrdersList?apiId=384&orderType=15&issueStatus=1&sporadicStatus=3",
        this.option2.requestUrl = "/api/v1/ams/getSporadicOrdersList?apiId=384&issueStatus=1&orderType=15&sporadicStatus=1&userIds=" + getCookie("userId")        
  },
  methods: {
    makeSure(){
        console.log(this.carrierId)
        this.axios.post("/api/v1/ams/addInwardSporadicOrder",{
            sporadicOrderId:this.sporadicOrderId,
            carrierId:this.carrierId,
            userId:getCookie("userId")
        }).then((res) => {
            if(res.data.code == '200'){
                console.log(res)
                this.sporadicOrderId = null
                this.carrierId = null
                if(this.index == 1){
                    this.$message.success("添加成功")
                }else{
                    this.$message.success("修改成功")
                }
            }
        })
        this.dialogFormVisible = false
    },
    selectionChange(selection){

    },
    addClick(scope,index){
        console.log(index)
        this.index = index
        this.sporadicOrderId = scope.row.sporadicOrderId
        console.log(scope.row.sporadicOrderId)
        this.dialogFormVisible = true
    },
    getRequestUrl(){
      if(getCookie("orgCode") == "dagangadmin"){
            this.option1.requestUrl = "/api/v1/ams/getSporadicOrdersList?apiId=428&issueStatus=0&orderType=15&dispatchStatus=0&test=" + new Date(),
            this.option2.requestUrl = "/api/v1/ams/getSporadicOrdersList?apiId=428&issueStatus=1&orderType=15&sporadicStatus=1&test=" + new Date()
            console.log(this.option1.requestUrl)
            console.log(this.option2.requestUrl)
      }else{
            this.option1.requestUrl = "/api/v1/ams/getSporadicOrdersList?apiId=428&issueStatus=0&orderType=15&dispatchStatus=0&userId=" + getCookie("userId") + "&test=" + new Date(),
            this.option2.requestUrl = "/api/v1/ams/getSporadicOrdersList?apiId=428&issueStatus=1&orderType=15&sporadicStatus=1&userId=" + getCookie("userId") + "&test=" + new Date()
            console.log(this.option1.requestUrl)
            console.log(this.option2.requestUrl)
     }
    },
    //承运商弹出层
    handleSelectCarrier(item){    
      this.carrierId = item.carrierId
      item.carrierName = this.stateCarrier
    },
    //以下是承运商边输边查搜索
    querySearchCarrier(queryString, cb) {
        this.axios.post('/api/v1/uc/getCarrierMesByLike?index='+queryString).then((res)=>{
        if(res.data.code == "200"){
          var restaurantsCarrier = res.data.data
          console.log(restaurantsCarrier,"restaurantsCarrier");
          var results = queryString ? restaurantsCarrier.filter(this.createFilterCarrier(queryString)) :restaurantsCarrier;
           // 调用 callback 返回建议列表的数据
          cb(results);
        }
        })     
      },
    createFilterCarrier(queryString) {
        return (restaurantsCarrier)  =>  {
          return (restaurantsCarrier.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1);
        };
      },
    //以上是承运商边输边查搜索
    onclick() {
      if(this.input != null){
        if(getCookie("orgCode") == "dagangadmin")
        {
          if(this.activeName == "first"){
            this.option1.requestUrl = "/api/v1/ams/getSporadicOrdersList?apiId=428&issueStatus=0&orderType=15&dispatchStatus=0&con=" + this.input
          }else{
            this.option2.requestUrl = "/api/v1/ams/getSporadicOrdersList?apiId=428&issueStatus=1&orderType=15&sporadicStatus=1&con=" + this.input
          }                
        }else
        {
          if(this.activeName == "first"){
            this.option1.requestUrl = "/api/v1/ams/getSporadicOrdersList?apiId=428&issueStatus=0&orderType=15&dispatchStatus=0&userId=" + getCookie("userId") + "&con=" + this.input
          }else{
            this.option2.requestUrl = "/api/v1/ams/getSporadicOrdersList?apiId=428&issueStatus=1&orderType=15&sporadicStatus=1&userId=" + getCookie("userId") + "&con=" + this.input
          }       
        }
        }else{
        this.$message.warning("未输入查询条件")
        }
    },
    handleClick(tab, event) {
    //   this.getRequestUrl()
    },
    // 下发
    sendClick() {
      if(this.maplist.length == 0){
        this.$message.warning('未选择零星订单')
        return
      }
      this.axios
        .post("/api/v1/ams/issueSporadicOrders", this.maplist)
        .then((res) => {
          if (res.data.code == "200") {
            this.$router.go(0)
            this.$message({
              type: "success",
              message: "下发成功!",
            });
            this.maplist = []
          }
        });
    },
  },
};
</script>
<style lang='scss' scoped>
.homeworkPath {
  .top {
    padding: 40px;
    .el-input {
      width: 250px;
      margin-right: 20px;
    }
  }
}
</style>