<template>
  <view class="custom-container" @click.stop>
    <!-- 导航栏 start -->
    <view class="custom-bav-bar">
      <view class="left">
        <u-icon name="arrow-left" color="#666" size="36rpx" @click="handleBack" />
      </view>
      <view class="center"> 添加关注申万行业 </view>
    </view>
    <!-- 导航栏 end -->

    <!-- 搜搜 start -->
    <view class="page-search">
      <u-input
        v-model="input"
        type="text"
        placeholder="输入行业查找"
        prefixIcon="search"
        fontSize="28rpx"
        prefixIconStyle="font-size: 40rpx;color: #BDC1C7;"
        :customStyle="{
          borderRadius: '36rpx',
          background: '#F3F5F8',
        }"
        clearable
        @confirm="handleSearchTag"
      />
    </view>
    <!-- 搜搜 end -->

    <view class="custom-main">
      <view class="my-industry">
        <view class="my-industry-top">
          <view class="label">我的行业</view>
          <view class="label-count">
            <text>已选</text>
            <text class="color">{{ myTagList.length }}</text>
            <text>/10</text>
          </view>
        </view>
        <view class="my-industry-list" v-if="myTagList.length > 0">
          <view class="industry-item" v-for="item in myTagList">
            <view class="industry-name">{{ item.name }}</view>
            <view class="industry-btn" @click="handleDeleteTag(item)"></view>
          </view>
        </view>
        <view class="my-industry-empty" v-else> 暂无自选，请从下方添加 </view>
      </view>
      <view class="recommend-industry">
        <view class="recommend-industry-top">
          <view class="label">推荐行业</view>
          <view class="right" @click="selectRandomTags">
            <view class="icon"></view>
            <view class="text">换一换</view>
          </view>
        </view>
        <view class="recommend-industry-list" v-if="industryList.length > 0">
          <view
            v-for="(item, index) in industryList"
            :key="index"
            :class="['industry-item', { active: isSelected(item.content) }]"
            @click="handleAddTag(item.content)"
          >
            <view class="industry-name">{{ item.content }}</view>
          </view>
        </view>
        <view v-else class="no-data"> <u-empty /></view>
      </view>
    </view>

    <view class="custom-footer">
      <view class="btn-clear" @click="handleClear">清空</view>
      <view class="btn-done" @click="handleComplete">完成配置</view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { getTopIndustryPeriod } from "@/api/newsInfo";
import dayjs from "dayjs";
import { onMounted, ref, watch } from "vue";

const props = defineProps({
  tagList: {
    type: Array,
    default: () => [],
  },
});
const emit = defineEmits(["onChange", "onBack"]);

const input = ref("");

// 导航栏路由返回
const handleBack = () => {
  if (props.tagList.length === 0) {
    uni.navigateBack({
      delta: 1,
    });
  } else {
    emit("onBack");
  }
};

const myTagList = ref([]);
const industryList = ref([]);
const industryListAll = ref([]);
const start_time = dayjs().subtract(1, "month").format("YYYY-MM-DD");
const end_time = dayjs().format("YYYY-MM-DD");
const limit_num = 20;
// 热门行业top10
async function getTopIndustry_dFn() {
  industryListAll.value = await getTopIndustryPeriod({
    start_time: start_time,
    end_time: end_time,
    limit_num: limit_num,
  });
  selectRandomTags();
}

// 从所有标签中随机选择10个
function selectRandomTags() {
  industryList.value = [];
  const availableTags = [...industryListAll.value];
  for (let i = 0; i < 10; i++) {
    if (availableTags.length === 0) break;

    const randomIndex = Math.floor(Math.random() * availableTags.length);
    industryList.value.push(availableTags[randomIndex]);
    availableTags.splice(randomIndex, 1);
  }
}

// 是否选中
function isSelected(tag) {
  const index = myTagList.value.findIndex((item) => item.name === tag);
  return index === -1 ? false : true;
}
// 添加标签
function handleAddTag(tag) {
  if (isSelected(tag)) return;
  if (myTagList.value.length >= 10) {
    uni.showToast({
      title: "最多添加10个标签",
      icon: "none",
    });
    return;
  }

  myTagList.value.push({
    name: tag,
  });
}

// 删除标签
function handleDeleteTag(tag) {
  const index = myTagList.value.findIndex((item) => item.name === tag);
  myTagList.value.splice(index, 1);
}

// 搜索标签
function handleSearchTag() {
  if (!input.value) {
    return;
  }
  industryList.value = industryListAll.value.filter((item) => {
    if (!item || !item.content) return false;

    return item.content.includes(input.value);
  });
}

// 完成配置
function handleComplete() {
  if (myTagList.value.length < 1) {
    uni.showToast({
      title: "请先选择标签",
      icon: "none",
    });
    return;
  }

  emit("onChange", myTagList.value);
}
// 清除
function handleClear() {
  myTagList.value = [];
}

