<template>
    <div class="body">
        <span class="grid" v-for="(v, idx) in grid">
            <div class="equip" v-if="v" @contextmenu.prevent.stop="emit('openEquipMenu', idx, $event)"
                @dblclick="commit('close_equip_tip'); dispatch('useEquip', idx);"
                @mouseover="commit('show_equip_tip', { equip: v, compare: true, e: $event })"
                @mouseleave="commit('close_equip_tip')">
                <EquipIcon :equip="v" />
                <div class="equip-lock" v-if="v.locked">
                    <img :src="lock_icon">
                </div>
            </div>
        </span>
    </div>
    <div class="footer">
        <span class="capacity" :class="{ 'height': itemNum / grid.length > 0.8 }">
            {{ itemNum }}/{{ grid.length }}
        </span>
        <button class="button" @click="neaten">{{ t('neaten') }}</button>
        <button class="button" @click="sellAll">{{ t('sellAll') }}</button>
        <AutoSell />
    </div>

</template>

<script lang="ts" setup>
import { useStore } from "vuex";
import { computed, onMounted, ref } from "vue";
import { useI18n } from "vue3-i18n";
import EquipIcon from "@/components/equip-icon.vue";
import { lock_icon, backpack_num } from "@/config";
import AutoSell from "./auto-sell.vue";

const { t } = useI18n();
const { state, commit, dispatch } = useStore();
const grid = computed(() => {
    return state.grid;
});
const emit = defineEmits(['openEquipMenu'])

const itemNum = computed(() => {
    let i = 0;
    state.grid.forEach((item) => {
        if (item) {
            i++;
        }
    })
    return i;
});

const neaten = () => {
    const tem = new Array(backpack_num);
    let i = 0;
    state.grid.forEach((item) => {
        if (item) {
            tem[i] = item
            i++;
        }
    })
    commit("set_backpack", tem);
}
const sellAll = () => {
    state.grid.forEach((item, index) => {
        if (item && !item.locked) {
            dispatch('sell_equip', index)
        }
    })
}


onMounted(() => { });
</script>
<style lang="scss" scoped>
.body {
    width: 30rem;
    height: 14rem;
    margin-top: 1rem;
    justify-items: flex-start;
    align-items: flex-start;
    flex-wrap: wrap;
    display: flex;
    padding-left: 1.3rem;
}

.grid {
    width: 3.5rem;
    height: 3.5rem;
    border: 1px solid #ccc;
    margin-left: -1px;
    margin-top: -1px;
    box-shadow: inset 0 0 6px 6px rgba($color: #000000, $alpha: 0.5);

    .equip {
        display: flex;
        width: 100%;
        cursor: pointer;
        position: relative;
    }

    .equip-lock {
        position: absolute;
        top: -0.2rem;
        right: -1.1rem;
        border-left: 1.7rem solid transparent;
        border-right: 1.7rem solid transparent;
        border-bottom: 1.7rem solid rgba(255, 0, 0, 0.658);
        transform: rotate(45deg);

        img {
            width: 1rem;
            height: 1rem;
            transform: rotate(-45deg) translate(-50%, -0%);
            position: absolute;
            top: 50%;
            left: 50%;
        }
    }
}

.footer {
    height: 4rem;
    width: 100%;

    .capacity {
        font-size: 1.5rem;
        line-height: 4rem;
        float: left;
        color: white;
    }

    .height {
        color: red;
    }

    .button {
        float: right;
        margin-top: 0.9rem;
    }
}
</style>