<template>
  <ScrollView class="format-wrapper" ref="scroll" :refresh-delay="800" :list="list">
    <div class="format-scroll">
      <div class="format-item" @click="handleFormat({ industryName: '全部品牌', industryNameEn: 'all' }, -1)">
        <div class="parent flex" :class="{ active: parentIdxRef === -1 }">
          <div class="format-left flex">
            <img src="../../assets/images/detail/all.svg" class="icon" alt="" />
            <span class="text">{{ $t('all') }}</span>
          </div>
          <div class="format-right flex">
            <span class="total">{{ total }}</span>
            <img src="../../assets/images/detail/all_dot.svg" class="right-icon" alt="" />
          </div>
        </div>
      </div>
      <div class="format-item" v-for="(item, index) of list" :key="item.industryId">
        <div
          class="parent flex"
          @click="handleFormat(item, index)"
          :class="[index === parentIdxRef ? 'active' : '', index === parentIdxRef && childIdxRef !== -1 ? 'child-active' : '']"
        >
          <div class="format-left flex">
            <img :src="config.sourceUrl + item.fileUrl" class="icon" alt="" />
            <span class="text">{{ switchLanguage(item, 'industryName') }}</span>
          </div>
          <div class="format-right flex">
            <span class="total">{{ item.shopNum }}</span>
            <Arrow :size="18" fill="rgba(0, 0, 0, 0.2)" v-if="item.industryList?.length" class="right-icon svg-icon" />
            <img src="../../assets/images/detail/all_dot.svg" class="icon" v-else alt="" />
          </div>
        </div>
        <template v-if="item.industryList?.length">
          <div class="format-child-wrapper" v-if="index === parentIdxRef">
            <div class="format-item" v-for="(child, index) of item.industryList" :key="child.industryId">
              <div class="parent flex" :class="{ active: childIdxRef === index }" @click="handleChildFormat(child, index)">
                <div class="format-left flex">
                  <span class="text">{{ switchLanguage(child, 'industryName') }}</span>
                </div>
                <div class="format-right flex">
                  <span class="total">{{ child.shopNum ?? 0 }}</span>
                </div>
              </div>
            </div>
          </div>
        </template>
      </div>
    </div>
  </ScrollView>
</template>

<script setup>
import { computed, nextTick, ref } from 'vue'
import ScrollView from '@/base/ScrollView/ScrollView.vue'
import { Arrow } from '@/base/Svg'

const props = defineProps({
  list: {
    type: Array,
    default: () => []
  },
  config: {
    type: Object,
    default: () => ({})
  }
})
const total = computed(() => props.list.reduce((_total, item) => _total + item.shopNum, 0))

const emits = defineEmits(['click'])
emits('click', { industryName: '全部品牌', industryNameEn: 'all' }, -1)

const scroll = ref(null)
const parentIdxRef = ref(-1)
const childIdxRef = ref(-1)
function handleFormat(item, index) {
  emits('click', item, index)
  childIdxRef.value = -1
  parentIdxRef.value = index
  nextTick(() => {
    scroll.value.refresh()
  })
}
function handleChildFormat(child, childIdx) {
  childIdxRef.value = childIdx
  emits('click', child, childIdx)
  nextTick(() => {
    scroll.value.refresh()
  })
}
</script>

<style lang="scss" scoped>
.format-wrapper {
  overflow: hidden;
  height: 810px;
  border-radius: 8px;
  width: 200px;
  margin-top: 18px;
  .format-scroll {
    overflow: hidden;
    border-radius: 8px;
  }
  .format-item {
    width: 200px;
    background-color: #fff;
    .parent {
      height: 56px;
      padding: 0 12px 0 24px;
      justify-content: space-between;
      align-items: center;
      &.active {
        background: linear-gradient(90deg, #ffbd35 0%, #ffd260 100%);
        .icon,
        .right-icon {
          filter: brightness(50) invert(70%);
          &.svg-icon {
            filter: none;
          }
        }

        :deep(path) {
          fill: rgba(0, 0, 0, 0.6);
        }

        .svg-icon {
          transform: rotate(-180deg);
        }
        .text,
        .total {
          color: rgba(0, 0, 0, 0.8);
        }
      }
      &.child-active {
        background: transparent;
        .icon,
        .right-icon {
          filter: none;
        }
        :deep(path) {
          fill: rgba(0, 0, 0, 0.6);
        }
        .right-icon {
          transform: rotate(-180deg);
        }
        .text,
        .total {
          color: var(--color-9b0);
        }
      }
    }
    .format-right {
      .icon {
        margin-left: 8px;
        margin-right: 0;
      }
    }
    .icon {
      width: 16px;
      height: 16px;
      margin-right: 8px;
    }
    .text,
    .total {
      font-weight: 700;
      font-size: 14px;
      font-family: 'font_bold';
      color: rgba(0, 0, 0, 0.6);
    }
    .total {
      font-weight: 400;
      font-family: 'font_regular';
      color: rgba(0, 0, 0, 0.4);
    }
    .right-icon {
      margin-left: 8px;
      transition: transform 0.3s;
    }
  }
  .format-child-wrapper {
    .parent {
      padding: 0 48px;
    }
    .text {
      color: rgba(0, 0, 0, 0.4);
    }
    .total {
      color: rgba(0, 0, 0, 0.2);
    }
  }
}
.flex {
  display: flex;
  align-items: center;
}
</style>
