<template>
<div class="list">
  <el-row class="list__operation">
    <div class="operation__btn">
      <template v-if="mode === 'detail'"></template>
      <template v-else>
        <el-button v-auth="50300050203" type="primary" @click="toAdd">新增</el-button>
        <template v-if="detailList.length">
          <el-button v-auth="50300050207" type="primary" needConfirm @click="e => operate(e, 'removeMvDetail')">删除</el-button>
          <el-button v-auth="50300050208" type="primary" needConfirm @click="e => operate(e, 'cancelMvDetail')">取消</el-button>
        </template>
      </template>
    </div>
    <div class="operation__msg">{{message}}</div>
  </el-row>
  <el-table
    ref="table"
    stripe
    border
    :data="detailList"
    @selection-change="selectionChange"
    @row-click="toCheck"
  >
    <table-column type="selection" fixed align="center"></table-column>
    <table-column label="行号" prop="lineNo">
      <template slot-scope="scope">
        <span class="detail-no" @click.stop="toDetail(scope.row)">{{scope.row.lineNo}}</span>
      </template>
    </table-column>
    <table-column label="状态" prop="status">
      <template slot-scope="scope">
        {{ $utils.keyMatcher(scope.row.status, $keys.inventory.mvOrder.status).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="packDesc"></table-column>
    <table-column label="包装单位" prop="uomDesc"></table-column>
    <table-column label="计划移动数" prop="qtyPlanMvEa"></table-column>
    <table-column label="已移动数" prop="qtyMvUom"></table-column>
    <table-column label="计划移动重量(吨)" prop="planMvWeight" :formatter="formatColumn"></table-column>
    <table-column label="已移动重量(吨)" prop="alreadyMvWeight" :formatter="formatColumn"></table-column>
    <table-column label="操作" width="100" fixed="right" v-if="mode !== 'detail'">
      <template slot-scope="scope">
        <el-button
          v-auth="50300030212"
          title="编辑"
          size="mini"
          type="primary"
          icon="el-icon-edit"
          plain
          circle
          @click.stop="toEdit(scope.row)"
        ></el-button>
        <el-button
          v-auth="50300030213"
          title="删除"
          size="mini"
          type="danger"
          icon="el-icon-delete"
          plain
          circle
          @click.stop="toDelete(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>
  <DetailCreator
    :mvNo="formData.mvNo"
    :ownerCode="formData.ownerCode"
    :showCreator="showCreator"
    :mode="detailMode"
    :detailData="detailData"
    @close="close"
    @createDone="getDetailList"
  />
</div>
</template>

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

  data () {
    return {
      currentNo: 1,
      pageSize: 10,
      total: 0,
      detailList: [], // 调整明细列表
      message: '', // 数据说明
      showCreator: false,
      detailMode: 'create', // 调整明细业务的状态
      detailData: {}, // 调整明细的详情对象, 等于列表每条数据
    }
  },
  methods: {
    close () {
      this.showCreator = false
    },
    // 获取明细列表
    getDetailList (query = {}) {
      const url = this.$API.inventory.mvOrder.getDetailList
      const param = {
        mvNo: this.formData.mvNo,
        'page.currentPage': 1,
        'page.limitCount': this.pageSize,
        ...query,
      }
      this.$https.post(url, param)
        .then(data => {
          // 计算数量
          let qtyPlanMvEa = 0, qtyMvUom = 0
          for (const item of data.rows) {
            qtyPlanMvEa += parseFloat(item.qtyPlanMvEa)
            qtyMvUom += parseFloat(item.qtyMvUom)
          }
          this.message = `总行数: ${data.rows.length}, 计划移动数: ${qtyPlanMvEa}, 已移动数EA: ${qtyMvUom}`
          this.total = data.total
          this.detailList = data.rows
        })
    },
    // 点击表格行
    toCheck (row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 页码变化
    currentChange (currentNo) {
      this.currentNo = currentNo
      const query = { 'page.currentPage': currentNo }
      this.getDetailList(query)
    },
    // 每页条数变化
    sizeChange (pageSize) {
      this.pageSize = pageSize
      const query = { 'page.limitCount': pageSize }
      this.getDetailList(query)
    },
    // 新增按钮
    toAdd () {
      this.showCreator = true
      this.detailData = {}
      this.detailMode = 'create'
    },
    // 编辑
    toEdit (row) {
      this.detailData = Object.assign({}, row)
      this.detailMode = 'edit'
      this.showCreator = true
    },
    // 详情
    toDetail (row) {
      this.detailData = row
      this.detailMode = 'detail'
      this.showCreator = true
    },
    // 删除
    toDelete (row) {
      const url = this.$API.inventory.mvOrder.removeMvDetail
      const param = {
        mvNo: this.formData.mvNo,
        lineNos: row.lineNo,
      }
      this.$confirm(`您确认要删除 ?`)
        .then(() => {
          this.$https.post(url, param)
            .then(data => {
              this.getDetailList()
            })
        })
    },
    // 勾选变化
    selectionChange (selection) {
      this.selectedIds = selection.map(item => item.lineNo).join(',')
    },
    // 检查勾选
    checkSelections () {
      if (!this.selectedIds) {
        this.$message.error('请选择记录')
        return false
      }
      return true
    },
    // 操作弹窗后确认
    operateConfirm (type) {
      const url = this.$API.inventory.mvOrder[type]
      const param = {
        mvNo: this.formData.mvNo,
        lineNos: this.selectedIds,
      }
      this.$https.post(url, param)
        .then(data => {
          this.getDetailList()
        })
    },
  },
}
</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>
