<!--
 Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

 https://www.mall4j.com/

 未经允许，不可做商业用途！

 版权所有，侵权必究！
-->
<template>
  <div class="sku-table-components">
    <el-form-item
      :label="$t('product.prodInfo.priceStock')"
      :label-width="'120px'"
    >
      <div class="sku-table-con">
        <!--批量设置-->
        <div
          v-if="lists.length>1"
          class="batch-settings-box"
        >
          <div class="set-txt">
            <span class="batch-set">{{ $t('product.prodInfo.batchSet') }}</span>
            <span class="weak-txt">{{ $t('product.prodInfo.selectContentFilling') }}</span>
          </div>
          <div class="batch-settings-tb">
            <el-form
              :model="dataFrom"
              :inline="true"
              class="batch-settings-form"
              @submit.prevent
            >
              <el-form-item
                v-for="(skuItem, index) in skuTags"
                :key="index"
              >
                <el-select
                  v-model="skuItem.value"
                  :placeholder="$t('tip.select')"
                  @change="$forceUpdate()"
                >
                  <el-option
                    :label="$t('date.a')"
                    value="全部"
                  />
                  <el-option
                    v-for="item in skuItem.tagItems"
                    :key="item.attrId"
                    :label="item.propValue"
                    :value="item.propValue"
                  />
                </el-select>
              </el-form-item>
              <!-- 价格 -->
              <el-form-item>
                <el-input
                  v-model="dataFrom.price"
                  type="number"
                  :max="100000000"
                  :min="0.01"
                  :step="0.01"
                  :placeholder="$t('product.prodInfo.supplyPrice')"
                  @blur="handleInputValue(dataFrom.price, null, 'price', 0.01, 100000000)"
                />
              </el-form-item>
              <el-form-item>
                <div class="stock-number">
                  <el-input
                    v-model.number="dataFrom.stocks"
                    type="number"
                    :placeholder="$t('product.prodInfo.stock')"
                    :disabled="spuMold === 2"
                    :max="9999999"
                    :min="0"
                    onkeyup="value=value.replace(/[^\d]/g,'')"
                    @keyup=" dataFrom.stocks = String(dataFrom.stocks).match(/[^0-9]{1,7}/) ? 0 : dataFrom.stocks "
                    @blur="handleInputValue(dataFrom.stocks, null, 'stocks', 0, 9999999)"
                  />
                </div>
              </el-form-item>
              <el-form-item>
                <el-input
                  v-model="dataFrom.marketPriceFee"
                  :placeholder="$t('product.prodInfo.marketValue')"
                  type="number"
                  :max="100000000"
                  :min="0"
                  :step="0.01"
                  @blur="handleInputValue(dataFrom.marketPriceFee, null, 'marketPriceFee', 0, 9999999)"
                />
              </el-form-item>
              <el-form-item>
                <el-input
                  v-model="dataFrom.volume"
                  :placeholder="$t('product.prodInfo.commodityVolume')+'(m³)'"
                  type="number"
                  :max="100000000"
                  :min="0"
                  :step="0.01"
                  @blur="handleInputValue(dataFrom.volume, null, 'volume', 0, 100000000)"
                />
              </el-form-item>
              <el-form-item>
                <el-input
                  v-model="dataFrom.weight"
                  :placeholder="$t('product.prodInfo.commodityWeight')+'(kg)'"
                  type="number"
                  :max="100000000"
                  :min="0"
                  :step="0.01"
                  @blur="handleInputValue(dataFrom.weight, null, 'weight', 0, 100000000)"
                />
              </el-form-item>
              <el-form-item>
                <el-input
                  v-model.number="dataFrom.stockWarning"
                  type="number"
                  :placeholder="$t('product.prodInfo.stockWarning')"
                  :max="9999999"
                  :min="0"
                  onkeyup="value=value.replace(/[^\d]/g,'')"
                  @keyup="
                    dataFrom.stockWarning = String(dataFrom.stockWarning).match(/[^0-9]{1,7}/) ? 0 : dataFrom.stockWarning
                  "
                  @blur="handleInputValue(dataFrom.stockWarning, null, 'stockWarning', 0, 9999999)"
                />
              </el-form-item>
              <el-form-item class="btn">
                <el-button
                  type="primary"
                  @click="batchSet"
                >
                  {{ $t('product.prodInfo.saveBatchSet') }}
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
        <!--sku列表-->
        <div class="table-con">
          <el-table
            :key="tableKey"
            :data="lists"
            header-cell-class-name="table-header"
            row-class-name="table-row"
            border
          >
            <el-table-column
              v-for="(leftTitle, index) in tableLeftTitles"
              :key="index"
              :label="leftTitle.tagName"
            >
              <template #default="scope">
                <div v-if="scope.row.propertiesEn && scope.row.properties">
                  {{ scope.row.properties.split(';')[index].substring(scope.row.properties.split(';')[index].indexOf(':') + 1) }}
                </div>
              </template>
            </el-table-column>
            <!-- 售价 -->
            <el-table-column
              prop="price"
              :label="$t('product.prodInfo.supplyPrice')"
            >
              <template #header>
                <span>
                  供货价(元)
                </span>
              </template>
              <template #default="scope">
                <el-input
                  v-model="scope.row.price"
                  type="number"
                  :min="0"
                  :max="9999999.99"
                  :step="0.01"
                  :precision="2"
                  :disabled="!scope.row.status || scope.row.isParticipate === 1"
                  @blur="handleInputValue(scope.row.price, scope.$index, 'price', 0.01, 9999999.99)"
                />
              </template>
            </el-table-column>
            <!-- 库存 -->
            <el-table-column
              prop="stocks"
              :label="$t('product.prodInfo.stock')"
            >
              <template #header>
                <span>
                  库存
                  <el-tooltip
                    v-if="spuMold === 2"
                    class="box-item"
                    effect="dark"
                    content="组合商品库存是由所关联商品的库存及搭配数共同决定，不能单独编辑"
                    placement="top"
                  >
                    <el-icon style="top: 2px"><QuestionFilled /></el-icon>
                  </el-tooltip>
                </span>
              </template>
              <template #default="scope">
                <div class="stock-number">
                  <el-input
                    v-model.number="scope.row.stocks"
                    type="number"
                    :max="9999999"
                    :min="0"
                    :step="1"
                    :disabled="!scope.row.status || spuMold === 2 || (spuMold === 0 && stockPointInfo.warehouseSize>1)"
                    :class="[spuMold===0 && scope.row.status!==0?'shade-input':null]"
                    onkeyup="value=value.replace(/[^\d]/g,'')"
                    @keyup="
                      scope.row.stocks = String(scope.row.stocks).match(/[^0-9]/) ? '' : scope.row.stocks
                    "
                    @blur="handleInputValue(scope.row.stocks, scope.$index, 'stocks', 0, 9999999)"
                  />
                  <div
                    v-if="spuMold !== 1 && spuMold !== 2 && scope.row.status && stockPointInfo.warehouseSize>1"
                    class="shade"
                    @click="onSetStock(scope)"
                  />
                </div>
              </template>
            </el-table-column>
            <!-- 市场价 -->
            <el-table-column
              prop="marketPriceFee"
              :label="$t('product.prodInfo.marketValue')"
            >
              <template #default="scope">
                <el-input
                  v-model="scope.row.marketPriceFee"
                  type="number"
                  :min="0"
                  :max="9999999.99"
                  :step="0.01"
                  :precision="2"
                  :disabled="!scope.row.status"
                  @blur="handleInputValue(scope.row.marketPriceFee, scope.$index, 'marketPriceFee', 0.01, 9999999.99)"
                />
              </template>
            </el-table-column>
            <!-- 体积 -->
            <el-table-column
              prop="volume"
              :label="$t('product.prodInfo.commodityVolume')+'(m³)'"
            >
              <template #default="scope">
                <el-input
                  v-model="scope.row.volume"
                  type="number"
                  :max="100000000"
                  :min="0"
                  :step="0.01"
                  :disabled="!scope.row.status"
                  @blur="handleInputValue(scope.row.volume, scope.$index, 'volume', 0, 100000000)"
                />
              </template>
            </el-table-column>
            <!-- 重量 -->
            <el-table-column
              prop="weight"
              :label="$t('product.prodInfo.commodityWeight')+'(kg)'"
            >
              <template #default="scope">
                <el-input
                  v-model="scope.row.weight"
                  type="number"
                  :max="100000000"
                  :min="0"
                  :step="0.01"
                  :disabled="!scope.row.status"
                  @blur="handleInputValue(scope.row.weight, scope.$index, 'weight', 0, 100000000)"
                />
              </template>
            </el-table-column>
            <!-- 库存预警 -->
            <el-table-column
              prop="stockWarning"
              :label="$t('product.prodInfo.stockWarning')"
            >
              <template #default="scope">
                <el-input
                  v-model.number="scope.row.stockWarning"
                  type="number"
                  :max="9999999"
                  :min="0"
                  :precision="0"
                  :step="1"
                  :disabled="!scope.row.status"
                  onkeyup="value=value.replace(/[^\d]/g,'')"
                  @keyup="
                    scope.row.stockWarning = String(scope.row.stockWarning).match(/[^0-9]/) ? '' : scope.row.stockWarning
                  "
                  @blur="handleInputValue(scope.row.stockWarning, scope.$index, 'stockWarning', 0, 9999999)"
                />
              </template>
            </el-table-column>
            <!-- 编码 -->
            <el-table-column
              prop="partyCode"
              :label="$t('product.prodInfo.skuCode')"
            >
              <template #default="scope">
                <el-input
                  ref="partyCodeInt"
                  v-model="scope.row.partyCode"
                  maxlength="36"
                  :disabled="!scope.row.status"
                  @blur="validatePartyCode(scope)"
                />
              </template>
            </el-table-column>
            <el-table-column
              :label="$t('table.actions')"
              align="center"
            >
              <template #default="scope">
                <el-button
                  v-if="scope.row.status"
                  link
                  type="primary"
                  @click="changeSkuStatus(`${scope.$index}`)"
                >
                  {{ $t('table.disable') }}
                </el-button>
                <el-button
                  v-else
                  link
                  type="primary"
                  @click="changeSkuStatus(`${scope.$index}`)"
                >
                  {{ $t('table.enable') }}
                </el-button>
                <el-button
                  v-if="spuMold===2"
                  type="primary"
                  link
                  @click="onAssociationGoods(scope)"
                >
                  商品({{ scope.row.skuComboList?.length || 0 }})
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-form-item>
    <!-- 弹窗, 关联单品的弹窗 -->
    <association-goods
      v-if="associationGoodsVisible"
      ref="associationGoodsRef"
      @on-select-success="onSelectSuccess"
    />
    <!-- 库存设置 -->
    <stock-update
      v-if="prodinventoryVisible"
      ref="prodInventoryRef"
      :is-record-change="isRecordChange"
      @set-stock-callback="onSetStockCallback"
    />
  </div>
