<template>
<div class="list">
  <el-row class="list__operation">
    <div class="operation__btn">
      <template v-if="(formData.status === '00' || formData.status === '20' || formData.status === '30') && (formData.auditStatus !== '00')">
        <el-button v-auth="50300050326" type="primary" @click="executeMoveTo">执行移动到</el-button>
        <el-button v-auth="50300050302" type="primary" @click="e => operate(e, 'performMoveForMvTask')">执行移动</el-button>
      </template>
      <el-button v-if="formData.status === '20' || formData.status === '30' || formData.status === '40'" v-auth="50300050305" type="primary" @click="cancelMove">取消移动</el-button>
    </div>
    <div class="operation__msg">{{message}}</div>
  </el-row>
  <el-table
    ref="table"
    stripe
    border
    :data="taskList"
    @selection-change="selectionChange"
    @row-click="toCheck"
  >
    <table-column type="selection" fixed align="center"></table-column>
    <table-column label="任务ID" prop="mvId">
      <template slot-scope="scope">
        <span class="detail-no" @click.stop="toDetail(scope.row)">{{scope.row.mvId}}</span>
      </template>
    </table-column>
    <table-column label="状态" prop="status">
      <template slot-scope="scope">
        {{ $utils.keyMatcher(scope.row.status, $keys.inventory.mvOrder.task.status).name }}
      </template>
    </table-column>
    <table-column label="行号" prop="lineNo"></table-column>
    <table-column label="移动任务行号" prop="mvLineNo"></table-column>
    <table-column label="货主" prop="ebcuNameCn"></table-column>
    <table-column label="商品" prop="skuName"></table-column>
    <table-column label="批次号" prop="lotNum"></table-column>
    <table-column label="库位(源/目标)" prop="loc">
      <template slot-scope="scope">
        {{scope.row.fmLoc}}/{{scope.row.toLoc}}
      </template>
    </table-column>
    <table-column label="包装单位" prop="uomDesc"></table-column>
    <table-column label="移动数" prop="toQtyMvUom"></table-column>
    <table-column label="移动重量(吨)" prop="mvWeight"></table-column>
    <table-column label="操作" width="100" fixed="right">
      <template slot-scope="scope">
        <el-button
          v-auth="50300050327"
          title="编辑"
          size="mini"
          type="primary"
          icon="el-icon-edit"
          plain
          circle
          @click.stop="toEdit(scope.row)"
        ></el-button>
      </template>
    </table-column>
  </el-table>
  <el-pagination
    class="pagination"
    @size-change="sizeChange"
    @current-change="currentChange"
    :current-page="currentNo"
    :page-sizes="[10, 20, 30, 40, 50]"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  ></el-pagination>
  <!--任务详情-->
  <TaskDetail
    :showDetail="showDetail"
    :detailData="detailData"
    @close="close"
  ></TaskDetail>
  <!--任务编辑-->
  <TaskEdit
    :showEdit="showEdit"
    :detailData="detailData"
    :showOperateBtn="formData.auditStatus !== '00'"
    @close="close"
    @createDone="refreshForm"
  ></TaskEdit>
  <!--执行移动到-->
  <MoveTo
    :show="showMoveTo"
    :selectedRows="selectedRows"
    @close="showMoveTo = false"
    @createDone="refreshForm"
  ></MoveTo>
</div>
</template>

<script>
import DetailCreator from '../detailList/DetailCreator'
import TaskDetail from './TaskDetail'
import TaskEdit from './TaskEdit'
import MoveTo from './MoveTo'
export default {
  props: ['mode', 'needRefresh', 'formData'],
  components: { DetailCreator, TaskDetail, TaskEdit, MoveTo },
  created () {
  },
  mounted () {
  },
  watch: {
    needRefresh () {
      this.getTaskList()
    }
  },

  data () {
    return {
      currentNo: 1,
      pageSize: 10,
      total: 0,
      taskList: [], // 移动任务列表
      message: '', // 数据说明
      showDetail: false, // 详情框
      showEdit: false, // 编辑框
      detailMode: 'create', // 移动任务业务的状态
      detailData: {}, // 移动任务的详情对象, 等于列表每条数据
      showMoveTo: false, // 执行移动到弹窗
      selectedRows: [], // 选中的行
    }
  },
  methods: {
    close () {
      this.showDetail = false
      this.showEdit = false
    },
    // 获取任务列表
    getTaskList (query = {}) {
      const url = this.$API.inventory.mvOrder.queryMvTask
      const param = {
        mvNo: this.formData.mvNo,
        'page.currentPage': 1,
        'page.limitCount': this.pageSize,
        ...query,
      }
      this.$https.post(url, param)
        .then(data => {
          // 计算数量
          let toQtyMvUom = 0
          for (const item of data.rows) {
            toQtyMvUom += parseFloat(item.toQtyMvUom)
          }
          this.message = `总行数: ${data.rows.length}, 移动数EA: ${toQtyMvUom}`
          this.total = data.total
          this.taskList = data.rows
        })
    },
    // 点击表格行
    toCheck (row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 页码变化
    currentChange (currentNo) {
      this.currentNo = currentNo
      const query = { 'page.currentPage': currentNo }
      this.getTaskList(query)
    },
    // 每页条数变化
    sizeChange (pageSize) {
      this.pageSize = pageSize
      const query = { 'page.limitCount': pageSize }
      this.getTaskList(query)
    },
    // 编辑
    toEdit (row) {
      this.detailData = Object.assign({}, row)
      this.detailMode = 'edit'
      this.showEdit = true
    },
    // 详情
    toDetail (row) {
      this.detailData = row
      this.detailMode = 'detail'
      this.showDetail = true
    },
    // 勾选变化
    selectionChange (selection) {
      this.selectedRows = selection
    },
    // 检查勾选
    checkSelections () {
      if (!this.selectedRows.length) {
        this.$message.error('请选择记录')
        return false
      }
      return true
    },
    // 操作弹窗后确认
    operateConfirm (type) {
      const url = this.$API.inventory.mvOrder[type]
      const param = {
        mvTaskRows: JSON.stringify(this.selectedRows)
      }
      this.$https.post(url, param)
        .then(data => {
          this.refreshForm()
        })
    },
    // 刷新表单
    refreshForm () {
      this.$emit('refreshForm')
    },
    // 执行移动到弹窗
    executeMoveTo () {
      if (this.checkSelections()) {
        this.showMoveTo = true
      }
    },
    // 取消移动
    cancelMove () {
      const url = this.$API.inventory.mvOrder.bacthCancelPerformByTask
      const param = {
        mvIdAndlineNos: this.selectedRows.map(item => `(${item.mvId},${item.lineNo})`).join(',')
      }
      this.$https.post(url, param)
        .then(data => {
          if (data === '') {
            this.$message.error('获取失败')
          } else {
            this.close()
            this.$emit('refreshForm')
          }
        })
    },
  },
}
</script>

<style lang="scss" scoped>
.pagination {
  margin-top: 20px;
}
.list__operation {
  padding-bottom: 20px;
  .operation__btn {
    float: left;
  }
  .operation__msg {
    float: right;
  }
}
.detail-no {
  color: #0682ea;
  &:hover {
    cursor: pointer;
  }
}
</style>
