<template>
  <div class="itemList">
    <!-- 数据表格 -->
    <item-search
      :curRow="curRow"
      @search="reload"
      ref="refSeavch"
      @handledime="handledime"
    ></item-search>
    <ele-pro-table
      ref="table"
      :initLoad="false"
      :columns="columns"
      :datasource="datasource"
      :row-key="selectedDime==4?'no':'code'"
      :height="400"
      :toolkit="[]"
      tool-class="ele-toolbar-form"
      cache-key="item-listTable"
      :selection.sync="selection"
      @done="onDone"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar></template>
      <!-- 批次号 -->
      <template v-slot:batchNo="{ row }">
        <el-popover placement="right-start" width="800" trigger="hover">
          <el-table :data="row.outInBatchDetailsVOList">
            <el-table-column
              width="150"
              property="bizNo"
              label="入库单号"
            ></el-table-column>
            <el-table-column
              width="100"
              property="bizType"
              label="入库场景"
            ></el-table-column>
            <el-table-column
              width="120"
              property="count"
              label="入库数量"
            ></el-table-column>
            <el-table-column
              width="80"
              property="measuringUnit"
              label="计量单位"
            ></el-table-column>
            <el-table-column
              width="100"
              property="packageCount"
              label="包装数量"
            ></el-table-column>
            <el-table-column
              width="80"
              property="packingUnit"
              label="包装单位"
            ></el-table-column>
            <el-table-column
              width="330"
              property="position"
              label="库位"
            ></el-table-column>
            <el-table-column
              width="160"
              property="createTime"
              label="入库时间"
            ></el-table-column>
          </el-table>
          <span slot="reference"> {{ row.batchNo }}</span>
        </el-popover>
      </template>
      <!-- 最小包装单元 -->
      <template v-slot:minPackingCount="{ row }">
        <span v-if="row.minPackingCount">
          {{ row.minPackingCount }}
          {{ row.measuringUnit }}/{{ row.minUnit }}
        </span>
      </template>
      <!-- 库存保质期 -->
      <template v-slot:expirationDate="{ row }">
        <span v-if="row.expirationDate">
          {{ row.expirationDate ? row.expirationDate : '-' }}
          {{
            row.expirationDateUnit == 'year'
              ? '年'
              : row.expirationDateUnit == 'month'
                ? '月'
                : '日'
          }}
        </span>
      </template>
      <!-- 质检状态 -->
      <template v-slot:qualityStatus="{ row }">
        <span v-if="row.qualityResult == 0 || row.qualityResult == 1"
        >已检</span
        >
        <span v-else-if="row.qualityStatus == 1">已检</span>
        <span v-else-if="row.qualityStatus == 0">未检</span>
        <span v-else>-</span>
      </template>
      <!-- 质检结果 -->
      <template v-slot:qualityResult="{ row }">
        <span v-if="row.qualityResult == 0 || row.qualityResult == ''">合格</span>
        <span v-else-if="row.qualityResult == 1">不合格</span>
        <span v-else-if="row.qualityResult == 3">让步接收</span>
        <span v-else>-</span>
      </template>
    </ele-pro-table>
  </div>
</template>

<script>
import ItemSearch from './item-search.vue';
import {getInventoryDetails, getMaterielDetails} from "@/api/wms";

