<template>
  <div class="shop-pages">
    <div class="shop-head flex-row-start">
      <div class="head-left">
        <div class="left-carousel">
          <div v-for="(item, index) in carousel" :key="index" class="carousel-item" :class="carouselIndex == index ? 'hig' : ''">
            <img :src="item.img" alt="" @click="onCarousel(1, index)" />
          </div>
        </div>
        <div class="left-next flex-row-center" @click="onCarousel(2)">
          <el-icon color="#6A7282" size="15">
            <ArrowDown />
          </el-icon>
        </div>
      </div>
      <img :src="carousel[carouselIndex].img" alt="" class="carousel-img" />
      <div class="head-right flex-column-between">
        <div>
          <div class="right-title">{{ dataInfo.itemName || '' }}</div>
          <div class="right-num">商品库存 999套</div>
          <div class="right-price flex-row-between">
            <div class="flex-row-start">
              <div class="price1">
                <span>¥</span>
                <span style="font-size: 24px">2654.00</span>
              </div>
              <div class="price2">平台价</div>
              <div class="price3">
                <span>¥</span>
                <span style="font-size: 16px">2654.00</span>
              </div>
            </div>
            <div class="right-collect flex-row-start" @click="editCollection">
              <img src="@/assets/images/dark.svg" alt="" />
              <span>{{ collection ? '已收藏' : '收藏' }}</span>
            </div>
          </div>
          <div class="address flex-row-start">
            <img class="address-img" src="@/assets/images/dark.svg" alt="" />
            <div>配送至</div>
            <div class="address-text ellipsis">湖北省武汉市洪山区湖北省武汉市洪山区</div>
            <el-icon color="#000000" size="15">
              <ArrowDown />
            </el-icon>
          </div>
          <div class="specs-bos">
            <div v-for="(item1, index1) in specsList" :key="index1" class="specs-list">
              <div>{{ item1.title }}</div>
              <div class="specs-box">
                <div
                  v-for="(item2, index2) in item1.list"
                  :key="index2"
                  class="specs-item"
                  :class="item2.id == item1.id ? 'hig' : item2.disabled ? 'disabled' : ''"
                >
                  {{ item2.title }}
                </div>
              </div>
            </div>
          </div>
          <div class="number-bos">
            <div>数量</div>
            <div class="flex-row-start number-box">
              <el-input-number v-model="num" :min="1" :max="10" @change="handleChange" />
              <div style="margin-left: 10px">本产品限购2件</div>
            </div>
          </div>
        </div>
        <div class="bnt-bos flex-row-start">
          <div @click="onCart">加入购物车</div>
          <div>立即购买</div>
        </div>
      </div>
    </div>
    <div class="nav-bos flex-row-start">
      <div v-for="(item, index) in navList" :key="index" :class="index == 0 ? 'hig' : ''">{{ item.title }}</div>
    </div>
    <img class="shop-img" src="@/assets/images/login-background.jpg" alt="" />
    <div class="shop-more flex-row-between">
      <div class="flex-row-start">
        <div class="more1">更多推荐</div>
        <div class="more2">甄选大牌,优质好品</div>
      </div>
      <div class="flex-row-start">
        <div class="more3">查看更多</div>
        <el-icon color="#364153" size="14">
          <ArrowRight />
        </el-icon>
      </div>
    </div>
    <div class="data-bos">
      <div v-for="(item, index) in 3" :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>
    <!-- 取消收藏 -->
    <el-dialog
      v-model="dialogVisible"
      title="选择取消的收藏夹"
      width="500"
      :before-close="
        () => {
          dialogVisible = false;
        }
      "
    >
      <div>
        <el-radio-group v-model="radio">
          <el-radio v-for="(item, index) in favorites" :key="index" :value="item.id">{{ item.title }}</el-radio>
        </el-radio-group>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="onCancel"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import back1 from '@/assets/images/login-background.jpg';
import back2 from '@/assets/images/login-background.jpg';
import back3 from '@/assets/images/login-background.jpg';
import back4 from '@/assets/images/login-background.jpg';
import back5 from '@/assets/images/login-background.jpg';
import { getToken } from '@/utils/auth';

