<template>
<div class="list">
  <el-row class="list__operation">
    <div class="operation__btn">
      <template v-if="mode === 'detail'">
        <template v-if="formData.status === '70' || (formData.status === '10' && formData.countMethod !== 'D')">
          <el-button v-auth="50300010304" type="primary" @click="confirm">盘点确认</el-button>
          <el-button v-auth="50300010217" type="primary" @click="toAdd">新增</el-button>
        </template>
      </template>
      <template v-else-if="taskList.length">
        <el-button v-auth="50300010206" type="primary" @click="cancelTask(formData.countNo)">取消盘点任务</el-button>
        <el-button v-auth="50300010210" type="primary" @click="e => operate(e, 'removeTask')">删除</el-button>
      </template>
      <template v-else>
        <el-button type="primary" @click="createTask(formData.countNo)">生成普通盘点任务</el-button>
      </template>
    </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="countId">
      <template slot-scope="scope">
        <span class="detail-no" @click.stop="toDetail(scope.row)">{{scope.row.countId}}</span>
      </template>
    </table-column>
    <table-column label="状态" prop="status">
      <template slot-scope="scope">
        {{ $utils.keyMatcher(scope.row.status, $keys.inventory.count.detailStatus).name }}
      </template>
    </table-column>
    <table-column label="货主" prop="ownerName"></table-column>
    <table-column label="商品" prop="skuName"></table-column>
    <table-column label="批次号" prop="lotNum"></table-column>
    <table-column label="库位" prop="locCode"></table-column>
    <table-column label="跟踪号" prop="traceId"></table-column>
    <table-column label="库存数" prop="qty"></table-column>
    <table-column label="盘点数" prop="qtyCountEa"></table-column>
    <table-column label="损益数" prop="qtyDiff"></table-column>
    <table-column label="库存重量(吨)" prop="countWeight" :formatter="formatColumn"></table-column>
    <table-column label="盘点重量(吨)" prop="weight" :formatter="formatColumn"></table-column>
    <table-column label="损益重量(吨)" prop="diffWeight" :formatter="formatColumn"></table-column>
    <table-column label="操作" width="100" fixed="right">
      <template slot-scope="scope">
        <el-button
          v-if="mode !== 'detail'"
          v-auth="50300010213"
          title="删除"
          size="mini"
          type="danger"
          icon="el-icon-delete"
          plain
          circle
          @click.stop="toDelete(scope.row)"
        ></el-button>
        <el-button
          v-if="mode === 'detail'"
          v-auth="50300010321"
          title="盘点确认"
          size="mini"
          type="primary"
          icon="el-icon-success"
          plain
          circle
          @click.stop="toConfirm(scope.row)"
        ></el-button>
        <el-button
          v-if="mode === 'detail'"
          v-auth="50300010214"
          title="复制"
          size="mini"
          type="primary"
          icon="el-icon-document"
          plain
          circle
          @click.stop="toDuplicate(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
    :show="showTaskDetail"
    :detailData="detailData"
    @close="showTaskDetail = false"
  ></TaskDetail>
  <TaskCreator
    :show="showTaskCreator"
    :detailData="detailData"
    :mode="creatorMode"
    :countNo="formData.countNo"
    @close="showTaskCreator = false"
    @refreshForm="refreshForm"
  ></TaskCreator>
</div>
</template>

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

  data () {
    return {
      currentNo: 1,
      pageSize: 10,
      total: 0,
      taskList: [], // 盘点任务列表
      message: '', // 数据说明
      detailData: {}, // 详情数据
      showTaskDetail: false, // 任务详情
      showTaskCreator: false, // 盘点确认弹窗
      creatorMode: 'create', // 确认框模式
      selection: [], // 选中的数据
      selectedIds: '', // 选中的id
    }
  },
  methods: {
    // 获取任务列表
    queryTaskCountList (query = {}) {
      const url = this.$API.inventory.count.queryTaskCountList
      const param = {
        countNo: this.formData.countNo,
        'page.currentPage': 1,
        'page.limitCount': this.pageSize,
        ...query,
      }
      this.$https.post(url, param)
        .then(data => {
          // 计算数量
          let qtyCount = 0, qtyCountEACount = 0, qtyDiffCount = 0
          for (const item of data.rows) {
            qtyCount += parseFloat(item.qty)
            qtyCountEACount += parseFloat(item.qtyCountEa)
            qtyDiffCount += parseFloat(item.qtyDiff)
          }
          this.message = `总行数: ${data.rows.length}, 库存数EA: ${qtyCount.toFixed(2)}, 盘点数EA: ${qtyCountEACount.toFixed(2)}, 损益数EA: ${qtyDiffCount.toFixed(2)}`
          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.queryTaskCountList(query)
    },
    // 每页条数变化
    sizeChange (pageSize) {
      this.pageSize = pageSize
      const query = { 'page.limitCount': pageSize }
      this.queryTaskCountList(query)
    },
    // 详情
    toDetail (row) {
      this.detailData = row
      this.showTaskDetail = true
    },
    // 删除
    toDelete (row) {
      const url = this.$API.inventory.count.removeTask
      const param = {
        countNo: this.formData.countNo,
        countIds: row.countId,
      }
      this.$confirm(`您确认要删除 ?`)
        .then(() => {
          this.$https.post(url, param)
            .then(data => {
              this.queryTaskCountList()
            })
        })
    },
    // 勾选变化
    selectionChange (selection) {
      this.selection = selection
      this.selectedIds = selection.map(item => item.countId).join(',')
    },
    // 取消盘点任务
    cancelTask (countNos) {
      const url = this.$API.inventory.count.cancelTask
      const param = { countNos }
      this.$https.post(url, param)
        .then(data => {
          this.queryTaskCountList()
        })
    },
    // 检查勾选
    checkSelections () {
      if (!this.selectedIds) {
        this.$message.error('请选择记录')
        return false
      }
      return true
    },
    // 操作弹窗后确认
    operateConfirm (type) {
      const url = this.$API.inventory.count[type]
      const param = {
        countNo: this.formData.countNo,
        countIds: this.selectedIds,
      }
      this.$https.post(url, param)
        .then(data => {
          this.queryTaskCountList()
        })
    },
    // 生成普通盘点任务
    createTask (countNos) {
      const url = this.$API.inventory.count.createTask
      const param = { countNos }
      this.$https.post(url, param)
        .then(data => {
          this.refreshForm()
        })
    },
    // 刷新表单
    refreshForm () {
      this.$emit('refreshForm')
    },
    // 列表盘点确认
    toConfirm (row) {
      if ((this.formData.status === '10' || this.formData.status === '70') && row.status === '00' && this.formData.countMethod !== 'D') {
        this.creatorMode = 'confirm'
        this.detailData = row
        this.showTaskCreator = true
      } else {
        this.$message.warning('该状态下不能确认盘点哦')
      }
    },
    // 复制
    toDuplicate (row) {
      this.creatorMode = 'duplicate'
      this.detailData = row
      this.showTaskCreator = true
    },
    // 新增
    toAdd () {
      this.creatorMode = 'create'
      this.detailData = {}
      this.showTaskCreator = true
    },
    // 表头盘点确认
    confirm () {
      if (this.checkSelections()) {
        const url = this.$API.inventory.count.confirmSelectCount
        const param = {
          countEntitys: JSON.stringify(this.selection),
        }
        this.$https.post(url, param)
          .then(data => {
            if (data.messagesAsString !== '操作成功') {
              this.$message.error(data.messagesAsString)
            }
            this.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>
