<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="50300030203" type="primary" @click="toAdd">新增</el-button>
        <template v-if="formData.erpBusinessDetailBeanList.length">
          <el-button v-auth="50300010210" type="primary" @click="multiDelete">删除</el-button>
          <!--<el-button v-auth="50300010210" type="primary" needConfirm @click="e => operate(e, 'cancelMvDetail')">取消</el-button>-->
        </template>
      </template>
    </div>
    <div class="operation__msg">{{message}}</div>
  </el-row>
  <el-table
    auto-fit-column
    ref="table"
    stripe
    border
    :data="formData.erpBusinessDetailBeanList"
    @selection-change="selectionChange"
    @row-click="toCheck"
  >
    <table-column type="selection" fixed align="center"></table-column>
    <!--<table-column label="行号" prop="lineNo"></table-column>-->
    <table-column label="商品" prop="skuName"></table-column>
    <table-column label="批次号" prop="lotNum"></table-column>
    <table-column label="包装规格" prop="packCode"></table-column>
    <table-column label="包装单位" prop="uom"></table-column>
    <table-column label="源库位" prop="fmLoc"></table-column>
    <table-column label="目标库位" prop="toLoc"></table-column>
    <!--司磅-->
    <template v-if="formData.businessType === '881003-1'">
      <table-column label="数量" prop="qty"></table-column>
      <!--<table-column label="操作后数量" prop="afterQty"></table-column>-->
      <table-column label="源重量" prop="weight"></table-column>
      <table-column label="操作后重量" prop="afterWeight"></table-column>
    </template>
    <!--检验-->
    <template v-if="formData.businessType === '881003-2'">
      <table-column label="商检号" prop="checkNo"></table-column>
    </template>
    <table-column label="创建时间" prop="createTime"></table-column>
    <table-column label="修改时间" prop="modifyTime"></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, scope.$index)"
        ></el-button>
        <el-button
          v-auth="50300030213"
          title="删除"
          size="mini"
          type="danger"
          icon="el-icon-delete"
          plain
          circle
          @click.stop="toDelete(scope.$index)"
        ></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"-->
  <!--&gt;</el-pagination>-->
  <DetailCreator
    :mainFormData="formData"
    :showCreator="showCreator"
    :mode="detailMode"
    :detailData="detailData"
    :selectedIndex="selectedIndex"
    :selectedType="selectedType"
    @close="close"
    @createDone="setDetailList"
  />
</div>
</template>

<script>
import DetailCreator from './DetailCreator'
export default {
  props: ['mode', 'formData', 'willAddDetail', 'selectedType'],
  components: { DetailCreator },
  created () {
  },
  mounted () {
  },

  data () {
    return {
      // currentNo: 1,
      // pageSize: 10,
      // total: 0,
      detailList: [], // 调整明细列表
      message: '', // 数据说明
      showCreator: false,
      selectedIndex: undefined, // 操作的行index
      detailMode: 'create', // 调整明细业务的状态
      detailData: {}, // 调整明细的详情对象, 等于列表每条数据
    }
  },
  methods: {
    close () {
      this.showCreator = false
    },
    // 点击表格行
    toCheck (row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 新增按钮
    toAdd () {
      if (this.willAddDetail()) {
        this.showCreator = true
        this.selectedIndex = undefined
        this.detailData = {}
        this.detailMode = 'create'
      } else {
        this.$message.warning('请先填写关键信息')
      }
    },
    // 编辑
    toEdit (row, $index) {
      this.detailData = Object.assign({}, row)
      this.selectedIndex = $index
      this.detailMode = 'edit'
      this.showCreator = true
    },
    // 删除
    toDelete (index) {
      this.$confirm(`确认要删除吗 ?`)
        .then(() => {
          this.formData.erpBusinessDetailBeanList.splice(index, 1)
          this.setLineNo()
        })
    },
    // 批量删除
    multiDelete () {
      if (this.checkSelections()) {
        this.$confirm(`确认要删除吗 ?`)
          .then(() => {
            this.selectedIds.map(item => {
              this.formData.erpBusinessDetailBeanList.some((listItem, listIndex) => {
                if (listItem.lineNo === item) {
                  this.formData.erpBusinessDetailBeanList.splice(listIndex, 1)
                }
                return listItem.lineNo === item
              })
            })
            this.setLineNo()
          })
          .catch(() => { console.log('取消') })
      }
    },
    // 勾选变化
    selectionChange (selection) {
      this.selectedIds = selection.map(item => item.lineNo)
    },
    // 检查勾选
    checkSelections () {
      if (!this.selectedIds) {
        this.$message.error('请选择记录')
        return false
      }
      return true
    },
    // 新增或修改确认
    setDetailList (obj, $index) {
      // 修改时使用 $index, 新增时没有 $index 则使用 length
      const index = $index !== undefined ? $index : this.formData.erpBusinessDetailBeanList.length
      this.formData.erpBusinessDetailBeanList.splice(index, 1, obj)
      // this.setLineNo()
    },
    // 设置行号, 暂时不用
    setLineNo () {
      const length = this.formData.erpBusinessDetailBeanList.length
      for (let index = 0; index < length; ++index) {
        this.formData.erpBusinessDetailBeanList[index].lineNo = this.formatLineNo(index)
      }
    },
    // 格式化行号
    formatLineNo (index) {
      let baseNo = ((index + 1) / 1000).toString().replace('.', '')
      const diff = 4 - baseNo.length
      for (let i = 0; i < diff; ++i) {
        baseNo += '0'
      }
      return baseNo
    },

    // 暂时不用的方案
    // 获取明细列表
    getDetailList (query = {}) {
      const url = this.$API.inventory.insideBusiness.getDetailList
      const param = {
        businessNo: this.formData.businessNo,
        '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
        })
    },

    // 页码变化
    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)
    },
    // 操作弹窗后确认
    operateConfirm (type) {
      const url = this.$API.inventory.insideBusiness[type]
      const param = {
        businessNo: this.businessNo,
        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;
  }
}
</style>
