<template>
  <div class="my-shoping-car-warp">
    <userHeader :title="'购物车' + total"></userHeader>
    <div class="page-content-warp">
      <div class="fix-top">
        <div class="do-all" @click="toCheckAll">
          <div class="do-all-pos">
            <ischeck :value="isAll"></ischeck>
            <span class="all-txt">全选</span>
          </div>
        </div>
        <div class="batch-del">
          <el-icon><Delete /></el-icon>
          <span class="del-txt">批量删除</span>
        </div>
      </div>
      <div class="conetnt-warp">
        <div class="content-item-warp">
          <div v-for="(n, index) in datalist" :key="index" class="content-item">
            <div class="shop-info" @click="toCheckShop(index)">
              <div class="shop-do">
                <ischeck :value="n.ischeck"></ischeck>
              </div>
              <div class="shop-img">
                <costomImg
                  :url="'images/ecommerce/' + 'pic.png'"
                  :preview-list="[getAssetsFile('images/ecommerce/' + 'pic.png')]"
                  :is-view="false"
                ></costomImg>
              </div>
              <span class="shop-name txt-ellipsis clamp2">{{ n.shop }}</span>
            </div>

            <div v-if="n.goodlist && n.goodlist.length > 0" class="good-list">
              <div v-for="(g, indexg) in n.goodlist" :key="indexg" class="good-item">
                <div class="good-do" @click="toCheckGood(index, indexg)">
                  <div class="good-do-pos">
                    <ischeck :value="g.ischeck" size="24px"></ischeck>
                  </div>
                </div>
                <div class="good-img" @click="toCheckGood(index, indexg)">
                  <costomImg
                    :url="'images/ecommerce/' + 'pic.png'"
                    :preview-list="[getAssetsFile('images/ecommerce/' + 'pic.png')]"
                    :is-view="false"
                  ></costomImg>
                </div>
                <div class="good-info" @click="toCheckGood(index, indexg)">
                  <div class="good-info-pos">
                    <div class="txt-ellipsis clamp2">{{ g.title || '--' }}</div>
                  </div>
                </div>
                <div class="good-price-num">
                  <div class="good-price-num-pos">
                    <div class="price" @click="toCheckGood(index, indexg)">{{ g.price }} / {{ g.unit }}</div>
                    <div class="total" @click="toCheckGood(index, indexg)">{{ (g.price * g.num).toFixed(2) }}</div>
                    <div class="num">
                      <div class="right-item">
                        <el-input-number v-model="g.num" :min="1">
                          <template #suffix>
                            <span>{{ g.unit }}</span>
                          </template>
                        </el-input-number>
                      </div>
                    </div>
                    <div class="good-del" @click="doSingleDel(index, indexg)">
                      <span>删除</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="fix-bottom">
        <div class="bottom-total">
          <span class="tips">合计</span>
          <span class="total">{{ totalAmout.toFixed(2) }}</span>
        </div>
        <div class="bottom-do">
          <el-button type="primary" @click="toSettlement">结算</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, reactive, computed } from 'vue';
import { isEmpty, getAssetsFile } from '@/utils';
import { useRoute, useRouter } from 'vue-router';
import userHeader from './components/userHeader.vue';
import ischeck from './components/ischeck.vue';
import costomImg from '@/components/costomImg.vue';
import { useApp } from '@/hooks';

const app = useApp();
const route = useRoute();
const router = useRouter();

let total = ref(99);
let isAll = ref(false);

