<template>
  <div class="content">
    <div class="cart_wrap card">
      <div class="menu_top">
        <div class="menu">
          <el-icon class="icon">
            <TakeawayBox />
          </el-icon>
          <el-text class="t">(1)</el-text>
        </div>
        <div class="number" @click="takeFoodCodeRef.show()">
          <el-text class="t">{{ foodCode }}</el-text>
        </div>
        <div class="select_user">
          <el-icon class="icon">
            <UserFilled />
          </el-icon>
          <el-text class="t">选择会员</el-text>
          <el-icon class="arrow">
            <ArrowRight />
          </el-icon>
        </div>
      </div>
      <div class="shop_operation">
        <div class="shop_list">
          <div class="item" :class="{ active: item.active }" v-for="item in cartList" :key="item.id"
            @click="selectCartItemHandle(item)">
            <div class="name_wrap">
              <span>{{ item.name }}</span>
              <span>￥{{ item.salePrice }}</span>
            </div>
            <div class="sku_list">
              <div class="tag">默认1份</div>
              <div class="tag">热</div>
              <div class="tag">半糖</div>
            </div>
            <div class="num">
              <el-text>X{{ item.number }}</el-text>
            </div>
          </div>
        </div>
        <!-- 购物车操作栏 -->
        <cartOperation :item="selectCartItem" @confirm="addCart" />
      </div>
      <div class="footer">
        <div class="top">
          <div class="left" @click="allSelected = !allSelected">
            <div class="selected">
              <div class="selected_round" v-if="!allSelected"></div>
              <el-icon class="icon" v-else>
                <CircleCheckFilled />
              </el-icon>
            </div>
            <el-text class="t">打包(￥0.00)</el-text>
          </div>
          <div class="num-wrap">
            <el-text>共6种商品，7件</el-text>
          </div>
        </div>
        <div class="btm">
          <el-button icon="Edit" @click="remarkRef.show()"></el-button>
          <div class="button">
            <el-button type="primary" style="width: 100%;">
              <div class="js">
                <el-text class="t">￥3.09</el-text>
                <el-text class="t" style="margin-left: 250px;">结算</el-text>
              </div>
            </el-button>
          </div>
        </div>
      </div>
    </div>
    <div class="shop_manage card">
      <!-- 分类/商品列表 -->
      <goods @success="addCart" />
    </div>
  </div>
  <!-- 备注 -->
  <remarkModal ref="remarkRef" />
  <!-- 修改取餐号 -->
  <takeFoodCode ref="takeFoodCodeRef" title="修改取餐号" placeholder="请输入取餐号" @success="takeFoodCodeSuccess" />
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useUser } from "@/store/user.js"
import remarkModal from '@/components/remarkModal.vue'
import takeFoodCode from '@/components/takeFoodCode.vue'
import cartOperation from '@/views/home/components/cartOperation.vue'

import { createCart, queryCart, createCode } from '@/api/product'

// 商品列表
import goods from '@/views/home/components/goods.vue'

const store = useUser()
const remarkRef = ref(null)
const takeFoodCodeRef = ref(null)

const allSelected = ref(false)

const cartList = ref([])

// 取餐码
const foodCode = ref('#006')

// 确认取餐号
function takeFoodCodeSuccess(code) {
  console.log('取餐号===', code)
}

const selectCartItem = ref({})
// 从购物车选择商品
function selectCartItemHandle(item) {
  cartList.value.map(val => {
    val.active = false
  })
  item.active = true
  selectCartItem.value = item
}

// 选择完规格开始添加购物车
async function addCart(params) {
  try {
    const res = await createCart({
      productId: params.productId,
      maskerId: foodCode.value,
      shopId: store.userInfo.shopId,
      skuId: params.id,
      number: params.number || 1
    })
    foodCode.value = res
  } catch (error) {
    console.log(error)
  }
}

// 获取购物车商品
async function queryCartAjax() {
  try {
    const res = await queryCart({
      masterId: foodCode.value,
      shopId: store.userInfo.shopId
    })
    cartList.value = res.list.map((item, index) => {
      if (index == 0) {
        item.active = true
        selectCartItem.value = item
      } else {
        item.active = false
      }
      return item
    })
    console.log(cartList.value)
  } catch (error) {
    console.log('获取购物车商品', error)
  }
}

// 获取取件码
async function createCodeAjax() {
  try {
    const res = await createCode({ shopId: store.userInfo.shopId })
    foodCode.value = res
    queryCartAjax()
  } catch (error) {
    console.log(error)
  }
}

onMounted(() => {
  // createCodeAjax()
  queryCartAjax()
})
</script>

<style scoped lang="scss">
.cart_wrap {
  width: 550px;
  height: 100%;
  display: flex;
  flex-direction: column;

  .menu_top {
    flex-shrink: 0;
    display: flex;
    height: 60px;
    $fs: 20px;

    .menu {
      background-color: var(--el-color-warning);
      color: #fff;
      width: 100px;
      display: flex;
      align-items: center;
      justify-content: center;

      .icon {
        font-size: 24px;
        position: relative;
        top: 2px;
      }

      .t {
        color: #fff;
        margin-left: 4px;
        font-size: $fs;
      }
    }

    .number {
      flex: 1;
      display: flex;
      align-items: center;
      background-color: var(--el-color-info-light-7);
      padding-left: 20px;

      .t {
        font-size: $fs;
      }
    }

    .select_user {
      display: flex;
      align-items: center;
      background-color: var(--el-color-info-light-8);
      padding: 0 20px;

      .icon {
        color: var(--el-color-primary);
        font-size: 24px;
      }

      .t {
        font-size: $fs;
        padding: 0 10px;
      }

      .arrow {
        color: #999;
        font-size: 20px;
        position: relative;
        top: 2px;
      }
    }
  }

  .shop_operation {
    flex: 1;
    display: flex;

    .shop_list {
      flex: 1;
      height: calc(100vh - 40px - 60px - 130px);
      overflow-y: auto;
      border-right: 1px solid #ececec;

      .item {
        padding: 20px;

        &.active {
          background-color: #f5f5f5;
        }

        &:not(:last-child) {
          border-bottom: 1px solid #ececec;
        }

        .name_wrap {
          display: flex;
          justify-content: space-between;
          font-size: 20px;
        }

        .sku_list {
          display: flex;
          padding-top: 10px;

          .tag {
            padding: 4px 10px;
            background-color: var(--el-color-danger);
            color: #fff;
            margin-right: 10px;
          }
        }

        .num {
          padding-top: 10px;
          display: flex;
          justify-content: flex-end;
        }
      }
    }
  }
}

.footer {
  padding: 20px;
  border-top: 1px solid #ececec;

  .left {
    display: flex;
    align-items: center;

    .selected {
      width: 20px;
      height: 20px;
      display: flex;
      align-items: center;
      position: relative;
      top: 1px;

      .icon {
        display: block;
        font-size: 20px;
        color: var(--primary-color);
      }

      .selected_round {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        border: 1px solid #ddd;
      }
    }

    .t {
      margin-left: 6px;
    }
  }

  .top {
    display: flex;
    justify-content: space-between;
  }

  .btm {
    $h: 70px;
    display: flex;
    height: $h;
    padding-top: 20px;
    gap: 20px;

    .editor {
      width: $h;
      border: 1px solid #ececec;
      border-radius: 6px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #555;
      font-size: 22px;
    }

    .button {
      flex: 1;


      .js {

        .t {
          color: #fff;
          font-size: 20px;
        }
      }
    }
  }
}

.shop_manage {
  flex: 1;
  margin-left: 20px;
  height: 100%;
}
</style>
