<template>
  <div class="real-pages">
    <div>
      <div class="real-info">
        <div class="title">{{ dataInfo.announcementTitle }}</div>
        <div class="time">{{ dataInfo.createTime }}</div>
        <div class="real-html" v-html="dataInfo.announcementContent"></div>
      </div>
    </div>
    <el-affix :offset="0">
      <div class="related-bos">
        <div class="related-box">
          <div class="flex-row-between related-title">
            <div>产品推荐</div>
            <div class="flex-row-start related-huan" @click="getList">
              <el-icon><Sort /></el-icon>
              <div style="margin-left: 4px">换一换</div>
            </div>
          </div>
          <div class="procure-bos">
            <div v-for="(item, index) in dataList" :key="index" class="procure-list" @click="onPath('/plan_info?id=' + item.id)">
              <img :src="item.coverImage" alt="" />
              <div class="procure1">{{ item.title }}</div>
              <div class="procure2">{{ item.releaseTime }}</div>
            </div>
          </div>
        </div>
      </div>
    </el-affix>
    <!--
    <div class="real-shop">
      <div class="shop-head flex-row-start">
        <div class="head1">为您推荐</div>
        <div class="head2">热门流行商品推荐</div>
      </div>
      <div class="data-bos">
        <div v-for="(item, index) in 9" :key="index" class="data-list">
          <img class="data-img" src="@/assets/images/login-background.jpg" alt="" />
          <div class="data-title">格力KFR-72LW/定频冷暖空调柜机3P</div>
          <div class="money">
            <span class="money1">¥1,299</span>
            <span class="money2">¥1,899</span>
          </div>
        </div>
      </div>
    </div> -->
  </div>
</template>

<script setup lang="ts">
import { onPath } from '@/utils/siteConfig';
import { getYouYzXunInfo } from '@/api/plan/index';
import { getProcurementProgramList } from '@/api/plan/index';
const id = ref<any>(null);
const dataInfo = ref<any>({});
const route = useRoute();
const dataList = ref<any>([]);
onMounted(() => {
  id.value = route.query.id;
  getInfo();
  getList();
});

const getInfo = () => {
  getYouYzXunInfo(id.value).then((res) => {
    if (res.code == 200) {
      dataInfo.value = res.data;
    }
  });
};

// 封装获取列表的逻辑
const getList = () => {
  getProcurementProgramList({
    pageSize: 10,
    pageNum: 1
  }).then((res) => {
    if (res.code == 200) {
      if (res.rows && res.rows.length > 0) {
        if (res.rows.length <= 2) {
          dataList.value = res.rows;
        } else {
          dataList.value = getRandomElements(res.rows, 3);
        }
      }
    }
  });
};

function getRandomElements(arr: any, count: any) {
  if (count > arr.length) {
    throw new Error('要取的数量不能超过数组长度');
  }
  const copy = [...arr];
  const result = [];
  for (let i = 0; i < count; i++) {
    const randomIndex = Math.floor(Math.random() * copy.length);
    result.push(copy.splice(randomIndex, 1)[0]);
  }
  return result;
}
</script>

<style lang="scss" scoped>
.real-pages {
  width: 1200px;
  margin: 0 auto;
  display: flex;
  gap: 0 20px;
  padding-bottom: 50px;
  .real-info {
    width: 715px;
    background: #ffffff;
    border-radius: 10px;
    padding: 20px;
    .title {
      font-size: 20px;
      color: #101828;
    }
    .time {
      font-size: 14px;
      color: #364153;
      margin: 10px 0 18px 0;
    }
    .real-html {
      width: 679px;
      :deep(img) {
        max-width: 100%;
        height: auto;
      }
    }
    .img {
      width: 679px;
      height: 351px;
      border-radius: 10px;
    }
    .head {
      font-weight: 600;
      font-size: 14px;
      color: #101828;
      margin: 10px 0 18px 0;
    }
    .info {
      font-size: 14px;
      color: #364153;
      margin-bottom: 15px;
    }
  }
  .real-shop {
    flex: 1;
    .head1 {
      font-weight: 600;
      font-size: 20px;
      color: #101828;
    }
    .head2 {
      font-size: 14px;
      color: #364153;
      margin-left: 10px;
    }
    //数据
    .data-bos {
      display: flex;
      gap: 17px;
      flex-wrap: wrap;
      margin-top: 10px;
      width: 100%;
      .data-list {
        width: 224px;
        background: #ffffff;
        border-radius: 10px;
        padding: 20px 20px 22px 20px;
        cursor: pointer;
        .data-img {
          width: 184px;
          height: 184px;
          border-radius: 10px;
        }
        .data-title {
          margin-top: 4px;
          font-size: 14px;
          color: #101828;
          height: 40px;
        }
        .money {
          margin-top: 4px;
          .money1 {
            font-size: 16px;
            color: #e7000b;
          }
          .money2 {
            font-size: 12px;
            color: #99a1af;
            text-decoration: line-through;
            padding-left: 6px;
          }
        }
        .data-cat {
          width: 86px;
          height: 26px;
          background: #e7000b;
          border-radius: 2px;
          font-size: 14px;
          color: #ffffff;
          line-height: 26px;
          text-align: center;
          margin-top: 16px;
        }
      }
    }
  }

  .related-bos {
    flex: 1;
    .related-box {
      width: 100%;
      background-color: #ffffff;
      border-radius: 10px;
      padding: 20px;
      .related-title {
        font-size: 16px;
        color: #666666;
        .related-huan {
          font-size: 14px;
          cursor: pointer;
        }
      }
      // 采购指南
      .procure-bos {
        width: 100%;
        display: flex;
        flex-direction: column;
        margin-top: 12px;
        gap: 12px 0px;
        .procure-list {
          width: 100%;
          height: 268px;
          background: #ffffff;
          cursor: pointer;
          border-radius: 10px;
          overflow: hidden;
          border: 1px solid #d0d5dd;
          img {
            width: 100%;
            height: 200px;
            border-bottom: 1px solid #d0d5dd;
          }
          .procure1 {
            padding: 12px 0 4px 20px;
            font-weight: 600;
            font-size: 14px;
            color: #101828;
          }
          .procure2 {
            padding-left: 20px;
            font-size: 12px;
            color: #364153;
          }
        }
      }
    }
  }
}
</style>
