<template>
  <div class="mod-config">
    <el-form :inline="true" ref="dataForm" :rules="dataRule" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="dataForm.idenCardNum" filterable placeholder="标识卡号">
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.orderNo" filterable placeholder="订单号">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="获取时间">
      </el-table-column>
      <el-table-column
        prop="idenCardNum"
        header-align="center"
        align="center"
        label="标识卡号">
      </el-table-column>
      <el-table-column
        prop="orderNo"
        header-align="center"
        align="center"
        label="订单号">
      </el-table-column>
      <el-table-column
        prop="orderSource"
        header-align="center"
        align="center"
        label="任务来源">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="任务状态">
      </el-table-column>
      <el-table-column
        prop="paramKey"
        header-align="center"
        align="center"
        label="订单状态">
      </el-table-column>
      <el-table-column
        prop="customerName"
        header-align="center"
        align="center"
        label="客户名称">
      </el-table-column>
      <el-table-column
        prop="productName"
        header-align="center"
        align="center"
        label="产品名称">
      </el-table-column>
      <el-table-column
        prop="materialDes"
        header-align="center"
        align="center"
        label="物料名称">
      </el-table-column>
      <el-table-column
        prop="quantity"
        header-align="center"
        align="center"
        label="物料数量">
      </el-table-column>
      <el-table-column
        prop="weight"
        header-align="center"
        align="center"
        label="物料重量">
      </el-table-column>
      <el-table-column
        prop="waitWeight"
        header-align="center"
        align="center"
        label="实际重量">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="80"
        label="任务详情">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="LocationBtn(scope.row.idenCardNum, scope.row.orderNo)">任务详情</el-button>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="80"
        label="订单详情">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">订单详情</el-button>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="completeOrder(scope.row.id)">完成</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">作废</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <order-detail v-if="orderDetailVisible" ref="orderDetail"></order-detail>
  </div>
</template>

<script>
  import orderDetail from './components/current-order-detail'
  export default {
    data () {
      return {
        dataForm: {
          idenCardNum: '',
          orderNo: ''
        },
        dataList: [],
        options1: [],
        options2: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        orderDetailVisible: false,
        dataRule: {
          idenCardNum: [
            { required: true, message: '标识卡号不能为空', trigger: 'change' }
          ],
          orderNo: [
            { required: true, message: '订单号不能为空', trigger: 'change' }
          ]
        }
      }
    },
    components: {
      orderDetail
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.$refs['dataForm'].validate((valid) => {
          console.log(valid)
          if (valid) {
            this.dataListLoading = true
            this.$http({
              url: this.$http.adornUrl('orderInfo/page'),
              method: 'post',
              data: this.$http.adornData({
                'current': this.pageIndex,
                'size': this.pageSize,
                'orderNo': this.dataForm.orderNo,
                'idenCardNum': this.dataForm.idenCardNum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataList = data.page.list
                this.totalPage = data.page.totalCount
              } else {
                this.dataList = []
                this.totalPage = 0
              }
              this.dataListLoading = false
            })
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      completeOrder (id) { // 完成订单
        this.$http({
          url: this.$http.adornUrl('orderInfo/completeOrder'),
          method: 'post',
          data: this.$http.adornData({
            'id': id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.orderDetailVisible = true
        this.$nextTick(() => {
          this.$refs.orderDetail.init(id)
        })
      },
      LocationBtn (idenCardNum, orderNo) {
        this.$router.push({name: 'order-current-order-task', query: {idenCardNum, orderNo}})
      },
      // 删除
      deleteHandle (id) {
        this.$confirm(`确定对[id=${id}]进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('orderInfo/voidOrder'),
            method: 'post',
            data: this.$http.adornData(id, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
