// 分解计划
<template>
  <div>
    <div style="margin:10px">
      <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>需求时间：</span>
      <el-date-picker
        v-model="startTime"
        type="datetime"
        placeholder="选择日期"
        style="width:220px"
      >
      </el-date-picker>
      <span>至</span>
      <el-date-picker
        v-model="endTime"
        type="datetime"
        placeholder="选择日期"
        style="width:220px"
      >
      </el-date-picker>
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>查询
      </el-button>
    </div>
    <template>
      <div>
        <el-tabs v-model="activeName">
          <el-tab-pane label="待分解" name="second">
            <mergeRowTable v-bind.sync="second" ref="table1" max-height="300">
              <el-table-column fixed="right" label="操作" width="100">
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    size="mini"
                    @click="desponsePlan(scope)"
                  >
                    分解
                  </el-button>
                </template>
              </el-table-column>
            </mergeRowTable>
          </el-tab-pane>
          <el-tab-pane label="已分解" name="third">
            <mergeRowTable v-bind.sync="third" ref="table2" max-height="300">
              <el-table-column fixed="right" label="操作" width="180">
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    size="mini"
                    @click="desponsePlan(scope)"
                  >
                    继续分解
                  </el-button>
                  <el-button
                    type="text"
                    size="mini"
                    @click="getPlanOrder(scope)"
                  >
                    运单
                  </el-button>
                  <el-button
                    type="text"
                    size="mini"
                    @click="closePlanOrder(scope)"
                  >
                    关闭计划
                  </el-button>
                </template>
              </el-table-column>
            </mergeRowTable>
          </el-tab-pane>
          <el-tab-pane label="已完成" name="four">
            <mergeRowTable v-bind.sync="four" ref="table3" max-height="300">
            </mergeRowTable>
          </el-tab-pane>
        </el-tabs>
      </div>
    </template>
  </div>
</template>
<script>
import { getCookie } from '@/utils/util.js'
import { sjTime } from '@/utils/sharedJsFile'
export default {
  data() {
    return {
      condition1: null,
      condition2: null,
      input1: null,
      input2: null,
      options: [
        {
          label: '用车单位',
          value: 'shipperName'
        },
        {
          label: '物资名称',
          value: 'materialName'
        },
        {
          label: '装货点',
          value: 'loadName'
        },
        {
          label: '卸货点',
          value: 'unloadName'
        }
      ],
      endTime: null,
      startTime: null,
      inputText: '',
      carrierId: 40,
      second: {
        columnIndexs: [0, 1, 2, 3, 4, 5, 9.1, 21, 22],
        comparison: 'requirementNumber',
        // second请求数据的地址
        requestUrl: ''
      },
      third: {
        columnIndexs: [0, 1, 2, 3, 4, 5, 9.1, 21, 22],
        comparison: 'requirementNumber',
        // second请求数据的地址
        requestUrl: ''
      },
      four: {
        requestUrl: ''
      },
      activeName: 'second',
      tableData: []
    }
  },
  created() {
    if (getCookie('orgCode') == 'chengyunshang') {
      this.second.requestUrl =
        '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=2&carrierId=' +
        getCookie('userId')
      this.third.requestUrl =
        '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=3&carrierId=' +
        getCookie('userId')
      this.four.requestUrl =
        '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=4&carrierId=' +
        getCookie('userId')
    } else {
      this.second.requestUrl =
        '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=2'
      this.third.requestUrl =
        '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=3'
      this.four.requestUrl =
        '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=4'
    }
  },
  methods: {
    handleClick(tab, event) {
      console.log('zhix')
      this.getRequestUrl()
    },
    onclick() {
      let startTime = null
      let endTime = null
      if (this.startTime && this.endTime) {
        startTime = sjTime(this.startTime)
        endTime = sjTime(this.endTime)
      }
      let requestQuery = {
        startTime: startTime,
        endTime: endTime
      }
      if (this.condition1 == this.condition2) {
        requestQuery[this.condition1] = [this.input1, this.input2]
      } else {
        requestQuery[this.condition1] = [this.input1 + '']
        requestQuery[this.condition2] = [this.input2 + '']
      }
      this.second.requestQuery = requestQuery
      this.third.requestQuery = requestQuery
      this.four.requestQuery = requestQuery
      if (getCookie('orgCode') == 'chengyunshang') {
        this.second.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=2&carrierId=' +
          getCookie('userId')
        this.third.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=3&carrierId=' +
          getCookie('userId')
        this.four.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=4&carrierId=' +
          getCookie('userId')
      } else {
        this.second.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=2'
        this.third.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=3'
        this.four.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=4'
      }
      this.$forceUpdate()
    },
    getRequestUrl() {
      if (getCookie('orgCode') == 'chengyunshang') {
        this.second.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=2&carrierId=' +
          getCookie('userId') +
          '&i=' +
          new Date()
        this.third.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=3&carrierId=' +
          getCookie('userId') +
          '&i=' +
          new Date()
        this.four.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=4&carrierId=' +
          getCookie('userId') +
          '&i=' +
          new Date()
      } else {
        this.second.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=2' +
          '&i=' +
          new Date()
        this.third.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=3' +
          '&i=' +
          new Date()
        this.four.requestUrl =
          '/api/v1/ams/getTruckPlanList?apiId=258&planStatus=4' +
          '&i=' +
          new Date()
      }
    },
    closePlanOrder(scope) {
      console.log(scope.row.planId)
      this.$confirm(
        '确定关闭该计划吗?该操作会将计划下未接收订单全部关闭',
        '是否继续?',
        {
          center: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }
      ).then(() => {
        this.axios
          .post('/api/v1/oms/closeInwardOrderByPlan?planId=' + scope.row.planId)
          .then(res => {
            if (res.data.code == '200') {
              this.$message.success('关闭成功')
              this.getRequestUrl()
            } else {
              this.$message.error('关闭失败')
            }
          })
          .catch(() => {
            this.$message.error('关闭失败')
          })
      })
    },
    receiver(scope) {
      this.$confirm('是否接收', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(() => {
          this.axios
            .post('/api/v1/ams/receiveInwardPlan/' + scope.row.planId)
            .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: '接收操作已取消!'
          })
        })
    },
    selectionChange(selection) {
      this.second.mapList = selection
    },
    desponsePlan(scope) {
      this.$router.push('/decomposePlan/' + scope.row.planId)
    },
    getPlanOrder(scope) {
      this.$router.push('/getPlanOrder/' + scope.row.planId)
    },
    Issue() {
      console.log(this.first.mapList)
      this.$confirm('是否下发', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(() => {
          this.axios
            .post(
              '/api/v1/ams/addFixedAmsDispatchSaleOrder',
              this.first.mapList
            )
            .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: '取消下发!'
          })
        })
    }
  }
}
</script>
<style lang="scss">
.inwardDisPlan {
  margin-top: 20px;
  margin-left: 20px;
  .sache {
    padding: 1.25rem 0.375rem;
    .el-input {
      width: 20%;
      margin-right: 1.25rem;
    }
  }
}
</style>
