<template>
  <div class="cart-wrap">
    <div class="shop" v-for="(shop, i) in list" :key="i">
      <div class="flex-align-center shop-info">
        <div
          class="check"
          :class="{ 'is-checked': shop.isChecked }"
          @click="changeShopChecked(shop)"
        >
          <wd-icon v-if="shop.isChecked" name="check1"></wd-icon>
        </div>
        <div class="type">{{ shop.type }}</div>
        <div class="shop-name">{{ shop.shopName }}</div>
      </div>
      <div class="commodity-list">
        <div class="commodity-item flex-align-center" v-for="(item, j) in shop.children" :key="j">
          <div
            class="check"
            :class="{ 'is-checked': item.isChecked }"
            @click="changeCommodityChecked(item, shop)"
          >
            <wd-icon v-if="item.isChecked" name="check1"></wd-icon>
          </div>
          <img :src="item.img" alt="" />
          <div class="commodity-info">
            <div class="name">{{ item.name }}</div>
            <div class="tag">{{ item.tag }}</div>
            <div class="flex-between">
              <div class="price">
                <span>￥</span>
                {{ item.price }}
              </div>
              <wd-input-number v-model="item.count" />
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="total flex-between">
      <div class="left flex-align-center">
        <div class="flex-align-center check-box">
          <div class="check" :class="{ 'is-checked': checkedAll }" @click="changeCheckedAll()">
            <wd-icon v-if="checkedAll" name="check1"></wd-icon>
          </div>
          全选
        </div>
        <div class="price">
          合计:
          <span>￥{{ totalPrice }}</span>
        </div>
      </div>
      <div class="submit" @tap="toSettle">去结算({{ totalPrice }})</div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
// 计算是否全选
const checkedAll = computed(() => {
  return list.value.every((item) => item.isChecked);
});
const totalPrice = computed(() => {
  let total = 0;
  list.value.forEach((item) => {
    item.children.forEach((c) => {
      if (c.isChecked) {
        total += c.price;
      }
    });
  });
  return total || 0;
});
const list = ref([
  {
    shopName: '杨家黄焖鸡',
    type: '店铺',
    isChecked: true,
    children: [
      {
        img: '/src/static/assistingAgriculture/assets/fruit.png',
        name: '招牌黄焖鸡+米饭（土豆+生菜）',
        price: 28.8,
        count: 1,
        tag: '约1000克',
        isChecked: false,
      },
    ],
  },
  {
    shopName: '杨家黄焖鸡',
    type: '店铺',
    isChecked: false,
    children: [
      {
        img: '/src/static/assistingAgriculture/assets/fruit.png',
        name: '招牌黄焖鸡+米饭（土豆+生菜）',
        price: 28.8,
        count: 1,
        tag: '约1000克',
        isChecked: false,
      },
      {
        img: '/src/static/assistingAgriculture/assets/fruit.png',
        name: '招牌黄焖鸡+米饭（土豆+生菜）',
        price: 28.8,
        count: 1,
        tag: '约1000克',
        isChecked: false,
      },
      {
        img: '/src/static/assistingAgriculture/assets/fruit.png',
        name: '招牌黄焖鸡+米饭（土豆+生菜）',
        price: 28.8,
        count: 1,
        tag: '约1000克',
        isChecked: false,
      },
    ],
  },
]);
// 店铺
const changeShopChecked = (s) => {
  s.isChecked = !s.isChecked;
  s.children.forEach((v) => {
    v.isChecked = s.isChecked;
  });
};
// 商品 店铺
const changeCommodityChecked = (c, s) => {
  c.isChecked = !c.isChecked;
  s.isChecked = s.children.every((v) => v.isChecked);
};
// 全选
const changeCheckedAll = () => {
  const temp = checkedAll.value;
  list.value.forEach((v) => {
    v.isChecked = temp;
    changeShopChecked(v);
  });
};
function toSettle() {
  // TODO: 跳转到结算页面
  xma.navigateTo({
    url: '/pages/assistingAgriculture/cart/ConfirmOrder',
  });
}
</script>

<style lang="scss" scoped>
uni-page-body {
  height: 100%;
}
.cart-wrap {
  height: 100%;
  background-color: #f6f6f6;
  position: relative;
  padding: 20rpx;
  padding-bottom: 0;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .check {
    width: 32rpx;
    height: 32rpx;
    border-radius: 50%;
    box-sizing: border-box;
    border: 1px solid #707070;
    display: flex;
    align-items: center;
    justify-content: center;
    &.is-checked {
      border-color: #f12a2a;
      background: #f12a2a;
      color: #fff;
    }
    .wd-icon {
      font-size: 25rpx;
    }
  }
  .shop {
    background-color: #fff;
    padding: 32rpx 20rpx;
    margin-bottom: 20rpx;

    .shop-info {
      .type {
        padding: 3rpx 6rpx;
        font-size: 20rpx;
        background-color: #f12a2a;
        color: #fff;
        border-radius: 4rpx;
        margin: 0 6rpx 0 18rpx;
      }
      .shop-name {
        color: #333333;
        font-size: 28rpx;
      }
    }
    .commodity-list {
      margin-top: 28rpx;
      display: flex;
      flex-direction: column;
      gap: 40rpx;
      .commodity-item {
        color: #333333;
        img {
          width: 170rpx;
          height: 170rpx;
          border-radius: 12rpx;
          margin: 0 23rpx 0 18rpx;
        }
        .commodity-info {
          .name {
            font-size: 28rpx;
            font-weight: normal;
            line-height: 28rpx;
            margin-top: 12rpx;
          }
          .tag {
            margin-top: 22rpx;
            padding: 8rpx 10rpx;
            font-size: 24rpx;
            font-weight: normal;
            line-height: 24rpx;
          }
          .price {
            color: #f12a2a;
            font-size: 36rpx;
            span {
              font-size: 24rpx;
            }
          }
        }
      }
    }
  }
  .total {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 92rpx;
    background-color: #fff;
    padding: 0 15rpx 0 23rpx;
    box-sizing: border-box;
    .left {
      .check-box {
        font-size: 24rpx;
        font-weight: normal;
        line-height: 24rpx;
        color: #333333;
        .check {
          margin-right: 17rpx;
        }
      }
      .price {
        font-size: 28rpx;
        margin-left: 20rpx;
        color: #333333;
        span {
          color: #f12a2a;
        }
      }
    }
    .submit {
      padding: 23rpx 30rpx;
      background: linear-gradient(90deg, #f74040 0%, #eb1818 100%);
      font-size: 28rpx;
      font-weight: normal;
      line-height: 28rpx;
      letter-spacing: 0em;
      border-radius: 37rpx;
      color: #ffffff;
    }
  }
}
</style>
