<script setup>
import { watch, ref } from "vue";
import apiSheetChart from "@/api/sheet";

const show = defineModel("show", { type: Boolean, default: false });
const emits=defineEmits(['insert'])

const props = defineProps({
  sheetData: {
    type: [null, Object],
    default: null,
  },
});

// 获取表格的所有版本
const showSelectVersion = ref(false);
const versionOpts = ref([]);
const version = ref("");
async function getSheetVersion() {
  const res = await apiSheetChart.balanceTableVersion({
    ExcelInfoId: props.sheetData.ExcelInfoId,
  });
  if (res.Ret === 200) {
    const arr = res.Data.List || [];
    versionOpts.value = arr.map((i) => {
      return {
        ...i,
        name: i.VersionName,
      };
    });
  }
}
// 点击选择某个版本
function handleSelectVersion(e) {
  version.value = e.ExcelInfoId;
  getChildSheetList();
  showSelectVersion.value=false
}

// 获取当前版本下所有子表
const childSheetList = ref([]);
async function getChildSheetList() {
  const res = await apiSheetChart.getBalanceChildTable({
    ParentId: version.value,
  });
  if (res.Ret === 200) {
    childSheetList.value = res.Data.List || [];
  }
}

// 选择表格
const selectChartList=ref([])
function handleSelect(item){
    const index=selectChartList.value.indexOf(item.UniqueCode)
    if(index!==-1){
        selectChartList.value.splice(index,1)
    }else{
        selectChartList.value.push(item.UniqueCode)
    }
}

watch(
  () => show.value,
  (n) => {
    if (n) {
      version.value = props.sheetData.ExcelInfoId;
      getSheetVersion();
      getChildSheetList();
    }
  }
);


function handleConfirmInsert(){
  show.value=false
  emits('insert',selectChartList.value)
}
</script>

<template>
  <van-popup v-model:show="show" position="bottom" round class="child-sheet-wrap">
    <div class="title-box">
      <span class="back-btn" @click="show = false">返回</span>
      <span class="title">{{ props.sheetData.ExcelName }}</span>
    </div>
    <van-cell title="版本" :value="versionOpts.filter((i) => i.ExcelInfoId === version)[0]?.VersionName" is-link @click="showSelectVersion = true" />
    <div class="content-box">
      <div v-if="childSheetList.length == 0">
        <img class="list-empty-img" src="https://hzstatic.hzinsights.com/static/ETA_mobile/empty_img.png" alt="" />
        <p style="text-align: center; color: #999999; font-size: 12px">暂无图表</p>
      </div>

      <ul class="chart-list">
        <li :class="['chart-item', selectChartList.includes(item.UniqueCode) && 'active']" v-for="item in childSheetList" :key="item.UniqueCode" @click="handleSelect(item)">
          <div class="van-multi-ellipsis--l2 title">{{ item.ExcelName }}</div>
          <img :src="item.ExcelImage" alt="" />

          <svg v-if="selectChartList.includes(item.UniqueCode)" width="29" height="28" viewBox="0 0 29 28" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path
              d="M14.5 28C22.232 28 28.5 21.732 28.5 14C28.5 6.26801 22.232 0 14.5 0C6.76801 0 0.5 6.26801 0.5 14C0.5 21.732 6.76801 28 14.5 28ZM7.5 14.413L8.913 13L12.5 16.586L20.085 9L21.5 10.415L12.5 19.414L7.5 14.413Z"
              fill="#0052D9"
            />
          </svg>
        </li>
        <li class="chart-item" style="height: 0; border: none; margin-bottom: 0; padding: 0"></li>
        <li class="chart-item" style="height: 0; border: none; margin-bottom: 0; padding: 0"></li>
      </ul>
    </div>
    <div class="bot-btn">
      <van-button type="primary" block @click="handleConfirmInsert" :disabled="selectChartList.length===0">插入</van-button>
    </div>
  </van-popup>

  <!-- 选择版本 -->
  <van-action-sheet v-model:show="showSelectVersion" :actions="versionOpts" @select="handleSelectVersion" />
</template>

<style lang="scss" scoped>
.child-sheet-wrap {
  height: 70vh;
  display: flex;
  flex-direction: column;
  .title-box {
    height: 116px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    border-bottom: 1px solid $border-color;
    .title {
      font-size: 36px;
    }
    .back-btn {
      position: absolute;
      top: 50%;
      left: 32px;
      transform: translateY(-50%);
      color: $font-grey_999;
    }
  }
  .content-box {
    overflow: hidden;
    flex: 1;
    padding: var(--van-padding-sm);
    min-height: 300px;
  }
  .chart-list {
    height: 100%;
    overflow-y: auto;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-content: flex-start;
    .chart-item {
      width: 48%;
      background: #ffffff;
      border: 3px solid $border-color;
      box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.03);
      border-radius: 4px;
      margin-bottom: 30px;
      overflow: hidden;
      padding: 14px;
      box-sizing: border-box;
      position: relative;
      .title {
        font-size: 28px;
        min-height: 60px;
      }
      img {
        width: 100%;
        height: 220px;
        object-fit: contain;
      }
    }
    .active {
      border-color: $theme-color;
      svg {
        width: 28px;
        height: 28px;
        position: absolute;
        right: 22px;
        bottom: 22px;
      }
    }
  }
  .bot-btn{
    padding: 48px;
  }
}
@media screen and (min-width:$media-width){
  .child-sheet-wrap{
    .title-box{
      height: 58px;
      .title{
        font-size: 18px;
      }
      .back-btn{
        left: 16px;
      }
    }
    .chart-list{
            height: auto;
            max-height: 100%;
            .chart-item{
                width: 260px;
                border-width: 1px;
                border-radius: 2px;
                margin-bottom: 15px;
                padding: 7px;
                .title{
                    font-size: 14px;
                    min-height: 30px;
                }
                img{
                    height: 150px;
                }
            }
            .active{
                svg{
                    width: 14px;
                    height: 14px;
                    right: 11px;
                    bottom: 11px;
                }
            }
    }
    .bot-btn{
      padding: 24px;
    }
  }

}
</style>