let datalist = reactive([
  {
    id: '01',
    shop: '银河生态农产品有限公司',
    shopimg: '',
    ischeck: false,
    goodlist: [
      { id: '001', title: '耿马镇 原生态 有机 西红柿', price: 4.9, unit: '份', num: 2, ischeck: false },
      { id: '002', title: '耿马镇 原生态 有机 西蓝花', price: 2.6, unit: '份', num: 100, ischeck: false },
    ],
  },
  {
    id: '02',
    shop: '方立生态农产品有限公司',
    shopimg: '',
    ischeck: false,
    goodlist: [
      { id: '001', title: '勐撒镇 原生态 有机 大白菜', price: 4.9, unit: '/份', num: 2, ischeck: false },
      { id: '002', title: '勐撒镇 原生态 有机 生姜', price: 2.6, unit: '/份', num: 100, ischeck: false },
    ],
  },
  {
    id: '03',
    shop: '佳佳生态农产品有限公司',
    shopimg: '',
    ischeck: false,
    goodlist: [
      { id: '001', title: '勐简镇 原生态 有机 花香蓝莓', price: 4.9, unit: '/份', num: 2, ischeck: false },
      { id: '002', title: '勐简镇 原生态古树茶', price: 2.6, unit: '/份', num: 100, ischeck: false },
    ],
  },
]);

let totalAmout = computed(() => {
  let num = 0;
  let list = [];
  if (datalist && datalist.length > 0) {
    list = datalist
      .map((m) => {
        return m.goodlist;
      })
      .flat();

    // console.info('totalAmout**************', list);
    if (list && list.length > 0) {
      num = list.reduce((acc, current) => {
        return acc + (current.ischeck ? current.price * current.num : 0);
      }, 0);
    }
  }

  return num;
});

const toSettlement = () => {};

const toCheckAll = () => {
  isAll.value = !isAll.value;
  console.info('操作全选', isAll.value);
  if (datalist && datalist.length > 0) {
    datalist.forEach((m) => {
      m.ischeck = isAll.value;
      if (m.goodlist && m.goodlist.length > 0) {
        m.goodlist = setCheck(m.goodlist, isAll.value);
      }
    });
  }
};

const setCheck = (data, val) => {
  let list = [];
  if (data && data.length > 0) {
    list = data.map((m) => {
      return { ...m, ischeck: val };
    });
  }

  return list;
};

const toCheckShop = (index) => {
  if (index > -1) {
    datalist[index].ischeck = !datalist[index].ischeck;
    if (datalist[index].goodlist && datalist[index].goodlist.length > 0) {
      datalist[index].goodlist = setCheck(datalist[index].goodlist, datalist[index].ischeck);
    }
    setIsAll();
  }
};

const toCheckGood = (indexP, index) => {
  if (indexP > -1 && index > -1) {
    let list = datalist[indexP].goodlist;
    list[index].ischeck = !list[index].ischeck;
    let len = list.length || 0;
    let checkNum = list.reduce((acc, current) => {
      return acc + (current.ischeck ? 1 : 0);
    }, 0);

    if (checkNum > 0 && checkNum < len) {
      datalist[indexP].ischeck = false;
    } else if (checkNum > 0 && checkNum == len) {
      datalist[indexP].ischeck = true;
    } else {
      datalist[indexP].ischeck = false;
    }
    setIsAll();
  }
};

const setIsAll = () => {
  let len = datalist.length || 0;
  let checkNum = datalist.reduce((acc, current) => {
    return acc + (current.ischeck ? 1 : 0);
  }, 0);

  if (checkNum > 0 && checkNum < len) {
    isAll.value = false;
  } else if (checkNum > 0 && checkNum == len) {
    isAll.value = true;
  } else {
    isAll.value = false;
  }
};

