<script setup>
import { ref, onMounted, onUnmounted  } from 'vue'
import { ElMessageBox } from 'element-plus'
import { getAttrValue } from "@/api/shop.js"
import mitt from "@/utils/mitt.js";

const dialogVisible = ref(false)

const show = (e)=>{
    dialogVisible.value = e;
}

const form = ref({});
const active = ref(null);
const loading = ref(false);
const mode = ref('add');
const editForm = ref({});
const setForm = (data, type='add')=>{
    mode.value = type;
    if(type == 'add'){
        form.value = data;
        active.value = data.attr[0];
    }
    else {
        loading.value = true;
        editForm.value = data;
        getAttrValue(data.product_id).then(res=>{
            res.data.attrValue = JSON.parse(JSON.stringify(res.data.attr))
            res.data.attr = Object.keys(res.data.sku)
            form.value = res.data;
            active.value = res.data.attr[0];
            loading.value = false;
        }).catch(err=>{
            loading.value = false;
        })
    }
}

const emit = defineEmits(['changeItem'])

const changeItem = ()=>{
    if(mode.value == 'add') emit('changeItem', form.value, active.value);
    else emit('editItem', editForm.value.cart_id, {
        cart_num: editForm.value.cart_num,
        product_attr_unique: form.value.sku[active.value].unique,
    });
    dialogVisible.value = false
}

const changeActive = (item)=>{
    active.value = item;
}

defineExpose({
    show,
    setForm
})

// 键盘事件
const aleft = () => {
  if(!dialogVisible.value) return;
  let index = form.value.attr.indexOf(active.value);
  if(index>0) return changeActive(form.value.attr[index-1]);
};
const aright = () => {
  if(!dialogVisible.value) return;
  let index = form.value.attr.indexOf(active.value);
  if(index<form.value.attr.length-1) return changeActive(form.value.attr[index+1]);
};
const aenter = ()=>{
    if(!dialogVisible.value) return;
    changeItem();
}
onMounted(() => {
  mitt.on("left", aleft);
  mitt.on("right", aright);
  mitt.on("enter", aenter);
});

onUnmounted(() => {
  mitt.off("left", aleft);
  mitt.off("right", aright);
  mitt.off("enter", aenter);
});

const close = ()=>{
    console.log('sss');
}
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    title="商品规格"
    width="650"
  >
    <div class="shop" v-loading="loading">
        <div class="shop-info" v-if="form.sku">
            <div class="shop-info-left">
                <el-image loading="lazy" :src="form.sku[active]?.image || form.image"></el-image>
            </div>
            <div class="shop-info-right">
                <div class="shop-info-right-top">{{ form.store_name }}</div>
                <div class="shop-info-right-center">库存{{ form.sku[active]?.stock || 0 }}</div>
                <div class="shop-info-right-price">¥<span>{{form.sku[active]?.price || form.price}}</span></div>
            </div>
        </div>
        <div class="shop-sku">
            <div class="title">产品</div>
            <div class="sku">
                <el-space wrap :size="20">
                    <div class="sku-item " :class="{'sku-item_active': active==item}" @click="changeActive(item)" v-for="(item, index) in form.attr" :key="index">{{ item || '默认规格' }}</div>
                </el-space>
                
            </div>
        </div>
    </div>
    <template #footer v-if="!(mode != 'add' && form.attr && form.attr.length==1)">
      <div class="dialog-footer">
        <el-button class="ok-btn" type="primary" @click="changeItem">
          确定 (Enter)
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
.dialog-footer{
    .ok-btn{
        width: 100%;
        height: 2.5rem;
        border-radius: 2.5rem;
    }
}
.shop{
    border-top: 1px solid #eee;
    padding-top: 1rem;
    .shop-info{
        display: flex;
        .shop-info-left{
            flex-shrink: 0;
            margin-right: 0.8rem;
            height: 8rem;
            width: 8rem;
            overflow: hidden;
            border-radius: 0.5rem;
        }
        .shop-info-right{
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            .shop-info-right-top{
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                -webkit-line-clamp: 2; /* 限制文本显示为两行 */
                font-size: 1.1rem;
            }
            .shop-info-right-center{
                font-size: 0.8rem;
            }
            .shop-info-right-price{
                color: #ff4a00;
                font-size: 1.2rem;
                font-weight: bold;
                span{
                    font-size: 1.4rem;
                    margin-left: 0.2rem;
                }
            }
        }
    }
    .shop-sku{
        min-height: 20rem;
        .title{
            font-size: 1.1rem;
            font-weight: bold;
            padding: 1rem 0 0.5rem 0;
        }
        .sku{
            .sku-item{
                cursor: pointer;
                padding: 0.5rem 1rem;
                background-color: #f5f5f5;
                border-radius: 3rem;
                &_active{
                    background-color: #1890ff;
                    color: #fff;
                }
            }
        }
    }
}
</style>
