<template>
  <el-drawer :visible.sync="show" title="产品选择" @close="handleCancelClick" :size="size" :close-on-click-modal="false" append-to-body>
    <div style="width: 90%;margin: 0 auto;">
        <div class="ed_title">
          <span>产品明细</span>
          <span style="margin-left: 10px;">已选：{{ selectItemSkuVoCheck.length }}</span>
        </div>
        <div style="width: 100%;max-height: 65vh;overflow-y: auto;margin-top: 20px;">
          <el-table :data="list" @selection-change="handleSelectionChange" border :row-key="row=>row.id" empty-text="暂无产品" v-loading="loading" ref="skuSelectFormRef" cell-class-name="my-cell">
            <el-table-column type="selection" width="55" :reserve-selection="true" v-if="!singleSelect" :selectable="judgeSelectable"/>
            <el-table-column label="产品名称" prop="itemName"></el-table-column>
            <el-table-column label="规格型号" prop="skuName"> </el-table-column>
            <el-table-column label="规格单位" prop="unit"></el-table-column>
            <el-table-column label="产品编号" prop="serialnumber"></el-table-column>
          </el-table>
        </div>
      <div style="width: 100%;display: flex;justify-content: space-between;margin-top: 20px;">
        <span></span>
        <span>
          <el-button @click="handleCancelClick">取消</el-button>
          <el-button type="primary" @click="handleOkClick" v-if="!singleSelect">确认</el-button>
      </span>
      </div>
    </div>
  </el-drawer>
</template>
<script setup name="SkuSelect">
import {computed, ref} from 'vue';
import  {getReceiptDetailByProjectId } from "@/api/wms/receiptOrder";
import {useWmsStore} from '@/store_v3/modules/wms'
useWmsStore().getItemBrandList();
useWmsStore().getProductList();

const loading = ref(false)
const selectItemSkuVoCheck = ref([])
const skuSelectFormRef = ref()
const selectNos = ref([])
const list = ref([]);
const orderId = ref('');

const getList = () => {
  getReceiptDetailByProjectId(orderId.value).then((res) => {
    let arr = [];
    res.data.forEach(d=>{
      d.serialnumbers.forEach(s=>{
        let temp = JSON.parse(JSON.stringify(d));
          temp.itemName= temp.item.itemName,
          temp.skuId= temp.skuId,
          temp.skuName= temp.itemSku.skuName,
          temp.unit= temp.item.unit
          temp.serialnumber = s;
        arr.push(temp);
      })
    })
    list.value = arr.map((it) => ({...it, id: it.id, checked: false}));
  }).then(()=>{toggleSelection()}).finally(() => loading.value = false);
}

// 定义props
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  size: {
    type: String,
    default: '30%'
  },
  singleSelect:{
    type: Boolean,
    default: false
  },
  selectedSku: {
    type: Array,
    default: []
  }
})

const show = computed(() => {
  return props.modelValue;
})

// 定义事件
const emit= defineEmits(['handleCancelClick', 'handleOkClick','handleSingleOkClick'])

function handleCancelClick() {
  emit('handleCancelClick');
  clearQuantity()
}


function handleOkClick() {
  emit('handleOkClick', selectItemSkuVoCheck.value);
  clearQuantity()
}
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  selectItemSkuVoCheck.value = selection
}

const toggleSelection = () => {
  list.value.forEach((l,i)=>{
    if (selectNos.value.includes(l.serialnumber)) {
      skuSelectFormRef.value.toggleRowSelection(list.value[i], true)
    } 
  })
}

const judgeSelectable = (row) => {
  if (selectNos.value.find(selected => selected === row.serialnumber)) {
    return false;
  }
  return true
}

function clearQuantity() {
  skuSelectFormRef.value.clearSelection()
}

const setWarehouseId = (warehouseId = null) => {
  // queryParams.warehouseId = warehouseId
}
defineExpose({
  getList,
  toggleSelection,
  setWarehouseId,
  orderId,
  selectNos
})
</script>
<style lang="scss">
.el-table .my-cell {
  vertical-align: top
}
</style>
<style scoped lang="scss">
::v-deep .el-drawer__wrapper .el-drawer__body{
  overflow-y: auto !important;
}

.ed_title{
  display: flex;
  align-items: center;
  span{
    font-family: PingFang-SC, PingFang-SC;
    font-weight: bold;
    font-size: 14px;
    color: #1D2129;
    line-height: 20px;
  }
}
</style>