</template>

<script setup>
import { flatten as genFlatten } from '@/utils'
import { computed, onBeforeMount, watch } from 'vue'
import { validNoEmptySpace } from '@/utils/validate'
import { ElMessage } from 'element-plus'
import AssociationGoods from './association-goods/index.vue'
import * as api from '@/api/stock/stock.js'
import Big from 'big.js'

const props = defineProps({
  value: {
    default () {
      return []
    },
    type: Array
  },
  // 0-普通商品;1-虚拟商品;2-组合商品
  spuMold: {
    default: 0,
    type: Number
  },
  isUpdate: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['on-change-data', 'change', 'update:value'])

const prodStore = useProdStore()
const Data = reactive({
  // 表格数据
  lists: [
    {
      marketPriceFee: 0,
      partyCode: '',
      price: 0.01,
      prodName: '',
      status: 1,
      stocks: 0,
      volume: 0,
      weight: 0,
      stockWarning: 0,
      spuMold: props.spuMold,
      skuComboList: []
    }
  ],
  rowspan: [],
  // 数据库中的规格
  dbSpecs: [],
  // 根据选定的规格所查询出来的规格值
  dbSpecValues: [],
  specs: [], // 使用的规格
  initIsCompose: false,
  initing: false,
  dataFrom: {
    marketPriceFee: null,
    price: null,
    stocks: null,
    weight: null,
    volume: null,
    stockWarning: null,
    partyCode: null
  },
  partyCodeErrTips: false,
  skuList: []
})

const { lists, dataFrom } = toRefs(Data)

const tableKey = ref(Symbol(''))

const tableLeftTitles = computed(() => {
  return skuTags.value
})

const skuTags = computed({
  get () {
    return prodStore.skuTags.filter(item => {
      item.value = '全部'
      return !!(item.tagItems && item.tagItems.length && !item.tagItems[0].creating)
    })
  }
})

watch(() => skuTags.value, (nval) => {
  Data.lists = genFlatten(skuTags.value, Data.lists, prodStore.defalutSku)
  computeRowspan()
  if (Data.skuList.length > 0) {
    // 数据回显
    Data.lists.forEach((item, index) => {
      item.marketPriceFee = parseFloat(bigActualTotal(parseFloat(Data.skuList[index].marketPriceFee), 100))
      item.partyCode = Data.skuList[index].partyCode
      item.price = parseFloat(bigActualTotal(parseFloat(Data.skuList[index].priceFee), 100))
      item.skuName = Data.skuList[index].skuName
      item.status = Data.skuList[index].status
      item.stocks = Data.skuList[index].stock
      item.stockPointList = Data.skuList[index].stockPointList
      item.volume = Data.skuList[index].volume
      item.weight = Data.skuList[index].weight
      item.stockWarning = Data.skuList[index].stockWarning
      item.skuId = Data.skuList[index].skuId || ''
      item.skuComboList = Data.skuList[index].skuComboList || []
    })
    Data.skuList = []
  }

  tableKey.value = Symbol(nval)
}, {
  deep: true
})

watch(() => props.spuMold, (val) => {
  if (!Data.initIsCompose) {
    return
  }
  if (val === 0) {
    // 清除已选择的单品
    clearSingleProds()
  } else if (!props.isUpdate) {
    // 清除库存
    clearStocks()
  }
  Data.dataFrom.stocks = null
})

const stockPointInfo = ref({})
// 未获取默认仓库id，则需要获取默认仓库id
const getDefaultWarehouse = () => {
  return new Promise((resolve) => {
    if (!stockPointInfo.value.defaultWarehouseId) {
      api.getStockPointInfo().then((res) => {
        stockPointInfo.value = res
        resolve()
      })
    } else {
      resolve()
    }
  })
}
watch(() => Data.lists, async (val) => {
  await getDefaultWarehouse()
  emit('on-change-data', val, stockPointInfo.value.defaultWarehouseId)
}, {
  deep: true,
  immediate: true
})

onBeforeMount(() => {
  Data.initIsCompose = true
})

const init = (skuList) => {
  Data.initing = true
  Data.lists = genFlatten(skuTags.value, skuList, prodStore.defalutSku)
  computeRowspan()
}

// 精度运算-除法
const bigActualTotal = (a, b) => {
  if (a == null) {
    return ''
  }
  return new Big(a).div(b).valueOf()
}

const computeRowspan = () => {
  Data.rowspan = []
  const rowspan = (index) => {
    const span = []
    let dot = 0
    Data.lists.forEach((item, idx) => {
      if (idx === 0) {
        span.push(1)
      } else {
        if (checkIsEqualByIndex(item.properties, Data.lists[idx - 1].properties, index)) {
          span[dot] += 1
          span.push(0)
        } else {
          dot = idx
          span.push(1)
        }
      }
    })

    Data.rowspan.push(span)
  }
  // eslint-disable-next-line no-unused-vars
  skuTags.value.forEach((item, index) => {
    rowspan(index)
  })
}

const checkIsEqualByIndex = (str1, str2, index) => {
  const strArr1 = str1.split(';')
  const strArr2 = str2.split(';')
  const index1 = strArr1[index].indexOf(':')
  const index2 = strArr2[index].indexOf(':')
  const temp1 = [strArr1[index].slice(0, index1), strArr1[index].substring(index1 + 1)]
  const temp2 = [strArr2[index].slice(0, index2), strArr2[index].substring(index2 + 1)]
  return temp1[1] === temp2[1]
}

const changeSkuStatus = (tagIndex) => {
  Data.lists[tagIndex].status = Data.lists[tagIndex].status ? 0 : 1
  emit('update:value', Data.lists)
}

const skuAddProdName = () => {
  if (Data.initing) {
    return
  }
  const skuList = []
  emit('update:value', Data.lists)
  for (let i = 0; i < Data.lists.length; i++) {
    const sku = Object.assign({}, Data.lists[i])
    if (!sku.properties || !sku.propertiesEn) {
      return
    }
    sku.skuName = ''
    sku.skuNameEn = ''
    const properties = sku.properties.split(';')
    const propertiesEn = sku.propertiesEn.split(';')
    for (const propertiesKey in properties) {
      sku.skuName += properties[propertiesKey].substring(properties[propertiesKey].indexOf(':') + 1) + ' '
    }
    for (const propertiesKey in propertiesEn) {
      sku.skuNameEn += propertiesEn[propertiesKey].substring(propertiesEn[propertiesKey].indexOf(':') + 1) + ' '
    }
    sku.prodNameCn = Data.prodNameCn + ' ' + sku.skuName
    sku.prodName = Data.prodNameCn + ' ' + sku.skuName
    sku.prodNameEn = Data.prodNameEn + ' ' + sku.skuNameEn
    skuList.push(sku)
  }
}

// 清除已选择的单品
const clearSingleProds = () => {
  for (let i = 0; i < Data.lists.length; i++) {
    Data.lists[i].skuSingleProds = []
  }
  emit('update:value', Data.lists)
}

// 清除所有sku的库存
const clearStocks = () => {
  for (let i = 0; i < Data.lists.length; i++) {
    Data.lists[i].stocks = 0
  }
  emit('update:value', Data.lists)
}

// 当前选中的列表序号
const newListIndex = ref(null)
const associationGoodsRef = ref(null)
const associationGoodsVisible = ref(false)
const onAssociationGoods = (scope) => {
  newListIndex.value = Number(scope.$index)
  associationGoodsVisible.value = true
  nextTick(() => {
    associationGoodsRef.value.openDialog(Data.lists[newListIndex.value]?.skuComboList || [])
  })
}

const onSelectSuccess = (data) => {
  Data.lists[newListIndex.value].skuComboList = data.list
  Data.lists[newListIndex.value].price = 0.01
  Data.lists[newListIndex.value].stocks = data.totalStock
}

const batchSet = () => {
  Data.lists.forEach(sku => {
    if (sku.properties) {
      const rowData = []
      sku.properties.split(';').forEach(el => {
        rowData.push(el.substring(el.indexOf(':') + 1))
      })
      const flag = skuTags.value.some((el, index) => {
        return el.value !== '全部' && el.value !== rowData[index]
      })
      if (flag) return
    }
    if (Data.dataFrom.partyCode) {
      sku.partyCode = Data.dataFrom.partyCode
    }
    if (Data.dataFrom.marketPriceFee || Data.dataFrom.marketPriceFee === 0) {
      sku.marketPriceFee = Data.dataFrom.marketPriceFee
    }
    if (Data.dataFrom.price) {
      sku.price = Data.dataFrom.price
    }
    if ((Data.dataFrom.stocks || Data.dataFrom.stocks === 0) && props.spuMold !== 2) {
      let isHaveDefaultWarehouse = false // 库存中是否存在默认仓库
      for (let i = 0; i < sku.stockPointList.length; i++) {
        if (sku.stockPointList[i].skuId) {
          // 存在skuId，则计算changeStock，并将默认仓库库存设置为Data.stockIntVal
          if (sku.stockPointList[i].type !== 0) {
            // 非默认仓库，库存改为0
            /* item.stockPointList[i].changeStock = -(item.stockPointList[i].stock - (item.stockPointList[i].changeStock || 0))
            item.stockPointList[i].stock = 0 */
            sku.stockPointList.splice(i, 1)
          } else {
            sku.stockPointList[i].changeStock = Data.dataFrom.stocks - sku.stockPointList[i].stock + (sku.stockPointList[i].changeStock || 0)
            sku.stockPointList[i].stock = Data.dataFrom.stocks
            isHaveDefaultWarehouse = true
          }
        } else {
          if (sku.stockPointList[i].type === 0) {
            isHaveDefaultWarehouse = true
            sku.stockPointList[i].stock = Data.dataFrom.stocks // 无skuId的默认仓库（此次修改新增的仓库库存，无需传changeStock）
            continue
          }
          sku.stockPointList.splice(i, 1) // 不存在skuId，则删除当前项
        }
      }
      if (!isHaveDefaultWarehouse) {
        // 不存在默认仓库，则存储一条默认仓库信息
        sku.stockPointList.push({
          stock: Data.dataFrom.stocks,
          stockPointId: stockPointInfo.value.defaultWarehouseId,
          stockPointType: 1,
          type: 0
        })
      }
      sku.stocks = Data.dataFrom.stocks
      Data.initing = false
    }
    if (Data.dataFrom.weight || Data.dataFrom.weight === 0) {
      sku.weight = Data.dataFrom.weight
    }
    if (Data.dataFrom.volume || Data.dataFrom.volume === 0) {
      sku.volume = Data.dataFrom.volume
    }
    if (Data.dataFrom.stockWarning || Data.dataFrom.stockWarning === 0) {
      sku.stockWarning = Data.dataFrom.stockWarning
    }
  })
  skuAddProdName()
}

/**
 * 处理输入框数据
 * @param data
 * @param index
 * @param dataFields
 * @param min 最小值
 * @param max 最大值
 */
const handleInputValue = (data, index, dataFields, min, max) => {
  if (index !== undefined && index !== null) {
    // 表格
    if (+data > max) {
      Data.lists[index][dataFields] = max
      if (dataFields === 'stocks') {
        emit('update:value', Data.lists)
      }
      return
    }
    if (dataFields === 'stocks' && stockPointInfo.value.warehouseSize === 1) {
      // 仅有一个仓库（默认仓库）时，直接修改库存时同步修改库存点列表
      Data.lists[index].stockPointList = [{
        stock: data || 0,
        stockPointId: stockPointInfo.value.defaultWarehouseId,
        stockPointType: 1,
        type: 0
      }]
    }
    if (+data <= min || !data) {
      Data.lists[index][dataFields] = min

      if (dataFields === 'stocks') {
        emit('update:value', Data.lists)
      }
      return
    }
    if (dataFields === 'price' ||
          dataFields === 'marketPriceFee' ||
          dataFields === 'volume' ||
          dataFields === 'weight' ||
          dataFields === 'stockWarning' ||
          dataFields === 'partyCode') {
      Data.lists[index][dataFields] = checkInput(data, dataFields === 'price' ? min : undefined)
    }
  } else {
    // 批量
    if (data === null || data === '') {
      return
    }
    if (+data > max) {
      Data.lists[index][dataFields] = max
      return
    }
    if (+data <= min) {
      Data.lists[index][dataFields] = min

      return
    }
    if (dataFields === 'price' ||
          dataFields === 'marketPriceFee' ||
          dataFields === 'volume' ||
          dataFields === 'weight' ||
          dataFields === 'stockWarning' ||
          dataFields === 'partyCode') {
      Data.lists[index][dataFields] = checkInput(data)
    }
  }
  if (dataFields === 'stocks') {
    emit('update:value', Data.lists)
  }
}

// 只允许输入正数和小数(保留小数点后两位)
const checkInput = (num, min = undefined) => {
  if (num) {
    let tmpVal = String(num).replace(/[^\d^\\.]/g, '')
    const reg = /^(0|([1-9]\d*))(\.\d{1,2})?$/ // 最多允许后输入两位小数
    if (!reg.test(tmpVal)) {
      let n = 0
      tmpVal = tmpVal + ''
      if (tmpVal.indexOf('.') !== -1) {
        tmpVal = tmpVal.substring(0, tmpVal.indexOf('.') + 3)
        n = (tmpVal.split('.')).length - 1
      } else {
        min && (tmpVal = min)
      }
      if (n > 1) {
        tmpVal = tmpVal.substring(0, tmpVal.indexOf('.'))
      }
    }
    return Number(tmpVal)
  } else {
    return ''
  }
}

// 编码输入框校验
const validatePartyCode = (scope) => {
  const { row, $index } = scope
  // 纯空格校验
  if (validNoEmptySpace(row.partyCode)) {
    Data.lists[$index].partyCode = ''
    return
  }
  // 商品编码重复校验
  check(row, $index)
}

const check = (row, $index) => {
  if (
    row.partyCode &&
        Data.lists.find((el, index) => el.partyCode &&
          index !== $index &&
          el.partyCode === row.partyCode)
  ) {
    ElMessage({
      message: '商品编码不能重复',
      type: 'error',
      duration: 1500
    })
  }
}

const prodinventoryVisible = ref(false)
const prodInventoryRef = ref(null)
const isRecordChange = ref(true) // 是否记录库存变化（changeStock）
const onSetStock = (scope) => {
  if (props.spuMold === 1) {
    // 虚拟商品库存为默认仓库
    return
  }
  const { $index, row } = scope
  isRecordChange.value = !!row.skuId // 新增的规格不记录库存变化
  prodinventoryVisible.value = true
  nextTick(() => {
    prodInventoryRef.value.init(row, $index)
  })
}

// 修改库存弹窗回调
const onSetStockCallback = (res, sum, index) => {
  Data.lists[index].stockPointList = res
  Data.lists[index].stocks = sum
}

defineExpose({
  init,
  Data
})
</script>

<style lang="scss" scoped>

.sku-table-components {

  // 表格+批量设置
  .sku-table-con {
    display: block;
    padding: 10px;
    border: 1px solid #DCDCDC;

    // 批量设置
    .batch-settings-box {
      .set-txt {
        padding-top: 10px;
        .batch-set{
          color: #155bd4;
        }
        .weak-txt {
          color: #999;
          font-size: 12px;
          margin-left: 5px;
        }
      }
      .batch-settings-tb {
        // 头部
        margin-bottom: 18px;

        .batch-settings-form{
          display: flex;

          :deep(.el-form-item ){
            margin-right: 10px;
            flex:1;
          }
          .btn {
            flex: 2;
            margin-right: 0;
            text-align: right;
          }
        }
      }
    }
  }

  :deep(.el-table .cell){
    word-break: break-word !important;
  }

  .filter-submitBtn span{
    color:#fff !important;
  }
  div :deep(.el-table tbody tr:hover>td) { background-color: #ffffff }

  .stock-number {
    position: relative;
    .shade {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      cursor: pointer;
    }
    :deep(.shade-input.is-disabled) {
      .el-input__wrapper {
        background-color: #ffffff;

        .el-input__inner {
          color: var(--el-input-text-color);
          -webkit-text-fill-color: var(--el-input-text-color);
        }
      }
    }
  }
}

</style>
