<template>
<div class="list">
  <el-row class="list__operation">
    <div class="operation__btn">
      <template v-if="mode === 'detail' && adList.length">
        <el-button v-if="(formData.status === '00' && formData.auditStatus !== '00') || formData.status === '10'" v-auth="50300030302" type="primary" @click="e => operate(e, 'cancelAdDetailEntity')">取消</el-button>
        <el-button v-if="(formData.status === '00' && formData.auditStatus !== '00') || formData.status === '10'" v-auth="50300030303" type="primary" @click="e => operate(e, 'executeAdDetail')">执行调整</el-button>
        <el-button v-if="formData.status === '10' || formData.status === '20'" v-auth="50300030304" type="primary" @click="e => operate(e, 'cancelExecuteAdDetail')">取消调整</el-button>
      </template>
      <template v-if="mode !== 'detail'">
        <el-button v-auth="50300030203" type="primary" @click="toAdd">新增</el-button>
        <template v-if="adList.length">
          <el-button v-auth="50300010210" type="primary" @click="e => operate(e, 'removeAdDetailEntity')">删除</el-button>
        </template>
      </template>
    </div>
    <div class="operation__msg">{{message}}</div>
  </el-row>
  <el-table
    ref="table"
    stripe
    border
    :data="adList"
    @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.adjustment.status).name }}
      </template>
    </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="adMode"></table-column>
    <table-column label="包装单位" prop="packCode"></table-column>
    <table-column label="调整数" prop="qtyAdUom"></table-column>
    <table-column label="调整重量(吨)" prop="adWeight" :formatter="formatColumn"></table-column>
    <table-column label="操作" width="100" fixed="right" v-if="mode !== 'detail'">
      <template slot-scope="scope">
        <el-button
          v-auth="50300030212"
          size="mini"
          type="primary"
          icon="el-icon-edit"
          plain
          circle
          @click.stop="toEdit(scope.row)"
        ></el-button>
        <el-button
          v-auth="50300030213"
          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
    :adNo="formData.adNo"
    :ownerCode="formData.ownerCode"
    :showCreator="showCreator"
    :mode="detailMode"
    :detailData="detailData"
    :mainFormData="formData"
    @close="close"
    @createDone="refreshForm"
  />
</div>
</template>

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

  data () {
    return {
      currentNo: 1,
      pageSize: 10,
      total: 0,
      adList: [], // 调整明细列表
      message: '', // 数据说明
      showCreator: false,
      detailMode: 'create', // 调整明细业务的状态
      detailData: {}, // 调整明细的详情对象, 等于列表每条数据
    }
  },
  methods: {
    close () {
      this.showCreator = false
    },
    // 获取明细列表
    getAdDetailList (query = {}) {
      const url = this.$API.inventory.adjustment.getAdDetailList
      const param = {
        adNo: this.formData.adNo,
        'page.currentPage': 1,
        'page.limitCount': this.pageSize,
        ...query,
      }
      this.$https.post(url, param)
        .then(data => {
          // 计算数量
          let qtyAdEa = 0, qtyAdUom = 0
          for (const item of data.rows) {
            qtyAdEa += parseFloat(item.qtyAdEa)
            qtyAdUom += parseFloat(item.qtyAdUom)
          }
          this.message = `总行数: ${data.rows.length}, 调整数: ${qtyAdEa.toFixed(2)}, 调整数EA: ${qtyAdUom.toFixed(2)}`
          this.total = data.total
          this.adList = data.rows
        })
    },
    // 点击表格行
    toCheck (row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 页码变化
    currentChange (currentNo) {
      this.currentNo = currentNo
      const query = { 'page.currentPage': currentNo }
      this.getAdDetailList(query)
    },
    // 每页条数变化
    sizeChange (pageSize) {
      this.pageSize = pageSize
      const query = { 'page.limitCount': pageSize }
      this.getAdDetailList(query)
    },
    // 新增按钮
    toAdd () {
      this.showCreator = true
      this.detailData = {}
      this.detailMode = 'create'
    },
    // 编辑
    toEdit (row) {
      const allowedStatus = ['00']
      if (!allowedStatus.includes(row.status)) {
        this.$message.warning('该状态下不能编辑哦')
      } else {
        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.adjustment.removeAdDetailEntity
      const param = {
        adNo: this.formData.adNo,
        lineNos: row.lineNo,
      }
      this.$confirm(`您确认要删除 ?`)
        .then(() => {
          this.$https.post(url, param)
            .then(data => {
              this.getAdDetailList()
            })
        })
    },
    // 勾选变化
    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.adjustment[type]
      const param = {
        adNo: this.formData.adNo,
        lineNos: this.selectedIds,
      }
      this.$https.post(url, param)
        .then(data => {
          if (data.messagesAsString !== '操作成功') {
            this.$message.closeAll()
            this.$message.error(data.messagesAsString)
          } else {
            this.refreshForm()
          }
        })
    },
    // 刷新表单
    refreshForm () {
      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>
