<template>
<div class="list">
  <el-row class="list__operation">
    <div class="operation__btn">
      <template v-if="mode === 'detail'">
        <template v-if="formData.status === '10' || (formData.status === '00' && formData.auditStatus !== '00')">
          <el-button v-auth="50300070306" type="primary" @click="e => operate(e, 'executeTfDetail')">执行转移</el-button>
          <!--todo: 扫描序列按钮-->
          <!--<el-button v-if="formData.status === '10' || (formData.status === '00' && formData.auditStatus !== '00')" v-auth="50300070215" @click="">扫描序列号</el-button>-->
        </template>
        <el-button v-auth="50300070307" v-if="formData.status !== '00'" type="primary" @click="e => operate(e, 'cancelExecuteTfDetail')">取消转移</el-button>
      </template>
      <template v-else>
        <el-button v-auth="50300070207" type="primary" @click="createTfDetail">生成转移明细</el-button>
        <el-button type="primary" @click="toAddTfDetail">新增</el-button>
        <template v-if="taskList.length">
          <el-button v-auth="50300070213" type="primary" needConfirm @click="e => operate(e, 'removeTask')">删除</el-button>
          <el-button v-auth="50300070214" type="primary" needConfirm @click="e => operate(e, 'cancelTask')">取消</el-button>
        </template>
      </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="行号" 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.tfOrder.status).name }}
      </template>
    </table-column>
    <table-column label="货主(源/目标)">
      <template slot-scope="scope">
        <div>{{scope.row.fmEbcuNameCn}}</div>
        <div>{{scope.row.toEbcuNameCn}}</div>
      </template>
    </table-column>
    <table-column label="商品(源/目标)">
      <template slot-scope="scope">
        <div>{{scope.row.fmSkuName}}</div>
        <div>{{scope.row.toSkuName}}</div>
      </template>
    </table-column>
    <table-column label="批次(源/目标)">
      <template slot-scope="scope">
        <div>{{scope.row.fmLot}}</div>
        <div>{{scope.row.toLot}}</div>
      </template>
    </table-column>
    <table-column label="库位(源/目标)">
      <template slot-scope="scope">
        <div>{{scope.row.fmLoc || '-'}}</div>
        <div>{{scope.row.toLoc || '-'}}</div>
      </template>
    </table-column>
    <table-column label="单位(源/目标)">
      <template slot-scope="scope">
        <div>{{scope.row.fmCdprDesc || '-'}}</div>
        <div>{{scope.row.toCdprDesc || '-'}}</div>
      </template>
    </table-column>
    <table-column label="转出数" prop="fmQtyUom"></table-column>
    <table-column label="转入数" prop="toQtyUom"></table-column>
    <table-column label="转出重量(吨)" prop="fmWeight" :formatter="formatColumn"></table-column>
    <table-column label="转入重量(吨)" prop="toWeight" :formatter="formatColumn"></table-column>
    <table-column label="操作" width="100" fixed="right">
      <template slot-scope="scope">
        <template v-if="mode === 'detail'">
          <el-button
            title="提交"
            size="mini"
            type="success"
            icon="el-icon-success"
            plain
            circle
            @click.stop="e => execute(e, scope.row)"
          ></el-button>
        </template>
        <template v-else>
          <el-button
            title="编辑"
            v-auth="50300070216"
            size="mini"
            type="success"
            icon="el-icon-edit"
            plain
            circle
            @click.stop="toEdit(scope.row)"
          ></el-button>
          <el-button
            title="删除"
            v-auth="50300070217"
            size="mini"
            type="danger"
            icon="el-icon-delete"
            plain
            circle
            @click.stop="toDelete(scope.row)"
          ></el-button>
        </template>
      </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
    :mainFormData="formData"
    :showCreator="showCreator"
    :mode="detailMode"
    :detailData="detailData"
    @close="showCreator = false"
    @createDone="queryTaskCountList"
  />
</div>
</template>

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

  data () {
    return {
      currentNo: 1,
      pageSize: 10,
      total: 0,
      taskList: [], // 盘点任务列表
      message: '', // 数据说明
      showCreator: false, // 转移明细的新增框
      detailData: {}, // 转移明细详情
      detailMode: 'create', // 新增框的模式
      selection: [], // 选中的数据
    }
  },
  methods: {
    // 获取转移任务列表
    queryTaskCountList (query = {}) {
      const url = this.$API.inventory.tfOrder.tfDetailList
      const param = {
        tfNo: this.formData.tfNo,
        'page.currentPage': 1,
        'page.limitCount': this.pageSize,
        ...query,
      }
      this.$https.post(url, param)
        .then(data => {
          // 计算数量
          let toQtyUom = 0, fmQtyUom = 0
          for (const item of data.rows) {
            toQtyUom += parseFloat(item.toQtyUom)
            fmQtyUom += parseFloat(item.fmQtyUom)
          }
          this.message = `总行数: ${data.rows.length}, 转入数: ${toQtyUom.toFixed(2)}, 转出数: ${fmQtyUom.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.detailMode = 'detail'
      this.detailData = row
      this.showCreator = true
    },
    // 编辑
    toEdit (row) {
      this.detailMode = 'edit'
      this.detailData = row
      this.showCreator = true
    },
    // 删除
    toDelete (row) {
      const url = this.$API.inventory.tfOrder.removeTask
      const param = {
        wmTfDetailBean: JSON.stringify([row])
      }
      this.$confirm(`您确认要删除 ?`)
        .then(() => {
          this.$https.post(url, param)
            .then(data => {
              this.queryTaskCountList()
            })
        })
    },
    // 勾选变化
    selectionChange (selection) {
      this.selection = selection
    },
    // 取消转移明细
    cancelTask (tfNos) {
      const url = this.$API.inventory.tfOrder.cancelTask
      const param = { tfNos }
      this.$https.post(url, param)
        .then(data => {
          this.queryTaskCountList()
        })
    },
    // 检查勾选
    checkSelections () {
      if (!this.selection.length) {
        this.$message.error('请选择记录')
        return false
      }
      return true
    },
    // 操作弹窗后确认
    operateConfirm (type) {
      let param
      if (this.mode === 'detail') {
        param = {
          tfNo: this.formData.tfNo,
          wmTfDetailEntity: JSON.stringify(this.selection)
        }
      } else {
        param = {
          wmTfDetailBean: JSON.stringify(this.selection)
        }
      }
      const url = this.$API.inventory.tfOrder[type]
      this.$https.post(url, param)
        .then(data => {
          if (data.success === true && data.messagesAsString === '操作成功') {
            this.$emit('operateDone')
          } else {
            this.$message.error(data.messagesAsString || '服务出问题了 >.<')
          }
        })
    },
    // 生成转移明细
    createTfDetail () {
      this.$emit('createTfDetail')
    },
    // 新增转移明细
    toAddTfDetail () {
      this.detailMode = 'create'
      this.showCreator = true
      this.detailData = {}
    },
    // 执行转移
    execute (e, row) {
      this.selection = [row]
      this.operate(e, 'executeTfDetail')
    },
  },
}
</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>
