//运输派单
<template>
  <div class="transportOrder">
    <div class="transportOrder_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:250px"
      >
      </el-input>
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>
      </el-button>
      <el-button type="primary" @click="Insert">
        <i class="el-icon-plus"></i>新增
      </el-button>
    </div>
    <el-dialog
      title="车辆信息"
      :visible.sync="dialogTableVisible"
      :before-close="handleClose"
    >
      <el-input style="width:250px" v-model="truckText"></el-input>
      <el-button type="primary" @click="getCapacity">查询</el-button>
      <dilTable
        v-bind.sync="truck"
        @radio-change="currentRadioChange1"
      ></dilTable>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateTruckCapacity()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <div class="transportOrder_bottom">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="未接收" name="first">
          <dilTable v-bind.sync="first" ref="table">
            <el-table-column fixed="right" label="操作" width="150">
              <template slot-scope="scope">
                <el-button @click="updateClick(scope)" type="text" size="small">
                  修改
                </el-button>
                <el-button @click="deleteClick(scope)" type="text" size="small">
                  删除
                </el-button>
                <el-button
                  @click="getMatrialDetails1(scope.row)"
                  type="text"
                  size="small"
                >
                  物资详情
                </el-button>
              </template>
            </el-table-column>
            //物资详情抽屉
            <el-table-column type="expand" width="1">
              <template slot-scope="props">
                <el-form
                  label-position="center"
                  inline
                  class="demo-table-expand"
                >
                  <div v-if="false">{{ props }}</div>
                  <div>
                    <el-table :data="tableData" border>
                      <el-table-column
                        v-for="(item, i) in tableHead"
                        :key="i"
                        :prop="item.prop"
                        :label="item.label"
                        :width="item.width"
                      ></el-table-column>
                    </el-table>
                  </div>
                </el-form>
              </template>
            </el-table-column>
          </dilTable>
        </el-tab-pane>
        <!-- 已接收 -->
        <el-tab-pane label="已接收" name="Received">
          <dilTable v-bind.sync="Received" ref="table1">
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button @click="deleteClick(scope)" type="text" size="small">
                  关闭
                </el-button>
                <el-button
                  @click="getMatrialDetails2(scope.row)"
                  type="text"
                  size="small"
                >
                  物资详情
                </el-button>
              </template>
            </el-table-column>
            //物资详情抽屉
            <el-table-column type="expand" width="1">
              <template slot-scope="props">
                <el-form
                  label-position="center"
                  inline
                  class="demo-table-expand"
                >
                  <div v-if="false">{{ props }}</div>
                  <div>
                    <el-table :data="tableData1" border>
                      <el-table-column
                        v-for="(item, i) in tableHead"
                        :key="i"
                        :prop="item.prop"
                        :label="item.label"
                        :width="item.width"
                      ></el-table-column>
                    </el-table>
                  </div>
                </el-form>
              </template>
            </el-table-column>
          </dilTable>
        </el-tab-pane>
        <!-- 已完成 -->
        <el-tab-pane label="已完成" name="completed">
          <dilTable v-bind.sync="completed" ref="table2">
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button
                  @click="getMatrialDetails3(scope.row)"
                  type="text"
                  size="small"
                >
                  物资详情
                </el-button>
              </template>
            </el-table-column>
            //物资详情抽屉
            <el-table-column type="expand" width="1">
              <template slot-scope="props">
                <el-form
                  label-position="center"
                  inline
                  class="demo-table-expand"
                >
                  <div v-if="false">{{ props }}</div>
                  <div>
                    <el-table :data="tableData2" border>
                      <el-table-column
                        v-for="(item, i) in tableHead"
                        :key="i"
                        :prop="item.prop"
                        :label="item.label"
                        :width="item.width"
                      ></el-table-column>
                    </el-table>
                  </div>
                </el-form>
              </template>
            </el-table-column>
          </dilTable>
        </el-tab-pane>
        <!-- 已拒绝 -->
        <el-tab-pane label="已拒绝" name="refused">
          <dilTable v-bind.sync="refused">
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button @click="ctrlZClick(scope)" type="text" size="small">
                  撤销
                </el-button>
              </template>
            </el-table-column>
          </dilTable>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getCookie } from '@/utils/util.js'
