<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="加工单号"
                prop="kitNo">
                <template slot-scope="scope">
                  <span class="blue" @click.stop="handleOpenDetail(scope.row.kitNo)">{{scope.row.kitNo}}</span>
                </template>
                </table-column>
                <table-column
                label="物流单号"
                prop="logisticNo">
                </table-column>
                <table-column
                label="状态"
                prop="status">
                  <template slot-scope="scope">
                    <p>{{ $utils.keyMatcher(scope.row.status, $keys.stockIn.status).name }}</p>
                    <p>{{ $utils.keyMatcher(scope.row.auditStatus, $keys.stockIn.auditStatus).name }}</p>
                  </template>
                </table-column>
                <table-column
                label="类型"
                prop="kitType">
                <template slot-scope="scope">
                    <p>{{ $utils.keyMatcher(scope.row.kitType, $keys.maintain.kitType).name }}</p>
                  </template>
                </table-column>
                <table-column
                label="货主">
                <template slot-scope="scope">
                  <p>{{scope.row.ownerCode}}</p>
                  <p>{{scope.row.ownerName}}</p>
                </template>
                </table-column>
                <table-column
                label="计划加工台"
                prop="kitLoc">
                </table-column>
                <table-column
                label="订单时间"
                prop="createTime">
                </table-column>
                <table-column
                label="计划加工时间">
                <template slot-scope="scope">
                  <p>从：{{scope.row.fmEtk}}</p>
                  <p>到：{{scope.row.toEtk}}</p>
                </template>
                </table-column>
                <table-column
                label="操作"
                width="100"
                fixed="right">
                    <template slot-scope="scope">
                       <el-tooltip content="编辑" placement="top">
                        <el-button
                        v-auth="50500020117"
                        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
                        v-auth="50500020106"
                        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 {

      remove: [],
      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)
    },
    getOrderStatus (kitOrderHeader) {
      const orderStatus = {
        'NEW': 'NEW', // 新增状态
        'CREATE_NEW': 'CREATE_NEW', // 创建未审核
        'CREATE_CLOSE': 'CREATE_CLOSE', // 创建已审核
        'CREATE_NOT': 'CREATE_NOT', // 创建不审核
        'PARTKIT_CLOSE': 'PARTKIT_CLOSE', // 部分加工已审核
        'PARTKIT_NOT': 'PARTKIT_NOT', // 部分加工不审核
        'FULLKIT_CLOSE': 'FULLKIT_CLOSE', // 完全加工已审核
        'FULLKIT_NOT': 'FULLKIT_NOT', // 完全加工不审核
        'CLOSE': 'CLOSE', // 关闭订单
        'CANCEL': 'CANCEL'// 取消订单
      }
      if (!kitOrderHeader || !kitOrderHeader.jobId || kitOrderHeader.jobId === '') {
        return orderStatus.NEW
      }
      // 创建
      if (kitOrderHeader.status === '00') {
        if (kitOrderHeader.auditStatus === '00') {
          return orderStatus.CREATE_NEW
        }
        if (kitOrderHeader.auditStatus === '99') {
          return orderStatus.CREATE_CLOSE
        }
        if (kitOrderHeader.auditStatus === '90') {
          return orderStatus.CREATE_NOT
        }
      }
      // 部分加工
      if (kitOrderHeader.status === '70') {
        if (kitOrderHeader.auditStatus === '99') {
          return orderStatus.PARTKIT_CLOSE
        }
        if (kitOrderHeader.auditStatus === '90') {
          return orderStatus.PARTKIT_NOT
        }
      }
      // 完全加工
      if (kitOrderHeader.status === '80') {
        if (kitOrderHeader.auditStatus === '99') {
          return orderStatus.FULLKIT_CLOSE
        }
        if (kitOrderHeader.auditStatus === '90') {
          return orderStatus.FULLKIT_NOT
        }
      }
      // 关闭订单
      if (kitOrderHeader.status === '99') {
        return orderStatus.CLOSE
      }
      // 取消订单
      if (kitOrderHeader.status === '90') {
        return orderStatus.CANCEL
      }
    },
    // 编辑行
    handleEdit (index, row) {
      const kitOrderStatus = this.getOrderStatus(row)
      if (kitOrderStatus === 'CLOSE' || kitOrderStatus === 'CANCEL' || kitOrderStatus === 'FULLKIT_NOT' || kitOrderStatus === 'FULLKIT_CLOSE') {
        return this.$message('关闭订单、取消订单、完全加工状态不能编辑')
      }

      const auditStatus = row.auditStatus
      const headUrl = this.$API.maintain.WmKitOrderList.getHead
      const parentUrl = this.$API.maintain.WmKitOrderList.getparent
      const subUrl = this.$API.maintain.WmKitOrderList.getsub
      const param = { kitNo: row.kitNo }
      this.$https.all([this.getData(headUrl, param), this.getData(parentUrl, param), this.getData(subUrl, param)]).then(res => {
        const head = res[0].data
        const parent = res[1].rows
        const sub = res[2].rows
        this.$emit('handleOpenEditForm', 'edit', head, parent, sub, auditStatus)
      })
    },

    // 详情
    handleOpenDetail (kitNo) {
      const headUrl = this.$API.maintain.WmKitOrderList.getHead
      const parentUrl = this.$API.maintain.WmKitOrderList.getparent
      const subUrl = this.$API.maintain.WmKitOrderList.getsub
      const queryKitUrl = this.$API.maintain.WmKitOrderList.queryKit
      const queryKitDetailUrl = this.$API.maintain.WmKitOrderList.queryKitDetail
      const param = { kitNo }
      this.$https.all([this.getData(headUrl, param), this.getData(parentUrl, param), this.getData(subUrl, param), this.getData(queryKitUrl, param), this.getData(queryKitDetailUrl, param)]).then(res => {
        const head = res[0].data
        const parent = res[1].rows
        const sub = res[2].rows
        const queryKit = res[3].rows
        const queryKitDetail = res[4].rows
        this.$emit('handleOpenEditForm', 'detail', head, parent, sub, queryKit, queryKitDetail)
      })
    },

    // 删除行
    handleDelete (index, row) {
      const url = this.$API.maintain.WmKitOrderList.delList
      this.remove.push(row.kitNo)
      const param = { kitNos: JSON.stringify(this.remove) }

      this.$confirm('是否删除此条数据？', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$https.post(url, param).then(res => {
          this.$emit('getList')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

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