<template>
    <div>
        <div class="mrg-b">
          <!--<drop-filter :columns="wholeLabels" :storeKey="storeKey"  :storeLabels="storeLables"  @doFilter="labels => storeLables = labels"></drop-filter>-->
            <el-table
            ref="table"
            stripe
            border
            :max-height="tableMaxHeight"
            :data="tableData"
            :filteredLabels="storeLables"
            @selection-change='handleSelectChange'>
                <table-column
                type="selection"
                fixed
                width="45"
                    align="center">
                </table-column>
                <table-column
                label="货主">
                  <template slot-scope="scope">
                    <p>{{scope.row.ownerCode}}</p>
                    <p>{{scope.row.ebcuNameCn}}</p>
                  </template>
                </table-column>
                <table-column
                label="商品">
                  <template slot-scope="scope">
                    <div @click.stop="handleOpenDetail(scope.row.ownerCode,scope.row.skuCode)">
                      <p  class="blue">{{scope.row.skuCode}}</p>
                      <p  class="blue">{{scope.row.skuName}}</p>
                    </div>
                  </template>
                </table-column>
                <table-column
                label="商品类型">
                  <template slot-scope="scope">
                    {{$utils.keyMatcher(scope.row.typeCode, $keys.warehouseBasic.typeCode).name}}
                  </template>
                </table-column>
                <table-column
                label="包装规格"
                prop="cdpaFormat">
                </table-column>
                <table-column
                label="批次属性"
                prop="lotName">
                </table-column>
                <table-column
                label="保质期(天数)"
                prop="shelfLife">
                </table-column>
                <table-column
                label="是否启用">
                  <template slot-scope="scope">
                     {{ $utils.keyMatcher(scope.row.isEnable, $keys.warehouseBasic.isEnable).name }}
                  </template>
                </table-column>
                <table-column
                label="操作"
                width="100"
                fixed="right">
                    <template slot-scope="scope">
                       <el-tooltip content="编辑" placement="top">
                        <el-button
                        size="mini"
                        type="primary"
                        icon="el-icon-edit"
                        plain
                        circle
                        @click="handleEdit(scope.$index, scope.row)"></el-button></el-tooltip>
                         <el-tooltip content="删除" placement="top">
                        <el-button
                        size="mini"
                        type="danger"
                        icon="el-icon-delete"
                        plain
                        circle
                        @click="handleDelete(scope.$index, scope.row)"></el-button></el-tooltip>
                    </template>
                </table-column>
            </el-table>
        </div>
    <div>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
    </div>
</div>
</template>
<script>
export default {
  data () {
    return {

      removeSkuIds: [],
      headData: {},
      goodsData: [],
      storeKey: 'libraryGoods'
    }
  },
  props: {
    tableData: {
      type: Array,
      default: () => []
    },
    total: Number,
    pageSize: Number,
    currentPage: Number,
  },
  methods: {

    // 每页显示条数
    handleSizeChange (pageSize) {
      const query = { 'page.limitCount': pageSize, 'page.currentPage': this.currentPage }
      this.$emit('getList', query)
    },

    // 当前页码
    handleCurrentChange (currentPage) {
      const query = { 'page.limitCount': this.pageSize, 'page.currentPage': currentPage }
      this.$emit('getList', query)
    },
    getData (url, param) {
      return this.$https.post(url, param)
    },
    // 编辑行
    handleEdit (index, row) {
      const headUrl = this.$API.warehouseBasic.libraryGoods.querySku
      const goodsUrl = this.$API.warehouseBasic.libraryGoods.querySkuLoc
      const param = { ownerCode: row.ownerCode, skuCode: row.skuCode }
      this.$https.all([this.getData(headUrl, param), this.getData(goodsUrl, param)]).then(res => {
        this.headData = res[0].rows[0]
        this.goodsData = res[1].rows
        this.$emit('handleOpenForm', 'edit', this.headData, this.goodsData)
      })
    },

    // 详情
    handleOpenDetail (ownerCode, skuCode) {
      const headUrl = this.$API.warehouseBasic.libraryGoods.querySku
      const goodsUrl = this.$API.warehouseBasic.libraryGoods.querySkuLoc
      const param = { ownerCode, skuCode }
      this.$https.all([this.getData(headUrl, param), this.getData(goodsUrl, param)]).then(res => {
        const headData = res[0].rows[0]
        const goodsData = res[1].rows
        this.$emit('handleOpenForm', 'detail', headData, goodsData)
      })
    },

    // 删除行
    handleDelete (index, row) {
      const url = this.$API.warehouseBasic.libraryGoods.remove
      this.removeSkuIds.push(row.jobId)
      const param = { removeSkuIds: JSON.stringify(this.removeSkuIds) }

      this.$confirm('是否删除此条数据？', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$https.post(url, param).then(res => {
          this.$emit('getList')
          this.$getLotAttrs() // 重新获取全局批次属性
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // checkbox勾选
    handleSelectChange (selection) {
      const selectedIds = selection.map(item => item.jobId).join(',')
      const skuCodes = selection.map(item => item.skuCode)
      const rows = selection
      this.$emit('handleSelectChange', selectedIds, skuCodes, rows)
    }
  }
}
</script>
<style lang="scss" scoped>
    .mrg-b{
        margin-bottom: 20px;
    }
    .mrg-l{
        margin-left:10px;
    }
    .blue{
      cursor: pointer;
      color:#0682ea
    }
</style>