import { sjTime } from '@/utils/sharedJsFile'
export default {
  data() {
    return {
      startTime: null,
      endTime: null,
      carrierUserId: '',
      //模态窗的框计算
      inputText: null,
      //车辆输入框
      truckText: null,
      //是否弹出对话框
      dialogTableVisible: false,
      //输入框的值
      input: null,
      //选项卡的当前选中的值
      activeName: 'first',
      //已下发的表格
      first: {
        requestUrl: ''
      },
      //车辆信息的表格
      truck: {
        requestUrl: '',
        // 控制显示当选列
        selectionType: 'radio'
      },
      Received: {
        requestUrl: ''
      },
      completed: {
        requestUrl: ''
      },
      refused: {
        requestUrl: ''
      },
      //车辆的表格
      truck: {
        requestUrl: '',
        selectionType: 'radio'
      },
      //记录旧的row对象
      oldRow: '',
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态
      oldRowCount: 1,
      //记录旧的row对象
      oldRow1: '',
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态
      oldRowCount1: 1,
      oldRow2: '',
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态
      oldRowCount2: 1,
      tableHead: [
        {
          prop: 'materialName',
          label: '物资名称',
          width: 150
        },
        {
          prop: 'specificationModel',
          label: '规格型号',
          width: 150
        },
        {
          prop: 'materialNumber',
          label: '物资件数',
          width: 100
        },
        {
          prop: 'materialWeight',
          label: '物资重量',
          width: 100
        }
      ],
      tableData: [],
      tableData1: [],
      tableData2: [],
      //当前选中的订单id
      selectOrderId: null,
      //选中的车辆号
      capacityNumber: '',
      //选中的运力id
      capacityId: null,
      //是否关闭模态框
      drawer: false,
      //模态框从左往右打开
      direction: 'rtl',

      orderId: null,
      //多选的选中的订单id
      selectionList: [],
      //区别点击的是添加运力，还是修改
      index: null
    }
  },
  created() {
    if (getCookie('orgCode') == 'chengyunshang') {
      this.truck.requestUrl =
        '/api/v1/uc/getAllCapacityByCarrierLike?apiId=429&carrierSsoId=' +
        getCookie('userId')
    } else {
      this.truck.requestUrl =
        '/api/v1/uc/getAllCapacityByCarrierLike?apiId=429&carrierSsoId=' + null
    }
    if (getCookie('orgCode') == 'chengyunshang') {
      this.first.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=4&carrierId=' +
        getCookie('userId')
      this.Received.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=5&carrierId=' +
        getCookie('userId')
      this.completed.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=2&carrierId=' +
        getCookie('userId')
      this.refused.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=6&carrierId=' +
        getCookie('userId')
    } else if (
      getCookie('orgCode') == 'dagangadmin' ||
      getCookie('orgCode') == 'zidonghuabu' ||
      getCookie('orgCode') == 'wuliuchuyunzhongxin'
    ) {
      this.first.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=4'
      this.Received.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=5'
      this.completed.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=2'
      this.refused.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=6'
    } else {
      this.first.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=4&userId=' +
        getCookie('orgCode')
      this.Received.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=5&userId=' +
        getCookie('orgCode')
      this.completed.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=2&userId=' +
        getCookie('orgCode')
      this.refused.requestUrl =
        '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=6&userId=' +
        getCookie('orgCode')
    }
  },
  methods: {
    //撤销已拒绝的运输订单
    ctrlZClick(scope) {
      console.log(scope.row.orderId)
      this.axios
        .post('/api/v1/oms/refuseCtrlZ', {
          orderId: scope.row.orderId
        })
        .then(res => {
          if (res.data.code == '200') {
            this.$message.success('撤单成功')
            this.getRequestUrl()
          }
        })
    },
    handleClose(done) {
      done()
      this.$message.info('取消修改运力')
    },
    onclick() {
      let startTime = null
      let endTime = null
      if (this.startTime && this.endTime) {
        startTime = sjTime(this.startTime)
        endTime = sjTime(this.endTime)
      }
      if (getCookie('orgCode') == 'chengyunshang') {
        this.first.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=4&carrierId=' +
          getCookie('userId') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
        this.Received.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=5&carrierId=' +
          getCookie('userId') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
        this.completed.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=2&carrierId=' +
          getCookie('userId') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
        this.refused.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=6&carrierId=' +
          getCookie('userId') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
      } else if (
        getCookie('orgCode') == 'dagangadmin' ||
        getCookie('orgCode') == 'zidonghuabu' ||
        getCookie('orgCode') == 'wuliuchuyunzhongxin'
      ) {
        this.first.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=4' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
        this.Received.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=5' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
        this.completed.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=2' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
        this.refused.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=6' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
      } else {
        this.first.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=4&userId=' +
          getCookie('orgCode') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
        this.Received.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=5&userId=' +
          getCookie('orgCode') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
        this.completed.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=2&userId=' +
          getCookie('orgCode') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
        this.refused.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=6&userId=' +
          getCookie('orgCode') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&con=' +
          this.input +
          '&test=' +
          new Date()
      }
    },
    getRequestUrl() {
      if (getCookie('orgCode') == 'chengyunshang') {
        this.first.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=4&carrierId=' +
          getCookie('userId') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
        this.Received.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=5&carrierId=' +
          getCookie('userId') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
        this.completed.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=2&carrierId=' +
          getCookie('userId') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
        this.refused.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=6&carrierId=' +
          getCookie('userId') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
      } else if (
        getCookie('orgCode') == 'dagangadmin' ||
        getCookie('orgCode') == 'zidonghuabu' ||
        getCookie('orgCode') == 'wuliuchuyunzhongxin'
      ) {
        this.first.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=4' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
        this.Received.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=5' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
        this.completed.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=2' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
        this.refused.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=6' +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
      } else {
        this.first.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=4&userId=' +
          getCookie('orgCode') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
        this.Received.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=5&userId=' +
          getCookie('orgCode') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
        this.completed.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=2&userId=' +
          getCookie('orgCode') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
        this.refused.requestUrl =
          '/api/v1/oms/getOthersOrderMesToSend?apiId=386&type=1&orderStatus=6&userId=' +
          getCookie('orgCode') +
          '&startTime=' +
          startTime +
          '&endTime=' +
          endTime +
          '&test=' +
          new Date()
      }
    },
    getTruckRequestUrl() {
      if (getCookie('orgCode') == 'chengyunshang') {
        this.truck.requestUrl =
          '/api/v1/uc/getAllCapacityByCarrierLike?apiId=429&carrierSsoId=' +
          getCookie('userId') +
          '&test=' +
          new Date()
      } else {
        this.truck.requestUrl =
          '/api/v1/uc/getAllCapacityByCarrierLike?apiId=429&carrierSsoId=' +
          null +
          '&test=' +
          new Date()
      }
    },
    currentRadioChange1(row) {
      console.log(row)
      this.capacityId = row.capacityId
    },
    //删除运输订单
    deleteClick(scope) {
      let map = {
        orderId: scope.row.orderId,
        userId: getCookie('userId')
      }
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.axios.post('/api/v1/oms/closeOmstruckOrder', map).then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getRequestUrl()
            } else {
              this.$message({
                message: '删除失败',
                type: 'warning'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //修改运输订单
    updateClick(scope) {
      this.getTruckRequestUrl()
      this.orderId = scope.row.orderId
      this.dialogTableVisible = true
      console.log(scope.row.orderId)
    },
    updateTruckCapacity() {
      if (this.capacityId == null) {
        this.$message.error('未选中运力')
        return
      }
      this.axios
        .post('/api/v1/oms/updateCapacityId', {
          capacityId: this.capacityId,
          orderId: this.orderId
        })
        .then(res => {
          if (res.data.code == '200') {
            this.$message.success('修改运力成功')
            this.getRequestUrl()
            this.dialogTableVisible = false
          } else {
            this.$message.error('修改运力失败')
          }
        })
    },
    handleClick(tab, event) {
      this.getRequestUrl()
    },
    Insert() {
      this.$router.push('addOldSporadicOrder')
    },
    getCapacity() {
      if (this.truckText != null) {
        if (getCookie('orgCode') == 'chengyunshang') {
          this.truck.requestUrl =
            '/api/v1/uc/getAllCapacityByCarrierLike?apiId=429&carrierSsoId=' +
            getCookie('userId') +
            '&index=' +
            this.truckText
        } else {
          this.truck.requestUrl =
            '/api/v1/uc/getAllCapacityByCarrierLike?apiId=429&carrierSsoId=' +
            null +
            '&index=' +
            this.truckText
        }
        this.truckText = null
      } else {
        if (getCookie('orgCode') == 'chengyunshang') {
          this.truck.requestUrl =
            '/api/v1/uc/getAllCapacityByCarrierLike?apiId=429&carrierSsoId=' +
            getCookie('userId') +
            '&i=' +
            new Date()
        } else {
          this.truck.requestUrl =
            '/api/v1/uc/getAllCapacityByCarrierLike?apiId=429&carrierSsoId=' +
            null +
            '&i=' +
            new Date()
        }
      }
    },
    //获取物资详情--未接收
    getMatrialDetails1(row) {
      console.log(row)
      // 记录重复点击次数
      if (this.oldRow === row) {
        this.oldRowCount += 1
      }
      // 切换当前详情表
      this.$refs.table.toggleRowExpansion(row)
      // 打开前关闭上一个详情表
      if (this.oldRow != '') {
        if (this.oldRow != row) {
          if (this.oldRowCount % 2 === 1) {
            this.$refs.table.toggleRowExpansion(this.oldRow)
          } else {
            this.oldRowCount = 1
          }
        } else {
          this.oldRow = null
          return
        }
      }
      // 重置上一个点击对象
      this.oldRow = row
      this.axios
        .post('/api/v1/ams/getSporadic?sporadicId=' + row.sporadicOrderId)
        .then(res => {
          console.log(res.data.data)
          this.tableData = res.data.data
        })
    },
    //获取物资详情--已接收
    getMatrialDetails2(row) {
      console.log(row)
      // 记录重复点击次数
      if (this.oldRow1 === row) {
        this.oldRowCount1 += 1
      }
      // 切换当前详情表
      this.$refs.table1.toggleRowExpansion(row)
      // 打开前关闭上一个详情表
      if (this.oldRow1 != '') {
        if (this.oldRow1 != row) {
          if (this.oldRowCount1 % 2 === 1) {
            this.$refs.table1.toggleRowExpansion(this.oldRow1)
          } else {
            this.oldRowCount1 = 1
          }
        } else {
          this.oldRow1 = null
          return
        }
      }
      // 重置上一个点击对象
      this.oldRow1 = row
      this.axios
        .post('/api/v1/ams/getSporadic?sporadicId=' + row.sporadicOrderId)
        .then(res => {
          console.log(res.data.data)
          this.tableData1 = res.data.data
        })
    },
    //获取物资详情--已完成
    getMatrialDetails3(row) {
      console.log(row)
      // 记录重复点击次数
      if (this.oldRow2 === row) {
        this.oldRowCount2 += 1
      }
      // 切换当前详情表
      this.$refs.table2.toggleRowExpansion(row)
      // 打开前关闭上一个详情表
      if (this.oldRow2 != '') {
        if (this.oldRow2 != row) {
          if (this.oldRowCount2 % 2 === 1) {
            this.$refs.table2.toggleRowExpansion(this.oldRow2)
          } else {
            this.oldRowCount2 = 1
          }
        } else {
          this.oldRow2 = null
          return
        }
      }
      // 重置上一个点击对象
      this.oldRow2 = row
      this.axios
        .post('/api/v1/ams/getSporadic?sporadicId=' + row.sporadicOrderId)
        .then(res => {
          this.tableData2 = res.data.data
        })
    }
  }
}
</script>

<style lang="scss">
.transportOrder {
  .transportOrder_top {
    margin-left: 20px;
    margin-top: 20px;
  }
  .transportOrder_bottom {
    margin-left: 20px;
    margin-top: 20px;
  }
  .drawer_top {
    width: 100%;
    height: 50px;
    padding-left: 20px;
    display: flex;
    align-items: center;
  }
}
</style>
