<template>
  <view class="item-container">
    <!-- 图书数量 + 右侧设置按钮 -->
    <view class="count-text">
      <text>图书数量 ({{ bookList.length }})</text>
      <text class="edit-btn" @click="toggleEdit">{{ editMode ? "完成" : "管理" }}</text>
    </view>

    <checkbox-group @change="checkboxChange">
      <view class="car-list" v-for="item in bookList" :key="item.isbn">
        <checkbox :value="item.isbn" :checked="item.checked" />
        <view class="book-item-box">
          <view class="item-box-left">
            <image class="img-item" :src="item.imgCover" mode="aspectFill" />
          </view>
          <view class="item-box-right">
            <view class="item-title line-clamp-2">{{ item.title || '暂无标题' }}</view>
            <view class="tag-box">
              <text class="item-author">{{ item.nickname || '佚名' }}</text>
              <text class="item-publish">{{ item.publish || '暂无出版社' }}</text>
            </view>
            <view class="item-desc line-clamp-2">{{ item.desc || '暂无简介' }}</view>
          </view>
        </view>
      </view>
    </checkbox-group>

    <view class="bottom-placeholder"></view>

    <view class="car-bottom">
      <!-- 全选 -->
      <view class="all-check" @click="handleAllCheck">
        <checkbox :checked="isAllChecked" />
        <text style="margin-left:6px">全选</text>
      </view>

      <!-- 根据编辑状态切换按钮：续借 / 删除 -->
      <button 
        class="join-btn" 
        @click="editMode ? handleDelete() : handleRenew()" 
        :disabled="!hasChecked"
      >
        {{ editMode ? "删除选中" : "一键续借" }}
      </button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      editMode: false, // 控制编辑/删除状态
      bookList: [
        {
          imgCover: "https://qiniu.aiyxlib.com/1606124577077",
          title: "名侦探柯南",
          nickname: "青山刚昌",
          publish: "长春出版社",
          isbn: "1001",
          desc: "测试图书简介",
          checked: true,
        },
        {
          imgCover: "https://qiniu.aiyxlib.com/1606124577077",
          title: "测试图书",
          nickname: "测试作者",
          publish: "测试出版社",
          isbn: "1002",
          desc: "测试简介",
          checked: true,
        },
        {
          imgCover: "https://qiniu.aiyxlib.com/1606124577077",
          title: "测试图书",
          nickname: "测试作者",
          publish: "测试出版社",
          isbn: "1003",
          desc: "测试简介",
          checked: false,
        }
      ],
    };
  },
  computed: {
    // 是否全选
    isAllChecked() {
      return this.bookList.every((item) => item.checked);
    },
    // 是否有选中
    hasChecked() {
      return this.bookList.some((item) => item.checked);
    },
  },
  methods: {
    // 切换编辑模式
    toggleEdit() {
      this.editMode = !this.editMode;
      // 退出编辑时可以取消所有选中（可选）
      if (!this.editMode) {
        this.bookList.forEach(item => item.checked = false);
      }
    },
    checkboxChange(e) {
      console.log("checkbox选中value：", e.detail.value);
      const values = e.detail.value;
      const items = this.bookList;

      for (let i = 0, lenI = items.length; i < lenI; ++i) {
        items[i].checked = false;
        for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
          if (items[i].isbn === values[j]) {
            items[i].checked = true;
            break;
          }
        }
      }
      this.bookList = items;
    },
    handleAllCheck() {
      const isAll = this.isAllChecked;
      this.bookList.forEach((item) => {
        item.checked = !isAll;
      });
    },

    // 一键续借
    handleRenew() {
      const checkedBooks = this.bookList.filter((item) => item.checked);
      uni.showToast({
        title: `已续借 ${checkedBooks.length} 本`,
        icon: "success",
      });
    },
    
    // 删除选中图书
    handleDelete() {
      const beforeLen = this.bookList.length;
      // 过滤掉选中的项
      this.bookList = this.bookList.filter(item => !item.checked);
      const deleteCount = beforeLen - this.bookList.length;
      
      uni.showToast({
        title: `已删除 ${deleteCount} 本`,
        icon: "success"
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.item-container {
  padding: 10px;
  background: #f5f6f7;
  min-height: 100vh;
}
.count-text {
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between; /* 数量左，设置右 */
  align-items: center;
}
.edit-btn {
  color: #01a4fe;
  font-size: 14px;
}
.car-list {
  display: flex;
  align-items: flex-start;
  margin-bottom: 10px;
  checkbox {
    margin-top: 15px;
    padding: 0 10px;
  }
}
.book-item-box {
  flex: 1;
  background: #fff;
  border-radius: 8px;
  padding: 12px;
  display: flex;
  .item-box-left {
    margin-right: 12px;
    .img-item {
      width: 64px;
      height: 90px;
      border-radius: 6px;
    }
  }
  .item-box-right {
    flex: 1;
  }
  .item-title {
    font-weight: bold;
    margin-bottom: 4px;
  }
  .item-author,
  .item-publish {
    font-size: 12px;
    background: #f4f6fc;
    padding: 2px 6px;
    border-radius: 4px;
    margin-right: 6px;
  }
  .item-desc {
    font-size: 12px;
    color: #999;
    margin-top: 6px;
  }
}
.bottom-placeholder {
  height: 60px;
}
.car-bottom {
  position: fixed;
  left: 0;
  bottom: 0;
  right: 0;
  background: #fff;
  padding: 12px 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.05);
}
.all-check {
  display: flex;
  align-items: center;
}
.join-btn {
  font-size: 14px;
  color: #fff;
  margin: 0 !important;
  background-color: #01a4fe !important;
  border-radius: 23px;
  padding: 0 30px;
  &::after{
    border: none !important;
  }
  &[disabled] {
    background: #ccc !important;
  }
}
</style>