watch(
  () => props.tagList,
  (newVal) => {
    if (newVal) {
      myTagList.value = [...newVal];
    }
  },
  {
    immediate: true,
  },
);

onMounted(() => {
  getTopIndustry_dFn();
});
</script>

<style scoped lang="scss">
.custom-container {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #fff;
  z-index: 9;
  padding-bottom: 80rpx;
  box-sizing: border-box;
}

.custom-bav-bar {
  width: 100%;
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;

  .back_icon {
    width: 36rpx;
    height: 36rpx;
  }

  .logo_icon {
    width: 168rpx;
    height: 36rpx;
    margin-right: 6rpx;
  }

  .left {
    position: absolute;
    top: 24rpx;
    left: 32rpx;
  }

  .center {
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: "PingFangSC, PingFang SC";
    font-weight: 500;
    font-size: 34rpx;
    color: #222222;
    line-height: 36rpx;
  }
}

.page-search {
  position: relative;
  margin-top: 16rpx;
  margin-bottom: 20rpx;
  padding: 0 30rpx;
  z-index: 2;
}

.custom-main {
  width: 100%;
  height: calc(100% - 250rpx);
  padding: 33rpx 30rpx;
  box-sizing: border-box;
  overflow: auto;

  .my-industry {
    margin-bottom: 80rpx;
    .my-industry-top {
      display: flex;
      align-items: center;
      margin-bottom: 32rpx;

      .label {
        margin-right: 13rpx;
        font-family: "PingFangSC, PingFang SC";
        font-weight: 500;
        font-size: 30rpx;
        color: #111111;
      }

      .label-count {
        font-family: "PingFangSC, PingFang SC";
        font-weight: 400;
        font-size: 26rpx;
        color: #666666;
        .color {
          color: #3f80fa;
        }
      }
    }

    .my-industry-list {
      display: flex;
      gap: 20rpx;
      flex-wrap: wrap;

      .industry-item {
        display: flex;
        align-items: center;
        padding: 18rpx 30rpx;
        background: #eff7ff;
        border-radius: 8rpx;

        .industry-name {
          margin-right: 20rpx;
          font-family: "PingFangSC, PingFang SC";
          font-weight: 400;
          font-size: 26rpx;
          color: #3f80fa;
        }
        .industry-btn {
          width: 20rpx;
          height: 20rpx;
          background: url("@/assets/images/page/icon_close@2x.png") no-repeat;
          background-size: 20rpx 20rpx;
        }
      }
    }

    .my-industry-empty {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100rpx;
      background: #f7f8fa;
      border-radius: 8rpx;
      border: 2rpx solid #e4e9f0;
      font-family: "PingFangSC, PingFang SC";
      font-weight: 400;
      font-size: 26rpx;
      color: #cccccc;
    }
  }

  .recommend-industry {
    .recommend-industry-top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30rpx;

      .label {
        font-family: "PingFangSC, PingFang SC";
        font-weight: 500;
        font-size: 30rpx;
        color: #111111;
      }
      .right {
        display: flex;
        align-items: center;
        .icon {
          width: 32rpx;
          height: 32rpx;
          background: url("@/assets/images/page/icon_change@2x.png") no-repeat 0 3rpx;
          background-size: 32rpx 32rpx;
          margin-right: 10rpx;
        }
        .text {
          font-family: "PingFangSC, PingFang SC";
          font-weight: 400;
          font-size: 26rpx;
          color: #999999;
        }
      }
    }

    .recommend-industry-list {
      display: grid;
      gap: 20rpx;
      grid-template-columns: repeat(2, 1fr);

      .industry-item {
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 18rpx 0;
        background-color: #f3f5f8;
        border-radius: 8px;

        &.active {
          background-image: url("@/assets/images/page/chose_icon@2x.png");
          background-position: right bottom;
          background-repeat: no-repeat;
          background-size: 38rpx 31rpx;
        }

        .industry-name {
          font-family: "PingFangSC, PingFang SC";
          font-weight: 400;
          font-size: 28rpx;
          color: #333333;
          line-height: 40rpx;
          text-align: center;
        }
      }
    }

    .no-data {
      display: flex;
      justify-content: center;
    }
  }
}

.custom-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16rpx 24rpx;
  background: #ffffff;
  box-shadow: 0px -4px 12px 0px rgba(0, 0, 0, 0.1);
  box-sizing: border-box;

  .btn-clear {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 239rpx;
    height: 100rpx;
    border-radius: 50rpx;
    background: #fff;
    border: 2rpx solid #979797;
    font-family: "PingFangSC, PingFang SC";
    font-weight: 500;
    font-size: 32rpx;
    color: #656565;
    line-height: 45rpx;
    cursor: pointer;
  }

  .btn-done {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 437rpx;
    height: 100rpx;
    border-radius: 50rpx;
    background: #3f80fa;
    font-family: "PingFangSC, PingFang SC";
    font-weight: 500;
    font-size: 32rpx;
    color: #ffffff;
    line-height: 45rpx;
    cursor: pointer;
  }
}
</style>
