<template>
    <el-dialog v-model="dialogVisible" width="637" class="dialog-box"
        align-center
        destroy-on-close
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :show-close="false">
        <template #header="{ close, titleId, titleClass }">
            <div class="msg-header" >
                <!-- <svg-icon icon-class="leverage" class="header-icon" /> -->
                <div class="header-title">{{ t('activity.addrMgr') }}</div>
                <div class="header-close" >
                    <el-icon color="rgba(196, 196, 196, 1)" @click="close" ><Close /></el-icon>
                </div>
            </div>
        </template>
        <div class="msg-body" >
            <div class="addr-list" v-if="list && list.length" >
                <div class="addr-item" v-for="(item, i) in list" :key="i" >
                    <el-descriptions :column="2" >
                        <el-descriptions-item :label="t('activity.country')">
                            <span style="color: #333333;font-weight: 700;font-size: 20px;" >{{ item.country }}</span>
                        </el-descriptions-item>
                        <el-descriptions-item :label="t('activity.userCity')">
                            <span style="color: #333333;font-weight: 700;font-size: 20px;" >{{ item.city }}</span>
                        </el-descriptions-item>
                        <el-descriptions-item :label="t('activity.recipients')">
                            <span style="color: #333333;font-weight: 700;font-size: 20px;" >{{ item.recipients }}</span>
                        </el-descriptions-item>
                        <el-descriptions-item :label="t('activity.phone')">
                            <span style="color: #333333;font-weight: 700;font-size: 20px;" >{{ item.phone }}</span>
                        </el-descriptions-item>
                        <el-descriptions-item :span="2" :label="t('activity.addr')">
                            <span style="color: #333333;font-size: 16px;" >{{ item.delivery_address }}</span>
                        </el-descriptions-item>
                    </el-descriptions>
                    <el-divider style="margin-top: 18px;margin-bottom: 12px;" />
                    <div>
                        <el-button style="min-width: 94px;" type="primary" @click="change(item)" >{{ t('activity.useAddr') }}</el-button>
                        <el-button text type="info" @click="openEdit(item)" >
                            <svg-icon icon-class="editAddr" style="font-size: 14px;margin-right: 7px;" />
                            <span>{{ t('activity.edit') }}</span>
                        </el-button>
                        <el-button text type="info" @click="del(item.id)" >
                            <svg-icon icon-class="delAddr" style="font-size: 14px;margin-right: 7px;" />
                            <span>{{ t('activity.del') }}</span>
                        </el-button>
                    </div>
                </div>
            </div>
            <div style="display: flex;width: 100%;justify-content: center;padding-top: 10px;">
                <el-button text @click="openAdd" style="width: 264px;font-size: 20px;" type="primary" icon="Plus" >{{ t('activity.add') }}</el-button>
            </div>
        </div>
        <el-dialog v-model="showEdit" width="500" append-to-body >
            <el-form ref="postRef" :model="form" autocapitalize="off" size="large" label-position="top" style="user-select: none;" >
                <el-form-item :label="t('activity.country')" >
                    <el-input v-model="form.country" maxlength="255" />
                </el-form-item>
                <el-form-item :label="t('activity.userCity')" >
                    <el-input v-model="form.city" maxlength="255" />
                </el-form-item>
                <el-form-item :label="t('activity.phone')" >
                    <el-input v-model="form.phone" maxlength="32" />
                </el-form-item>
                <el-form-item :label="t('activity.recipients')" >
                    <el-input v-model="form.recipients" maxlength="64" />
                </el-form-item>
                <el-form-item :label="t('activity.addr')" >
                    <el-input v-model="form.delivery_address" maxlength="255" />
                </el-form-item>
                <el-form-item>
                    <div style="display: flex;width: 100%;justify-content: center;">
                        <el-button @click="save" class="dark-btn" style="width: 264px;" type="primary">{{ t('common.okText') }}</el-button>
                        <el-button @click="showEdit = false" style="width: 264px;" type="primary">{{ t('common.cancelText') }}</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-dialog>
    </el-dialog>

</template>
    
<script setup>
import { useLocale } from 'element-plus'
import { ref, defineModel, watch, onMounted } from 'vue';
import { serverLang } from '@/utils/i18n'
import { delAddr, saveAddr, queryAddrList } from '@/api/shop'

const { proxy } = getCurrentInstance();
const { t } = useLocale()
const dialogVisible = defineModel("show")
const emit = defineEmits(['change'])

const showEdit = ref(false)

const d = reactive({
    form: {
        id: undefined,
        country: undefined,
        city: undefined,
        delivery_address: undefined,
        phone: undefined,
        recipients: undefined,
        qcc_language: serverLang()
    }
});

const { form } = toRefs(d);

const list = ref([])

function save() {
    saveAddr(form.value).then(resp => {
        window.showTip({
            title: t('common.success.action'),
            status: 'success'
        })
        showEdit.value = false
        loadData()
    })
}

function change(data) {
    dialogVisible.value = false
    emit('change', data)
}

watch(dialogVisible, (n, o) => {
    if(!n) {
        proxy.resetForm("postRef");
        list.value = []
    } else {
        loadData()
    }
})

function loadData() {
    queryAddrList().then(resp => {
        list.value = resp.data
    })
}

function del(id) {
    delAddr({id}).then(resp => {
        window.showTip({
            title: t('common.success.action'),
            status: 'success'
        })
        loadData()
    })
}

function openEdit(data) {
    form.value.id = data.id
    form.value.country = data.country
    form.value.city = data.city
    form.value.delivery_address = data.delivery_address
    form.value.phone = data.phone
    form.value.recipients = data.recipients
    showEdit.value = true
}

function openAdd() {
    form.value.id = undefined
    form.value.country = undefined
    form.value.city = undefined
    form.value.delivery_address = undefined
    form.value.phone = undefined
    form.value.recipients = undefined
    showEdit.value = true
}

</script>

<style lang="scss">
.dialog-box {
    border-radius: 16px;
    padding: 0;

    .el-dialog__header {
        margin-right: 0;
        padding: 0;
        overflow: hidden;
    }

    .el-dialog__body {
        box-sizing: border-box;
        padding: 16px 22px;
        min-height: 250px;
    }

    .msg-header {
        border-radius: 16px 16px 0 0;
        width: 100%;
        height: 50px;
        position: relative;
        overflow: hidden;
        background-color: #0F3675;
        display: flex;
        align-items: center;
        color: #fff;

        .header-icon {
            width: 25px;
            height: 28px;
            margin-left: 22px;
            margin-right: 14px;
        }

        .header-title {
            margin-left: 22px;
            font-size: 26px;
            font-weight: 700;
        }

        .header-close {
            cursor: pointer;
            position: absolute;
            bottom: 0;
            right: 20px;
            font-size: 32px;
        }
    }

    .msg-body {
        background-color: #ffffff;

        .addr-list {
            width: 100%;
            height: 500px;
            overflow-y: auto;

            .addr-item {
                box-sizing: border-box;
                padding: 10px;
                width: 100%;
                margin-bottom: 20px;

                .el-descriptions__label {
                    font-size: 16px;
                }
            }
        }
    }
}
</style>