<template>
  <!-- 辅料运输预约 -->
  <div >
    <div style="margin:10px">
      <el-form>
        <el-form-item
          >
           <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 class="text" v-if="activeName == 'second'">下发时间：</span>
          <el-date-picker
            v-if="activeName == 'second'"
            v-model="startTime"
            type="datetime"
            placeholder="选择日期"
            style="width:200px"
          >
          </el-date-picker>
          <span class="text" v-if="activeName == 'second'">至</span>
          <el-date-picker
            v-if="activeName == 'second'"
            v-model="endTime"
            type="datetime"
            placeholder="选择日期"
            style="width:200px"
          >
          </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="insertClick">
            <i class="el-icon-plus"></i>新增
          </el-button>
          <el-input
            class="input"
            placeholder="请从表格复制采购订单号"
            v-model="inputPurOrder"
            style="width:200px"
            clearable
            v-if="activeName == 'second'"
          >
          </el-input>
          <el-button
            type="primary"
            class="btn"
            @click="closePurOrder"
            v-if="activeName == 'second'"
          >
            关闭一车多趟
          </el-button>
          <el-button
            type="primary"
            class="btn"
            @click="issue"
            v-if="activeName == 'first'"
          >
            <i class="el-icon-plus"></i>下发
          </el-button></el-form-item
        >
      </el-form>
    </div>
    <el-dialog title="提醒" :visible.sync="dialogVisible" width="30%">
      <div v-if="this.isMoreTrips != 1" style="color: red">
        <span>该订单不是一车多趟的订单</span>
      </div>
      <div>
        <span>采购订单号:</span>
        <span>{{ inputPurOrder }}</span>
      </div>
      <div>
        <span>发货单位:</span>
        <span>{{ supplierName }}</span>
      </div>
      <div>
        <span>物资名称:</span>
        <span>{{ materialName }}</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="closePurOrder1"
          v-if="this.isMoreTrips == 1"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <div class="tab">
      <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"
              align="center"
              label="操作"
              width="120"
            >
              <template slot-scope="scope">
                <el-button
                  @click="sendClick(scope.row.orderId)"
                  type="text"
                  size="small"
                >
                  下发
                </el-button>
                <el-button
                  @click="updateClick(scope.row.orderId)"
                  type="text"
                  size="small"
                >
                  修改
                </el-button>
                <el-button
                  @click="
                    deleteClick(scope.row.orderId, scope.row.capacityNumber)
                  "
                  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" @func="func"> 
             <el-table-column
              fixed="right"
              align="center"
              label="操作"
              width="120"
            >
              <template slot-scope="scope">
                <el-button
                  @click="openDraw(scope.row)"
                  type="text"
                  size="small"
                >
                  修改线路
                </el-button>
              </template>
            </el-table-column>
          </dilTable>
        </el-tab-pane>
      </el-tabs>
    </div>
    <el-drawer :visible.sync="drawer" :direction="direction" size="45%">
      <div class="drawer_top">
        <el-input
          placeholder="请输入内容"
          v-model="input"
          style="margin-top: 10px; margin-right: 10px; width: 250px"
          clearable
        ></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="updateLineId">
          <i class="el-icon-confirm"></i>确定修改
        </el-button>
      </div>
      <div>
        <dilTable
          v-bind.sync="line"
           @radio-change="redioChange"
          :drawer="drawer"
        ></dilTable>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { getCookie } from '@/utils/util.js'