export default {
  components: {ItemSearch},
  props: {
    current: {
      type: Object,
      default: () => ({})
    },
    sendProductDetail: {
      type: Array,
      default: () => {
        return []
      }
    },
    curRow: {
      type: Object,
      default: () => {
        return {}
      }
    },
  },
  data() {
    return {
      selectedDime: '3',
      selection: [],
      where: {},
      index: 0
    };
  },
  computed: {
    // 表格列配置
    columns() {
      // selectedDime 1物品维度 2批次维度 3包装维度 4物料维度
      switch (this.selectedDime) {
        // 包装维度
        case '3':
          return [
            {
              width: 45,
              type: 'selection',
              columnKey: 'selection',
              reserveSelection: true,
              selectable: (row, index) => {
                let codes = this.sendProductDetail.map(item => item.code)
                return !codes.includes(row.code);
              },
              align: 'center'
            },
            {
              columnKey: 'index',
              type: 'index',
              width: 50,
              align: 'center',
              label: '序号',
              showOverflowTooltip: true,
              fixed: 'left'
            },
            {
              slot: 'batchNum',
              prop: 'batchNum',
              label: '批次号',
              showOverflowTooltip: true
            },
            {
              prop: 'packagingCode',
              label: '包装编码',
              showOverflowTooltip: true
            },
            {
              prop: 'categoryCode',
              label: '编号',
              showOverflowTooltip: true
            },
            {
              prop: 'name',
              label: '名称',
              showOverflowTooltip: true
            },
            {
              prop: 'brandNum',
              label: '牌号',
              showOverflowTooltip: true
            },
            {
              prop: 'modelType',
              label: '型号',
              showOverflowTooltip: true
            },
            {
              prop: 'specification',
              label: '规格',
              showOverflowTooltip: true
            },
            {
              prop: 'manualBatchNo',
              label: '批号',
              align: 'center'
            },
            {
              prop: 'barcodes',
              label: '发货条码',
              align: 'center'
            },
            {
              prop: 'engrave',
              label: '刻码',
              align: 'center'
            },
            {
              prop: 'packingCountBase',
              label: '包装库存数量',
              sortable: 'custom',
              showOverflowTooltip: true,
              width: 130,
              align: 'center'
            },
            {
              prop: 'minUnit',
              label: '包装单位',
              showOverflowTooltip: true
            },
            {
              prop: 'minPackingCount',
              slot: 'minPackingCount',
              label: '最小包装单元',
              showOverflowTooltip: true,
              width: 120
            },
            {
              prop: 'availableCountBase',
              label: '计量库存数量',
              sortable: 'custom',
              showOverflowTooltip: true,
              width: 130,
              align: 'center'
            },
            {
              prop: 'measuringUnit',
              label: '计量单位',
              align: 'center'
            },
            {
              prop: 'weight',
              label: '重量',
              showOverflowTooltip: true
            },
            {
              prop: 'weightUnit',
              label: '重量单位',
              showOverflowTooltip: true
            },
            {
              prop: 'expirationDate',
              slot: 'expirationDate',
              label: '库存保质期',
              showOverflowTooltip: true,
              width: 100
            },
            {
              prop: 'expirationTime',
              label: '周期倒计时',
              showOverflowTooltip: true,
              width: 100
            },
            {
              prop: 'qualityResult',
              slot: 'qualityResult',
              label: '质检结果',
              showOverflowTooltip: true,
              width: 100
            },
            {
              prop: 'qualityStatus',
              slot: 'qualityStatus',
              label: '质检状态',
              showOverflowTooltip: true,
              width: 100
            },
            {
              prop: 'paths',
              width: 230,
              label: '仓库',
              showOverflowTooltip: true
            },
            {
              columnKey: 'action',
              label: '操作',
              width: 100,
              align: 'left',
              resizable: false,
              slot: 'action',
              fixed: 'right'
            }
          ];
        // 物料维度
        default:
          return [
            {
              width: 45,
              type: 'selection',
              columnKey: 'selection',
              reserveSelection: true,
              selectable: (row, index) => {
                let codes = this.sendProductDetail.map(item => item.code)
                return !codes.includes(row.no);
              },
              align: 'center',
            },
            {
              columnKey: 'index',
              type: 'index',
              width: 50,
              align: 'center',
              label: '序号',
              showOverflowTooltip: true,
              fixed: 'left'
            },

            {
              slot: 'batchNo',
              prop: 'batchNo',
              label: '批次号',
              showOverflowTooltip: true
            },
            {
              prop: 'no',
              label: '物料编码',
              showOverflowTooltip: true
            },
            {
              prop: 'assetCode',
              label: '编号',
              showOverflowTooltip: true
            },
            {
              prop: 'name',
              label: '名称',
              showOverflowTooltip: true
            },
            {
              prop: 'brandNum',
              label: '牌号',
              showOverflowTooltip: true
            },
            {
              prop: 'modelType',
              label: '型号',
              showOverflowTooltip: true
            },
            {
              prop: 'specification',
              label: '规格',
              showOverflowTooltip: true
            },
            {
              prop: 'manualBatchNo',
              label: '批号',
              align: 'center'
            },
            {
              prop: 'barcodes',
              label: '发货条码',
              align: 'center'
            },
            {
              prop: 'engrave',
              label: '刻码',
              showOverflowTooltip: true
            },
            {
              prop: 'materielCode',
              label: '物料代号',
              showOverflowTooltip: true
            },
            {
              prop: 'clientCode',
              label: '客户代号',
              showOverflowTooltip: true
            },
            {
              prop: 'availableCountBase',
              label: '计量库存数量',
              sortable: 'custom',
              showOverflowTooltip: true,
              width: 130,
              align: 'center'
            },
            {
              prop: 'measuringUnit',
              label: '计量单位',
              align: 'center'
            },
            {
              prop: 'weight',
              label: '重量',
              showOverflowTooltip: true
            },
            {
              prop: 'weightUnit',
              label: '重量单位',
              showOverflowTooltip: true
            },
            {
              prop: 'expirationDate',
              slot: 'expirationDate',
              label: '库存保质期',
              showOverflowTooltip: true,
              width: 100
            },
            {
              prop: 'expirationTime',
              label: '周期倒计时',
              showOverflowTooltip: true,
              width: 100
            },
            {
              prop: 'qualityResult',
              slot: 'qualityResult',
              label: '质检结果',
              showOverflowTooltip: true,
              width: 100
            },
            {
              prop: 'qualityStatus',
              slot: 'qualityStatus',
              label: '质检状态',
              showOverflowTooltip: true,
              width: 100
            },
            {
              prop: 'paths',
              width: 230,
              label: '仓库',
              showOverflowTooltip: true
            },
            {
              columnKey: 'action',
              label: '操作',
              width: 100,
              align: 'left',
              resizable: false,
              slot: 'action',
              fixed: 'right'
            }
          ];
      }
    }
  },
  methods: {
    //复选框多选回显
    onDone(res) {
      // this.$nextTick(() => {
      //   if (this.index > 0) return
      //   const ids = this.selection.map(item => item.id);
      //   this.$refs.table.setSelectedRowKeys(ids);
      //   this.index++
      // });
    },
    handledime(val) {
      this.selectedDime = val;
      this.reload(this.where);
    },
    /* 表格数据源 */
    async datasource({page, limit, where, order}) {
      const dimension = this.$refs.refSeavch.dimension;
      console.log(this.current?.id);
      const treeId = this.current?.id;
      if (this.selectedDime == 4) {
        // 物料维度
        const params = {
          categoryLevelId: treeId,
          ...where,
          ...order,
          dimension: dimension,

        };
        const res = await getMaterielDetails({
          ...params,
          pageNum: page,
          size: limit
        });
        console.log('111111')
        const data = res.list.map((item) => {
          return {
            ...item,
            minUnit: item.packingUnit,
            paths: item.pathName,
            availableCountBase:
              item.availableCountBase < 0 ? 0 : item.availableCountBase,
            packingCountBase:
              item.packingCountBase < 0 ? 0 : item.packingCountBase,
            materialCode: item.code,
            batchNum: item.batchNo,
            qualityResult:
              typeof item.qualityResult != 'number'
                ? 3
                : item.qualityResult,
            qualityStatus:
              typeof item.qualityStatus != 'number' ? 3 : item.qualityStatus
          };
        });
        return {...res, list: data};

      } else {
        // 包装维度
        const params = {
          categoryLevelId: treeId,
          ...where,
          ...order,
          dimension: dimension
        };
        const data = getInventoryDetails({
          ...params,
          pageNum: page,
          size: limit
        });
        return data.then((res) => {
          const data = res.list.map((item) => {
            return {
              ...item,
              minUnit: item.packingUnit,
              paths: item.pathName.split(',')[0],
              availableCountBase:
                item.availableCountBase < 0 ? 0 : item.availableCountBase,
              packingCountBase:
                item.packingCountBase < 0 ? 0 : item.packingCountBase,
              packagingCode: item.code,
              qualityResult:
                typeof item.qualityResult != 'number'
                  ? 3
                  : item.qualityResult,
              qualityStatus:
                typeof item.qualityStatus != 'number' ? 3 : item.qualityStatus
            };
          });
          return {...res, list: data};
        });
      }
    },
    /* 刷新表格 */
    reload(where) {
      where.categoryCode = where.code
      if (this.selectedDime == 4) {
        this.$delete(where, 'categoryCode')
      }
      this.where = where
      this.$nextTick(async () => {
        await this.$refs.table.reload({
          pageNum: 1,
          where: {
            ...where,

            categoryLevelId: this.current?.id
          }
        });
      });
    },
    getSelectionList() {
      this.selection.forEach(item => item.dimension = this.selectedDime)
      return this.selection
    },
  },

  watch: {
    // 监听机构id变化
    current: {
      handler() {
        this.reload(this.where);
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.itemList{
  :deep .el-checkbox__input .is-disabled .el-checkbox__inner {
    background-color: #262626 !important;
    border-color: #181818 !important;
    cursor: not-allowed;
  }
}
</style>
