<template>
  <div class="pages-wrap">
    <!-- 列表 -->
    <a-table
      class="sTable"
      style="margin-top:10px;width:100%;"
      ref="table"
      size="small"
      :pagination="false"
      :rowKey="(record) => record.productScopeSn"
      :columns="columns"
      :data-source="dataSource"
      bordered>
      <!-- 产品分类 -->
      <template slot="productType" slot-scope="text, record,index">
        <div style="max-height:126px;width:100%;overflow-y:auto;" v-if="record.dataSourceOrigin=='0'">
          <div style="display: flex;align-items: center;" v-if="record.productTypeArr && record.productTypeArr.length>0">
            <div style="max-height:126px;overflow-y:auto;flex-grow: 1;display: flex;flex-wrap: wrap;">
              <div v-for="con in record.productTypeArr" :key="con.id">
                <a-tooltip v-if="con.title&&con.title.length > 7" :title="con.title">
                  <a-tag style="margin-bottom:10px;" :id="'tableaaType-delType'+record.productScopeSn" closable @close.prevent="delLabel(record.productScopeSn,con,'typeList')">
                    {{ `${con.title.slice(0, 7)}...` }}
                  </a-tag>
                </a-tooltip>
                <a-tag v-else style="margin-bottom:10px;" :id="'tableaaType-delType'+record.productScopeSn" closable @close.prevent="delLabel(record.productScopeSn,con,'typeList')">
                  {{ con.title }}
                </a-tag>
              </div>
            </div>
            <div style="text-align:center;padding-left:10px;">
              <a-tag style="background: #fff; borderStyle: dashed;cursor:pointer" title="添加分类" :id="'tableaaType-addType'+record.productScopeSn" @click="addClassifyTag(index, record)" color="blue">
                <a-icon type="plus" />
              </a-tag>
            </div>
          </div>
          <div v-else>
            <a-tag style="background: #fff; borderStyle: dashed;cursor:pointer" title="添加分类" :id="'tableaaType-addType'+record.productScopeSn" @click="addClassifyTag(index, record)" color="blue">
              <a-icon type="plus" />
            </a-tag>
          </div>
        </div>
        <span v-else v-for="con in record.productTypeArr" :key="con.id">{{ con.title }}</span>
      </template>
      <!-- 品牌 -->
      <template slot="productBrand" slot-scope="text, record,index">
        <div v-if="record.dataSourceOrigin=='0'">
          <div style="display: flex;align-items: center;" v-if="record.productBrandArr && record.productBrandArr.length>0">
            <div :span="18" style="max-height:126px;width:100%;overflow-y:auto;">
              <div v-for="item in record.productBrandArr" :key="item.brandSn">
                <a-tooltip v-if="item.brandName&&item.brandName.length > 7" :title="item.brandName">
                  <a-tag style="margin-bottom:10px;" :id="'tableaaType-delBrand'+record.productScopeSn" closable @close.prevent="delLabel(record.productScopeSn,item,'brandList')">
                    {{ `${item.brandName.slice(0, 7)}...` }}
                  </a-tag>
                </a-tooltip>
                <a-tag v-else style="margin-bottom:10px;" :id="'tableaaType-delBrand'+record.productScopeSn" closable @close.prevent="delLabel(record.productScopeSn,item,'brandList')">
                  {{ item.brandName }}
                </a-tag>
              </div>
            </div>
            <div style="text-align:center;padding-left:10px;">
              <a-tag style="background: #fff; borderStyle: dashed;cursor:pointer" title="添加品牌" :id="'tableaaType-addBrand'+record.productScopeSn" @click="addBrandTag(index, record)" color="blue">
                <a-icon type="plus" />
              </a-tag>
            </div>
          </div>
          <div v-else>
            <a-tag style="background: #fff; borderStyle: dashed;cursor:pointer" title="添加品牌" :id="'tableaaType-addBrand'+record.productScopeSn" @click="addBrandTag(index, record)" color="blue">
              <a-icon type="plus" />
            </a-tag>
          </div>
        </div>
        <span v-else v-for="item in record.productBrandArr" :key="item.brandSn">{{ item.brandName }}</span>
      </template>
      <!-- 产品 -->
      <template slot="product" slot-scope="text, record">
        <span v-if="record.dataSourceOrigin=='1'" v-for="item in record.productThisList" :key="item.productSn">{{ item.productCode }}</span>
        <span v-else>--</span>
      </template>
      <!-- 省价特价 -->
      <template slot="provincePrice" slot-scope="text, record">
        <div v-if="rulesType==='0'">
          <div v-if="record.dataSourceOrigin=='1'">
            <a-input-number
              size="small"
              style="width:90%;"
              v-model="record.provinceDiscountPrice"
              :id="'tableaaType-provinceDiscountPrice'+record.productScopeSn"
              :min="0"
              :step="1"
              :precision="2"
              placeholder="请输入"
              :max="99999999"
              @blur="e=>calculatePrice(e.target.value,record.productScopeSn,'province')"/>
          </div>
          <span v-else>--</span>
        </div>
        <div v-else >
          <div v-if="record.provincePrice" style="width:90%;background:#ccc;border-radius: 3px;text-align:center;">
            <span v-if="rulesType=='1'">{{ (setData[0].provinceValue||setData[0].provinceValue==0)?(record.provincePrice*setData[0].provinceValue/100).toFixed(2):'--' }}</span>
            <span v-else>{{ (setData[0].provinceValue||setData[0].provinceValue==0)?(record.provincePrice - setData[0].provinceValue).toFixed(2):'--' }}</span>
          </div>
          <span v-else>--</span>
        </div>
      </template>
      <!-- 省价折扣 -->
      <template slot="provinceDiscount" slot-scope="text, record">
        <div v-if="rulesType=='0'">
          <div v-if="record.dataSourceOrigin=='0'">
            <a-input-number
              size="small"
              style="width:80%;"
              v-model="record.provinceDiscountRate"
              :id="'tableaaType-provinceDiscountRate'+record.productScopeSn"
              :min="0"
              :step="1"
              :precision="2"
              placeholder="请输入"
              :max="99999999"/>%
          </div>
          <span v-else>{{ (record.provinceDiscountRate||record.provinceDiscountRate==0)?record.provinceDiscountRate+'%':'--' }}</span>
        </div>
        <div v-else>
          <span v-if="rulesType=='1'">{{ (setData[0].provinceValue||setData[0].provinceValue==0)?setData[0].provinceValue.toFixed(2)+'%':'--' }}</span>
          <span v-else>{{ setData[0].provinceValue?'-'+(setData[0].provinceValue).toFixed(2):setData[0].provinceValue==0?'0.00':'--' }}</span>
        </div>
      </template>
      <!-- 市价特价 -->
      <template slot="cityPrice" slot-scope="text, record">
        <div v-if="rulesType==='0'">
          <div v-if="record.dataSourceOrigin=='1'">
            <a-input-number
              size="small"
              style="width:90%;"
              v-model="record.cityDiscountPrice"
              :min="record.provinceDiscountPrice || 0"
              :step="1"
              :precision="2"
              :id="'tableaaType-cityDiscountPrice'+record.productScopeSn"
              placeholder="请输入"
              :max="99999999"
              :disabled="rulesType!='0'"
              @blur="e=>calculatePrice(e.target.value,record.productScopeSn,'city')"/>
          </div>
          <span v-else>--</span>
        </div>
        <div v-else >
          <div v-if="record.cityPrice" style="width:90%;background:#ccc;border-radius: 3px;text-align:center;">
            <span v-if="rulesType=='1'">{{ (setData[0].cityValue||setData[0].cityValue==0)?(record.cityPrice*setData[0].cityValue/100).toFixed(2):'--' }}</span>
            <span v-else>{{ (setData[0].cityValue||setData[0].cityValue==0)?(record.cityPrice - setData[0].cityValue).toFixed(2):'--' }}</span>
          </div>
          <span v-else>--</span>
        </div>
      </template>
      <!-- 市价折扣 -->
      <template slot="cityDiscount" slot-scope="text, record">
        <div v-if="rulesType=='0'">
          <div v-if="record.dataSourceOrigin=='0'">
            <a-input-number
              size="small"
              style="width:80%;"
              v-model="record.cityDiscountRate"
              :min="record.provinceDiscountRate ||0"
              :id="'tableaaType-cityDiscountRate'+record.productScopeSn"
              :step="1"
              :precision="2"
              placeholder="请输入"
              :max="99999999"/>%
          </div>
          <span v-else>{{ (record.cityDiscountRate||record.cityDiscountRate==0)?record.cityDiscountRate+'%':'--' }}</span>
        </div>
        <div v-else>
          <span v-if="rulesType=='1'">{{ (setData[0].cityValue||setData[0].cityValue==0)?setData[0].cityValue.toFixed(2)+'%':'--' }}</span>
          <span v-else>{{ setData[0].cityValue?'-'+(setData[0].cityValue).toFixed(2):setData[0].cityValue==0?'0.00':'--' }}</span>
        </div>
      </template>
      <!-- 特约特价 -->
      <template slot="specialPrice" slot-scope="text, record">
        <div v-if="rulesType==='0'">
          <div v-if="record.dataSourceOrigin=='1'">
            <a-input-number
              size="small"
              style="width:90%;"
              v-model="record.specialDiscountPrice"
              :min="record.cityDiscountPrice ||0"
              :id="'tableaaType-specialDiscountPrice'+record.productScopeSn"
              :step="1"
              :precision="2"
              placeholder="请输入"
              :max="99999999"
              :disabled="rulesType!='0'"
              @blur="e=>calculatePrice(e.target.value,record.productScopeSn,'special')"/>
          </div>
          <span v-else>--</span>
        </div>
        <div v-else>
          <div v-if="record.specialPrice" style="width:90%;background:#ccc;border-radius: 3px;text-align:center;">
            <span v-if="rulesType=='1'">{{ (setData[0].specialValue||setData[0].specialValue==0)?(record.specialPrice*setData[0].specialValue/100).toFixed(2):'--' }}</span>
            <span v-else>{{ (setData[0].specialValue||setData[0].specialValue==0)?(record.specialPrice - setData[0].specialValue).toFixed(2):'--' }}</span>
          </div>
          <span v-else>--</span>
        </div>
      </template>
      <!-- 特约折扣 -->
      <template slot="specialDiscount" slot-scope="text, record">
        <div v-if="rulesType=='0'">
          <div v-if="record.dataSourceOrigin=='0'">
            <a-input-number
              size="small"
              style="width:80%;"
              v-model="record.specialDiscountRate"
              :min="record.cityDiscountRate ||0"
              :id="'tableaaType-specialDiscountRate'+record.productScopeSn"
              :step="1"
              :precision="2"
              placeholder="请输入"
              :max="99999999"/>%
          </div>
          <span v-else>{{ (record.specialDiscountRate||record.specialDiscountRate==0) ? record.specialDiscountRate+'%':'--' }}</span>
        </div>
        <div v-else>
          <span v-if="rulesType=='1'">{{ (setData[0].specialValue||setData[0].specialValue==0)?setData[0].specialValue.toFixed(2)+'%':'--' }}</span>
          <span v-else>{{ setData[0].specialValue?'-'+(setData[0].specialValue).toFixed(2):setData[0].specialValue==0?'0.00':'--' }}</span>
        </div>
      </template>
      <!-- 设置起订量 -->
      <template slot="setNum" slot-scope="text, record">
        <a-select
          default-value="SL"
          v-model="record.unitType"
          size="small"
          style="width:100%;"
          placeholder="请选择"
          :id="'tableaaType-unitType'+record.productScopeSn"
          allowClear>
          <a-select-option :value="con.code" v-for="con in unitTypeList" :key="con.id">
            {{ con.dispName }}
          </a-select-option>
        </a-select>
        <a-input-number
          size="small"
          style="margin-top:8px;width:100%;"
          v-model="record.unitQty"
          :min="0"
          :step="1"
          :precision="0"
          :id="'tableType-unitQty'+record.productScopeSn"
          :max="99999999"
          placeholder="请输入"/>
      </template>
      <span slot="action" slot-scope="text,record">
        <a-button
          size="small"
          type="link"
          class="button-error"
          :id="'tableType-del-btn'+record.productScopeSn"
          @click="handleDel(record)"
        >
          删除
        </a-button>
      </span>
    </a-table>
    <div class="btn-cont" v-if="rulesType!='0'">
      <a-button id="tableType1-add" type="primary" class="button-gray" @click="handleAddRow">+新增一行</a-button>
    </div>
    <!-- 选择产品品牌 -->
    <chooseBrandModal
      pageType="1"
      ref="brandBox"
      :openModal="openBrandModal"
      :chooseData="chooseBrand"
      @close="openBrandModal=false"
      @ok="handleBrandOk" />
    <!-- 选择产品分类 -->
    <chooseTypeModal
      ref="typeBox"
      :openModal="openTypeModal"
      :linkageStatus="false"
      :chooseData="chooseType"
      @close="openTypeModal=false"
      @ok="handleTypeOk" />
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { VSelect } from '@/components'
import ChooseBrandModal from '@/views/common/chooseBrandModal.vue'
import ChooseTypeModal from '@/views/common/chooseTypeModal.vue'
export default {
  name: 'TableType3',
  mixins: [commonMixin],
  components: { VSelect, ChooseBrandModal, ChooseTypeModal },
  props: {
    itemSn: {
      type: [Number, String],
      default: ''
    },
    rulesType: {// 特价规则类型  0 手动 1折扣 2直降
      type: [Number, String],
      default: ''
    },
    setData: {
      type: Array,
      default: () => []
    },
    unitTypeList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      spinning: false,
      dataSource: [], // 列表数据
      openBrandModal: false, // 打开产品品牌弹窗
      openTypeModal: false, // 打开产品分类弹窗
      chooseBrand: [], // 品牌数据
      chooseType: [], // 分类数据
      val: '',
      brandTagIndex: null, // 所选品牌位置
      classifyTagIndex: null, // 所选分类位置
      dataSourceOrigin: 0 // 数据来源  1  选择产品  0 新增一行品类
    }
  },
  computed: {
    columns () {
      const _this = this
      var arr = [
        { title: '产品分类', width: '17%', scopedSlots: { customRender: 'productType' }, align: 'center' },
        { title: '品牌', width: '17%', scopedSlots: { customRender: 'productBrand' }, align: 'center' },
        { title: '产品', scopedSlots: { customRender: 'product' }, width: '10%', align: 'center' },
        { title: '省级原价', dataIndex: 'provincePrice', width: '5%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '省级特价', scopedSlots: { customRender: 'provincePrice' }, width: '5%', align: 'center' },
        // { title: '省级折扣', scopedSlots: { customRender: 'provinceDiscount' }, width: '5%', align: 'center' },
        { title: '市级原价', dataIndex: 'cityPrice', width: '5%', align: 'center', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '市级特价', scopedSlots: { customRender: 'cityPrice' }, width: '5%', align: 'center' },
        // { title: '市级折扣', scopedSlots: { customRender: 'cityDiscount' }, width: '5%', align: 'center' },
        { title: '特约原价', dataIndex: 'specialPrice', width: '5%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '特约特价', scopedSlots: { customRender: 'specialPrice' }, width: '5%', align: 'center' },
        // { title: '特约折扣', scopedSlots: { customRender: 'specialDiscount' }, width: '5%', align: 'center' },
        { title: '设置起订量', width: '6%', scopedSlots: { customRender: 'setNum' }, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '5%', align: 'center' }
      ]
      if (_this.rulesType === '0' || _this.rulesType === '1') {
        arr.splice(5, 0, { title: '省级折扣', scopedSlots: { customRender: 'provinceDiscount' }, width: '5%', align: 'center' })
        arr.splice(8, 0, { title: '市级折扣', scopedSlots: { customRender: 'cityDiscount' }, width: '5%', align: 'center' })
        arr.splice(11, 0, { title: '特约折扣', scopedSlots: { customRender: 'specialDiscount' }, width: '5%', align: 'center' })
      } else if (_this.rulesType === '2') {
        arr.splice(5, 0, { title: '省价直降金额', scopedSlots: { customRender: 'provinceDiscount' }, width: '5%', align: 'center' })
        arr.splice(8, 0, { title: '市价直降金额', scopedSlots: { customRender: 'cityDiscount' }, width: '5%', align: 'center' })
        arr.splice(11, 0, { title: '特约直降金额', scopedSlots: { customRender: 'specialDiscount' }, width: '5%', align: 'center' })
      }
      return arr
    }
  },
  methods: {
    // 计算百分比
    calculatePrice (val, sn, typePrice) {
      const _this = this
      const pos = _this.dataSource.findIndex(con => con.productScopeSn == sn)
      const oldPrice = typePrice + 'Price'
      const str = ((val / _this.dataSource[pos][oldPrice]) * 100).toFixed(2)
      const name = typePrice + 'DiscountRate'
      _this.dataSource[pos][name] = str
      _this.$forceUpdate()
    },
    // 新增产品 数据
    setSourceData (list) {
      this.dataSource = this.dataSource.concat(list)
    },
    // 生成随机数
    generateUniqueRandomNumber () {
      const timestamp = new Date().getTime()
      const randomNumber = Math.floor(Math.random() * 1e5).toString().padStart(5, '0')
      const uniqueRandomNumber = timestamp + randomNumber
      return uniqueRandomNumber
    },
    // 新增一行
    handleAddRow () {
      const _this = this
      const newSn = _this.generateUniqueRandomNumber()
      if (newSn) {
        const newData = {
          productScopeSn: newSn,
          dataSourceOrigin: '0',
          productTypeArr: [],
          productTypeList: [],
          productBrandArr: [],
          productBrandList: [],
          unitType: 'SL',
          unitQty: 1
        }
        _this.dataSource.push(newData)
      }
    },
    // 删除一行
    handleDel (row) {
      const pos = this.dataSource.findIndex(con => con.productScopeSn == row.productScopeSn)
      this.dataSource.splice(pos, 1)
    },
    // 添加产品分类
    addClassifyTag (pos, row) {
      this.classifyTagIndex = pos
      this.openTypeModal = true
      this.chooseType = this.handleRowData(row)
      const getArr = this.setTypeShowData()
      // 无品牌有分类的选择数据
      const onlyTypeData = this.onlyType()
      // 获取品牌重复数据
      var flag = false
      var c_typeArr = []
      this.dataSource.forEach(item => {
        if (item.dataSourceOrigin === '0') {
          if ((item.productTypeArr && item.productTypeArr.length > 0) && (item.productBrandList && item.productBrandList.length > 0)) {
            item.productBrandList.forEach((con) => {
              row.productBrandList.forEach(newCon => {
                if (newCon.productBrandSn == con.productBrandSn) {
                  flag = true
                  c_typeArr = c_typeArr.concat(item.productTypeList)
                }
              })
            })
          }
        }
      })
      const disTypeList = []
      getArr.forEach(con => {
        c_typeArr.forEach(item => {
          for (const i in item) {
            if (item[i] == con.goodsSn) {
              disTypeList.push(con)
            }
          }
        })
      })
      var uniqueArr = [...new Set(disTypeList)]

      if (row.productBrandArr && row.productBrandArr.length == 0) {
        this.$refs.typeBox.handleAllDisabled(onlyTypeData)
      } else {
        if (flag) {
          this.$refs.typeBox.handleAllDisabled(uniqueArr)
        } else {
          this.chooseType = []
          this.$refs.typeBox.handleAllDisabled([])
        }
      }
    },
    // 选择品牌后重组数据
    handleTypeOk (mainArr) {
      const classifyArr = []
      const newClassifyArr = []
      mainArr.forEach(item => {
        if (item.productTypeLevel == 1) {
          if ((item.dataRef && !item.dataRef.children) || (!item.dataRef && !item.children)) {
            const objType1 = {
              productTypeSn1: item.snPaths
            }
            newClassifyArr.push(objType1)
            const obj = JSON.parse(JSON.stringify(objType1))
            obj.title = item.title
            obj.id = item.snPaths
            classifyArr.push(obj)
          }
        }
        if (item.productTypeLevel == 2) {
          if ((item.dataRef && !item.dataRef.children) || (!item.dataRef && !item.children)) {
            const typeArr1 = item.snPaths ? item.snPaths.split(',') : []
            const objType2 = {
              productTypeSn1: typeArr1[0],
              productTypeSn2: typeArr1[1]
            }
            newClassifyArr.push(objType2)
            const obj = JSON.parse(JSON.stringify(objType2))
            const newName = item.namePaths.split(',')
            obj.title = newName[1]
            obj.id = typeArr1[1]
            classifyArr.push(obj)
          }
        }
        if (item.productTypeLevel == 3) {
          const typeArr2 = item.snPaths ? item.snPaths.split(',') : []
          const objType3 = {
            productTypeSn1: typeArr2[0],
            productTypeSn2: typeArr2[1],
            productTypeSn3: typeArr2[2]
          }
          newClassifyArr.push(objType3)
          const obj = JSON.parse(JSON.stringify(objType3))
          const newName = item.namePaths.split(',')
          obj.title = newName[1] + '/' + newName[2]
          obj.id = typeArr2[2]
          classifyArr.push(obj)
        }
      })
      this.dataSource[this.classifyTagIndex].productTypeList = newClassifyArr
      this.dataSource[this.classifyTagIndex].productTypeArr = classifyArr
    },
    // 添加产品品牌
    addBrandTag (pos, row) {
      const _this = this
      _this.brandTagIndex = pos
      _this.openBrandModal = true
      // 回显当前行选中
      const echoData = _this.handleEchoData('Brand', row.productBrandList)
      _this.chooseBrand = echoData
      // 只有品牌没有分类
      const onlyBrandData = this.onlyBrand()
      // 同行中分类是否与所选分类相同   已选分类
      var flag = false
      var c_brandArr = []
      this.dataSource.forEach(item => {
        if (item.dataSourceOrigin === '0') {
          if ((item.productTypeArr && item.productTypeArr.length > 0) && (item.productBrandList && item.productBrandList.length > 0)) {
            item.productTypeArr.forEach((con) => {
              row.productTypeArr.forEach(newCon => {
                if (newCon.id == con.id) {
                  flag = true
                  c_brandArr = c_brandArr.concat(item.productBrandList)
                }
              })
            })
          }
        }
      })
      const reBrandArr = c_brandArr.map(list => {
        return list.productBrandSn
      })
      if (row.productTypeArr && row.productTypeArr.length == 0) {
        _this.$refs.brandBox.handleDisabled(onlyBrandData)
      } else {
        if (flag) { // 当前行有重复分类
          _this.$refs.brandBox.handleDisabled(reBrandArr)
        } else {
          _this.chooseBrand = []
          _this.$refs.brandBox.handleDisabled([])
        }
      }
    },
    // 添加品牌后重组数据
    handleBrandOk (conList) {
      const newConList = conList.map(con => {
        return {
          productBrandSn: con.brandSn
        }
      })
      this.dataSource[this.brandTagIndex].productBrandArr = conList
      this.dataSource[this.brandTagIndex].productBrandList = newConList
    },
    // 获取回显禁用数据
    setShowData (name) {
      const _this = this
      const snArr = []
      _this.dataSource.forEach(item => {
        if (item['product' + name + 'List'] && item['product' + name + 'List'].length > 0) {
          item['product' + name + 'List'].forEach(con => {
            const newName = name === 'This' ? '' : name
            snArr.push(con['product' + newName + 'Sn'])
          })
        }
      })
      return snArr
    },
    // 只有分类
    onlyType (name, onlyName) {
      const _this = this
      const snArr = []
      _this.dataSource.forEach(item => {
        if (item.dataSourceOrigin === '0') {
          if (item.productTypeList && item.productTypeList.length > 0) {
            if (!item.productBrandList || (item.productBrandList && item.productBrandList.length == 0)) {
              item.productTypeList.forEach(con => {
                for (const i in con) {
                  const newObj = {
                    goodsSn: con[i]
                  }
                  if (i === 'productTypeSn1') {
                    newObj.disabled = true
                  } else {
                    newObj.disableCheckbox = true
                  }
                  snArr.push(newObj)
                }
              })
            }
          }
        }
      })
      return snArr
    },
    // 只有品牌
    onlyBrand () {
      const _this = this
      var snArr = []
      _this.dataSource.forEach(item => {
        if (item.dataSourceOrigin === '0') {
          if (item.productBrandList && item.productBrandList.length > 0) {
            if (!item.productTypeArr || (item.productTypeArr && item.productTypeArr.length == 0)) {
              item.productBrandList.forEach(con => {
                snArr.push(con.productBrandSn)
              })
            }
          }
        }
      })
      return snArr
    },
    // 筛选选中数据中的code和goodsn值
    handleEchoData (name, conArr) {
      conArr.map(item => {
        if (name === 'This') {
          item.code = item.productCode
          item.goodsSn = item.productSn
        } else {
          item.goodsSn = item['product' + name + 'Sn']
        }
      })
      return conArr
    },
    // 回显禁用列表
    handleRowData (row) {
      const echoData = []
      row.productTypeList.forEach(item => {
        for (const i in item) {
          const newObj = {
            goodsSn: item[i]
          }
          if (i === 'productTypeSn1') {
            newObj.disabled = true
          } else {
            newObj.disableCheckbox = true
          }
          echoData.push(newObj)
        }
      })
      return echoData
    },
    // 禁用行中产品品牌或者产品数据
    setTypeShowData () {
      const _this = this
      const snArr = []
      _this.dataSource.forEach(item => {
        if (item.dataSourceOrigin === '0') {
          if (item.productTypeList && item.productTypeList.length > 0) {
            item.productTypeList.forEach(con => {
              for (const i in con) {
                const newObj = {
                  goodsSn: con[i]
                }
                if (i === 'productTypeSn1') {
                  newObj.disabled = true
                } else {
                  newObj.disableCheckbox = true
                }
                snArr.push(newObj)
              }
            })
          }
        }
      })
      return snArr
    },
    // 获取最后结果
    getResultVal () {
      const _this = this
      if (_this.dataSource) {
        if (_this.rulesType === '1') { // 折扣
          _this.dataSource.forEach(con => {
            con.provinceDiscountRate = _this.setData[0].provinceValue
            con.cityDiscountRate = _this.setData[0].cityValue
            con.specialDiscountRate = _this.setData[0].specialValue
            if (con.dataSourceOrigin === '1') {
              con.provinceDiscountPrice = (_this.setData[0].provinceValue * con.provincePrice / 100).toFixed(2)
              con.cityDiscountPrice = (_this.setData[0].cityValue * con.cityPrice / 100).toFixed(2)
              con.specialDiscountPrice = (_this.setData[0].specialValue * con.specialPrice / 100).toFixed(2)
            }
          })
        } else if (_this.rulesType === '2') { // 直降
          _this.dataSource.forEach(con => {
            con.provinceSubtract = _this.setData[0].provinceValue
            con.citySubtract = _this.setData[0].cityValue
            con.specialSubtract = _this.setData[0].specialValue
            if (con.dataSourceOrigin === '1') {
              con.provinceDiscountPrice = con.provincePrice - _this.setData[0].provinceValue
              con.cityDiscountPrice = con.cityPrice - _this.setData[0].cityValue
              con.specialDiscountPrice = con.specialPrice - _this.setData[0].specialValue
            }
          })
        }
      } else {
        _this.$message.warning('请添加特价产品！')
        return
      }
      const resultObj = JSON.parse(JSON.stringify(this.dataSource))
      resultObj.map(item => {
        delete item.productTypeArr
        delete item.productBrandArr
        return item
      })
      return resultObj
    },
    // 关闭时清空表格数据
    reSetTableData () {
      this.dataSource = []
    },
    // 删除标签
    delLabel (sn, row, type) {
      const pos = this.dataSource.findIndex(con => con.productScopeSn == sn)
      if (type === 'typeList') {
        // 判断品牌是否有重复 重复值有无分类
        const isRepeatBrandFlag = this.isRepeatBrand(this.dataSource[pos])
        const uniqueArr1 = this.dataSource[pos].productTypeList && this.dataSource[pos].productTypeList.length > 0 ? this.dataSource[pos].productTypeList : []
        if (isRepeatBrandFlag.flag && isRepeatBrandFlag.noTypeFlag && uniqueArr1.length == 1) {
          this.dataSource.splice(pos, 1)
        } else {
          const i = this.dataSource[pos].productTypeArr.findIndex(con => con.id == row.id)
          this.dataSource[pos].productTypeArr.splice(i, 1)
          this.dataSource[pos].productTypeList.splice(i, 1)
        }
      } else if (type === 'brandList') {
        const isRepeatTypeFlag = this.isRepeatType(this.dataSource[pos])
        const uniqueArr = this.dataSource[pos].productBrandList && this.dataSource[pos].productBrandList.length > 0 ? this.dataSource[pos].productBrandList : []
        if (isRepeatTypeFlag.flag && isRepeatTypeFlag.noTypeFlag && uniqueArr.length == 1) {
          this.dataSource.splice(pos, 1)
        } else {
          const j = this.dataSource[pos].productBrandArr.findIndex(con => con.brandSn == row.brandSn)
          this.dataSource[pos].productBrandArr.splice(j, 1)
          this.dataSource[pos].productBrandList.splice(j, 1)
        }
      } else {
        const k = this.dataSource[pos].productThisList.findIndex(con => con.productSn == row.productSn)
        this.dataSource[pos].productThisList.splice(k, 1)
      }
      this.$forceUpdate()
    },
    // 判断当前行是否有重复品牌
    isRepeatBrand (row) {
      var flag = false
      var noTypeFlag = false
      var c_typeArr = []
      this.dataSource.forEach(item => {
        if (item.productBrandList && item.productBrandList.length > 0) {
          item.productBrandList.forEach((con) => {
            row.productBrandList.forEach(newCon => {
              if (newCon.productBrandSn == con.productBrandSn) {
                flag = true
                if (item.productTypeList && item.productTypeList.length == 0) {
                  noTypeFlag = true
                } else {
                  c_typeArr = c_typeArr.concat(item.productTypeList)
                }
              }
            })
          })
        }
      })
      return { list: c_typeArr, flag: flag, noTypeFlag: noTypeFlag }
    },
    // 判断分类是否重复
    isRepeatType (row) {
      var flag = false
      var noTypeFlag = false
      var c_brandArr = []
      this.dataSource.forEach(item => {
        if (item.productTypeArr && item.productTypeArr.length > 0) {
          item.productTypeArr.forEach((con) => {
            row.productTypeArr.forEach(newCon => {
              if (newCon.id == con.id) {
                flag = true
                if (item.productBrandList && item.productBrandList.length == 0) {
                  noTypeFlag = true
                } else {
                  c_brandArr = c_brandArr.concat(item.productBrandList)
                }
              }
            })
          })
        }
      })
      return { list: c_brandArr, flag: flag, noTypeFlag: noTypeFlag }
    }
  }
}
</script>

<style lang="less" scoped>
  .pages-wrap{
    .btn-cont {
      text-align: center;
      margin: 10px 0;
    }
  }

</style>