import { sjTime } from '@/utils/sharedJsFile'
export default {
  name: 'inplantTMS',
  data() {
    return {
      condition1:null,
      condition2:null,
      input1:null,
      input2:null,
      options:[
          {
          label:"车牌号",
          value:"capacityNumber"
          },
          {
          label:"物资名称",
          value:"materialName"
          },
          {
          label:"发货单位",
          value:"supplierName"
          }
      ],
      input: '',
      Time: '',
      startTime: null,
      endTime: null,
      activeName: 'first',
      dialogVisible: false,
      materialName: null,
      supplierName: null,
      inputPurOrder: null,
      option1: {
        // 表格请求数据的地址
        requestUrl: '',
        selectionType: 'select',
        mapList: []
      },
      isMoreTrips: null,
      option2: {
        // 表格请求数据的地址
        requestUrl: ''
      },
      lineName: null,
      lineId: null,
      purOrderList: [],
      drawer:false,
      direction: 'rtl',
      orderDetail:{},
      line: {
        requestUrl: '',
        selectionType: 'radio'
      },
    }
  },
  created() {
    //判断是否是承运商
    if (getCookie('orgCode') == 'chengyunshang') {
      this.option1.requestUrl =
        '/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=' +
        getCookie('userId')
      this.option2.requestUrl =
        '/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=' +
        getCookie('userId')
    } else {
      this.option1.requestUrl =
        '/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=' +
        null
      this.option2.requestUrl =
        '/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=' +
        null
    }
  },
  methods: {
    handleClick() {
      this.getRequestUrl()
    },
    getRequestUrl() {
      //判断是否是承运商
      if (getCookie('orgCode') == 'chengyunshang') {
        this.option1.requestUrl =
          '/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=' +
          getCookie('userId') +
          '&i=' +
          new Date()
        this.option2.requestUrl =
          '/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=' +
          getCookie('userId') +
          '&i=' +
          new Date()
      } else {
        this.option1.requestUrl =
          '/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=' +
          null +
          '&i=' +
          new Date()
        this.option2.requestUrl =
          '/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=' +
          null +
          '&i=' +
          new Date()
      }
    },
    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+'']
        }
      if (this.activeName == 'first') {
        //判断是否是承运商
        if (getCookie('orgCode') == 'chengyunshang') {
          this.option1.requestUrl =
            '/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=' +
            getCookie('userId') +
            '&con=' +
            this.input +
            '&i=' +
            new Date()
        } else {
          this.option1.requestUrl =
            '/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&carrierSSOId=' +
            null +
            '&con=' +
            this.input +
            '&i=' +
            new Date()
        }
        this.option1.requestQuery=requestQuery;
      } else {
        let startTime = null
        let endTime = null
        if (this.startTime) {
          startTime = sjTime(this.startTime)
        }
        if (this.endTime) {
          endTime = sjTime(this.endTime)
        }
        if (startTime && endTime) {
          if (startTime < endTime) {
            //判断是否是承运商
            if (getCookie('orgCode') == 'chengyunshang') {
              this.option2.requestUrl =
                '/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=' +
                getCookie('userId') +
                '&con=' +
                this.input +
                '&startTime=' +
                startTime +
                '&endTime=' +
                endTime +
                '&i=' +
                new Date()
            } else {
              this.option2.requestUrl =
                '/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=' +
                null +
                '&con=' +
                this.input +
                '&startTime=' +
                startTime +
                '&endTime=' +
                endTime +
                '&i=' +
                new Date()
            }
          }
        } else {
          if (getCookie('orgCode') == 'chengyunshang') {
            this.option2.requestUrl =
              '/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=' +
              getCookie('userId') +
              '&con=' +
              this.input +
              '&i=' +
              new Date()
          } else {
            this.option2.requestUrl =
              '/api/v1/oms/getAllTruckOrder?apiId=243&orderStatus=111&orderType=5&carrierSSOId=' +
              null +
              '&con=' +
              this.input +
              '&i=' +
              new Date()
          }
        }
        this.option2.requestQuery=requestQuery;
      }
      this.$forceUpdate();
    },
    selectionChange(selection) {
      this.mapList = selection
    },
    closePurOrder() {
      if (this.inputPurOrder == null) {
        this.$message.warning('请输入需要关闭一车多趟的采购订单号')
        return
      }
      this.purOrderList.forEach(e => {
        if (e.purchaseOrderNo == this.inputPurOrder) {
          this.materialName = e.materialName
          this.supplierName = e.supplierName
          this.isMoreTrips = e.isMoreTrips
        }
      })
      this.dialogVisible = true
    },
    closePurOrder1() {
      this.axios
        .post(
          '/api/v1/bp/closePurOrderMoreTrips?purchaseOrderNo=' +
            this.inputPurOrder
        )
        .then(res => {
          console.log(res)
          if (res.data.code == '200') {
            this.$message.success('关闭成功')
          } else {
            this.$message.error(res.data.data)
          }
        })
      this.dialogVisible = false
    },
    func(res) {
      this.purOrderList = res.list
      console.log(res.list)
    },
    issue() {
      if (typeof this.mapList == 'undefined' || this.mapList.length == 0) {
        this.$message({
          type: 'warning',
          duration: '2000',
          offset: '250',
          message: '请勾选订单'
        })
        return
      }
      let mapvalue = {
        mapList: this.mapList
      }
      this.axios.post('/api/v1/oms/dispatchOrder', mapvalue).then(() => {
        this.$message({
          type: 'success',
          message: '下发成功!'
        })
        this.option1.requestUrl =
          '/api/v1/oms/getAllTruckOrder?apiId=141&orderStatus=3&orderType=5&i=' +
          new Date()
      })
    },
    // 下发
    sendClick(orderId) {
      let mapvalue = {
        mapList: [{ orderId: orderId }]
      }
      this.$confirm('是否下发', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(() => {
          this.axios.post('/api/v1/oms/dispatchOrder', mapvalue).then(() => {
            this.$message({
              type: 'success',
              message: '下发成功!'
            })
            this.getRequestUrl()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消下发!'
          })
        })
    },
    // 删除
    deleteClick(orderId, capacityNumber) {
      this.$confirm('是否删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.axios
            .post('/api/v1/oms/deleteOrder', {
              orderId: orderId,
              capacityNumber: capacityNumber
            })
            .then(() => {
              this.getRequestUrl()
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消删除!'
          })
        })
    },
    //新增
    insertClick() {
      this.$router.push('/transportReserveFuAddIngredients/')
    },
    //修改
    updateClick(orderId) {
      this.$router.push('/transportReserveFuEdit/' + orderId)
    },
    redioChange(selection) {
      this.lineName = selection.lineName
      this.lineId = selection.lineId
    },
    openDraw(row){
      this.orderDetail=row;
      this.lineName = null
      this.lineId = null
      this.drawer = true;
      this.line.requestUrl =
      '/api/v1/rms/getAllLineDesk?apiId=249&lineType=3&con=辅料&i=' +
      new Date()
    },
    updateLineId(){
      this.orderDetail.lineId=this.lineId;
      if(!this.orderDetail.lineId){
        this.$message.warning("请选择修改线路");
      }
      this.axios.post("/api/v1/tms/changeOrderLine",this.orderDetail).then((res)=>{
        console.log(res);
        if(res.data.status && res.data.status != "succeed"){
          this.$message.error(res.data.data);
        }else{
          this.$message.success("修改成功");
        }
      });
    }
  }
}
</script>
<style lang="scss" scoped>
.transportReserveFu {
  .top {
    margin-left: 20px;
    margin-top: 20px;
    .btn {
      margin-right: 10px;
    }
  }
  .tab {
    margin-left: 20px;
    margin-top: 20px;
  }
}
</style>