const doSingleDel = (indexP, index) => {
  if (indexP > -1 && index > -1) {
    app
      .$confirm(`删除后信息将不可查看，确认要删除吗？`, '确定删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      .then(() => {
        datalist[indexP].goodlist.splice(index, 1);
      })
      .catch(() => {});
  }
};
</script>
<style lang="scss" scoped>
.my-shoping-car-warp {
  width: 100%;
  .page-content-warp {
    width: 100%;
    height: calc(100vh - 116px);
    position: relative;
    background: $color-fff;
    border-radius: 16px;
    padding: 16px;
    .fix-top,
    .fix-bottom {
      position: absolute;
      left: 0;
      width: 100%;
      padding: 16px;
      z-index: 1;
      background: $color-fff;
    }
    .fix-top {
      top: 0;
      display: inline-flex;
      justify-content: space-between;
      .do-all,
      .batch-del {
        display: inline-block;
        vertical-align: middle;
      }
      .do-all {
        display: inline-flex;
        flex-direction: column;
        justify-content: center;
        .all-txt {
          font-size: 16px;
          vertical-align: middle;
          display: inline-block;
          padding-left: 16px;
        }
      }
      .batch-del {
        border: 1px solid $color-333;
        border-radius: 16px;
        padding: 8px 16px;
        font-size: 16px;
        .el-icon {
          font-size: 18px;
        }
        .del-txt,
        .el-icon {
          display: inline-block;
          vertical-align: middle;
        }
        .del-txt {
          padding-left: 16px;
        }
      }
    }
    .fix-bottom {
      bottom: 0;
      display: inline-flex;
      justify-content: space-between;
      width: 100%;
      .bottom-total,
      .bottom-do {
        display: inline-block;
        vertical-align: middle;
      }
      .bottom-total {
        .tips,
        .total {
          display: inline-block;
          vertical-align: middle;
        }
        .tips {
          font-size: 30px;
        }
        .total {
          font-size: 42px;
          color: $color-main;
          padding-left: 16px;
        }
        .total::before {
          content: '￥';
        }
      }
      .bottom-do {
        flex-direction: column;
        justify-content: center;
        display: inline-flex;
        ::v-deep() {
          .el-button {
            font-size: 18px !important;
            padding: 0 40px !important;
            line-height: 42px !important;
            height: 42px !important;
            display: inline-block !important;
          }
        }
      }
    }
    .conetnt-warp {
      width: 100%;
      padding: 80px 16px 96px 16px;
      overflow-y: auto;
      height: 100%;
      .content-item-warp {
        width: 100%;
        .content-item {
          width: 100%;
          margin-bottom: 16px;
          cursor: pointer;
          .shop-info {
            width: 100%;
            display: inline-flex;
            justify-content: flex-start;
            gap: 16px;
            .shop-do,
            .shop-img,
            .shop-name {
              display: inline-block;
              vertical-align: middle;
            }
            .shop-img {
              width: 32px;
              height: 32px;
              display: inline-flex;
              flex-direction: column;
              justify-content: center;
              border-radius: 4px;
            }
            .shop-do {
              width: 30px;
              display: inline-flex;
              flex-direction: column;
              justify-content: center;
            }
            .shop-name {
              width: calc(100% - 62px);
              font-size: 18px;
            }
          }

          .good-list {
            width: 100%;
            .good-item {
              display: inline-flex;
              width: 100%;
              justify-content: flex-start;
              gap: 16px;
              padding-left: 16px;
              margin: 8px 0;
            }
            .good-do,
            .good-img,
            .good-info,
            .good-price-num {
              display: inline-block;
              vertical-align: middle;
            }
            .good-do {
              display: inline-flex;
              flex-direction: column;
              justify-content: center;
              .good-do-pos {
              }
            }
            .good-img {
              width: 120px;
              height: 120px;
            }
            .good-info {
              width: 200px;
              display: inline-flex;
              flex-direction: column;
              justify-content: center;
              .good-info-pos {
                font-size: 18px;
                color: $color-666;
              }
            }
            .good-price-num {
              width: calc(100% - 340px);
              display: inline-flex;
              justify-content: center;
              flex-direction: column;
              .good-price-num-pos {
                display: inline-flex;
                justify-content: space-around;
                gap: 16px;
                .price,
                .total {
                  font-size: 20px;
                }
                .price {
                  font-weight: 400;
                }
                .total {
                  color: $color-main;
                  font-weight: 700;
                }
                .total::before {
                  content: '￥';
                }
                .good-del {
                  color: $color-999;
                  font-size: 16px;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>