import {
  getProductDetail,
  addProductShoppingCart,
  isProductInDefaultCollect,
  addProductBrowsingHistory,
  addProductCollect,
  favoritesList,
  cancelProductCollect
} from '@/api/goods/index';
import { edit } from '@/api/workflow/definition';

const route = useRoute();
const id = ref<any>(null);
const dataInfo = ref<any>({});

const radio = ref<any>(null);
const favorites = ref<any>([]);
const collection = ref<any>(null);
const dialogVisible = ref<any>(false);
onMounted(() => {
  id.value = route.query.id;
  getInfo();

  if (getToken()) {
    getCollection();
    // 浏览记录
    addProductBrowsingHistory(id.value).then((res) => {});
  }
});

// 商品相信
const getInfo = () => {
  getProductDetail(id.value).then((res) => {
    if (res.code == 200) {
      dataInfo.value = res.data;
    }
  });
};

// 收藏
const getCollection = () => {
  isProductInDefaultCollect(id.value).then((res) => {
    if (res.code == 200) {
      collection.value = res.data;
    }
  });
};

//修改收藏
const editCollection = () => {
  if (collection.value) {
    dialogVisible.value = true;
    favoritesList(id.value).then((res) => {
      if (res.code == 200) {
        if (res.rows.length > 0) {
          radio.value = res.rows[0].id;
        }
        favorites.value = res.rows;
      }
    });
  } else {
    // 添加
    addProductCollect({ productId: id.value }).then((res) => {
      if (res.code == 200) {
        dataInfo.value = res.data;
        getCollection();
      }
    });
  }
};

// 取消收藏
const onCancel = () => {
  cancelProductCollect({ productId: id.value, favoritesId: radio.value }).then((res) => {
    if (res.code == 200) {
      getCollection();
      dialogVisible.value = false;
    }
  });
};

const num = ref<any>(1);
const carousel = ref<any>([
  {
    img: back1,
    id: 1
  },
  {
    img: back2,
    id: 2
  },
  {
    img: back3,
    id: 3
  },
  {
    img: back4,
    id: 4
  },
  {
    img: back5,
    id: 5
  }
]);
const navList = ref<any>([{ title: '商品详情' }, { title: '参数信息' }, { title: '售后服务说明' }, { title: '品牌信息' }, { title: '商品评价' }]);
const specsList = ref<any>([
  {
    title: '规格参数01',
    id: 1,
    list: [
      { title: '极光蓝', id: 1, disabled: false },
      { title: '极光蓝', id: 2, disabled: false },
      { title: '极光蓝', id: 3, disabled: false }
    ]
  },
  {
    title: '规格参数02',
    id: 1,
    list: [
      { title: '极光蓝', id: 1, disabled: false },
      { title: '极光蓝', id: 2, disabled: false },
      { title: '极光蓝', id: 3, disabled: true }
    ]
  }
]);
const carouselIndex = ref<any>(0);

const onCarousel = (type: number, index?: any) => {
  if (type == 1) {
    carouselIndex.value = index;
  } else {
    carouselIndex.value++;
    if (carouselIndex.value > carousel.value.length - 1) {
      carouselIndex.value = 0;
    }
  }
};

const handleChange = (val: any) => {
  // num.value = val;
};

const onCart = () => {
  addProductShoppingCart({
    productId: id.value,
    productNum: 3
  }).then((res) => {
    if (res.code == 200) {
      ElMessage.success('加入购物车成功');
    }
  });
};
</script>

<style lang="scss" scoped>
.shop-pages {
  width: 1200px;
  margin: 0 auto;

  .shop-head {
    width: 1200px;

    .head-left {
      width: 106px;

      .left-carousel {
        height: 540px;
        overflow-y: auto;

        .carousel-item {
          width: 100px;
          height: 100px;
          border-radius: 6px;
          overflow: hidden;
          margin-top: 10px;
          cursor: pointer;

          &:nth-of-type(1) {
            margin-top: 0px;
          }

          &.hig {
            border: 1.5px solid #e7000b;
          }

          img {
            width: 100px;
            height: 100px;
          }
        }
      }

      .left-next {
        width: 100px;
        height: 36px;
        background: #ffffff;
        border-radius: 6px 6px 6px 6px;
        margin-top: 10px;
        cursor: pointer;
      }
    }

    .carousel-img {
      width: 586px;
      height: 586px;
      border-radius: 10px;
      margin-left: 4px;
      margin-right: 10px;
    }

    .head-right {
      width: 494px;
      height: 586px;
      background: #ffffff;
      border-radius: 10px 10px 10px 10px;
      padding: 20px 20px 30px 20px;

      .right-title {
        height: 56px;
        font-weight: 600;
        font-size: 20px;
        color: #101828;
      }

      .right-num {
        font-size: 14px;
        color: #6a7282;
      }

      .right-price {
        margin-top: 10px;

        .price1 {
          font-size: 12px;
          color: #e7000b;
        }

        .price2 {
          width: 52px;
          height: 21px;
          background: #e7000b;
          border-radius: 2px;
          font-size: 12px;
          color: #ffffff;
          line-height: 21px;
          text-align: center;
          margin: 0 16px 0 8px;
        }

        .price3 {
          font-size: 12px;
          color: #6a7282;
          text-decoration: line-through;
        }

        .right-collect {
          font-size: 14px;
          color: #6a7282;
          cursor: pointer;

          img {
            width: 12px;
            height: 12px;
            margin-right: 6px;
          }
        }
      }

      .address {
        height: 44px;
        width: 100%;
        border-top: 1px solid #e5e7eb;
        border-bottom: 1px solid #e5e7eb;
        margin-top: 20px;
        font-size: 14px;
        color: #000000;

        .address-img {
          width: 16px;
          height: 16px;
          margin-right: 10px;
          margin-top: 2px;
        }

        .address-text {
          margin-left: 10px;
          width: 138px;
        }
      }

      .specs-bos {
        .specs-list {
          margin-top: 20px;
          font-size: 14px;
          color: #364153;
        }

        .specs-box {
          display: flex;
          gap: 8px 10px;
          padding-top: 8px;

          .specs-item {
            padding: 0 14px;
            height: 32px;
            border-radius: 4px 4px 4px 4px;
            border: 1px solid #e8e8ea;
            line-height: 32px;
            cursor: pointer;

            &.hig {
              border: 1px solid #e7000b;
              color: #e7000b;
            }

            &.disabled {
              opacity: 0.5;

              &:hover {
                cursor: not-allowed;
              }
            }
          }
        }
      }

      .number-bos {
        margin-top: 20px;
        font-size: 14px;
        color: #364153;

        .number-box {
          font-size: 14px;
          color: #6a7282;
          margin-top: 8px;
        }
      }

      .bnt-bos {
        width: 428px;
        height: 44px;
        border-radius: 10px;
        overflow: hidden;

        div {
          width: 214px;
          height: 44px;
          line-height: 44px;
          text-align: center;
          cursor: pointer;

          &:nth-of-type(1) {
            background: #fcecf1;
            color: #e7000b;
          }

          &:nth-of-type(2) {
            background: #e7000b;
            color: #ffffff;
          }
        }
      }
    }
  }

  .nav-bos {
    width: 696px;
    height: 48px;
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    margin-top: 20px;
    gap: 0 32px;
    padding-left: 32px;
    font-size: 16px;
    color: #364153;

    div {
      cursor: pointer;
    }

    .hig {
      color: #101828;
    }
  }

  .shop-img {
    width: 696px;
    height: 752px;
    margin-top: 20px;
  }

  .shop-more {
    width: 696px;
    margin-top: 34px;

    .more1 {
      font-weight: 600;
      font-size: 20px;
      color: #101828;
    }

    .more2 {
      font-size: 14px;
      color: #364153;
      margin-left: 10px;
    }

    .more3 {
      font-size: 14px;
      color: #364153;
      margin-right: 6px;
      cursor: pointer;
    }
  }

  //数据
  .data-bos {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
    margin-bottom: 40px;
    margin-top: 24px;

    .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;
      }
    }
  }
}
</style>
