<template>
    <div class="center">
        <div class="title">Create NFTs</div>
        <div class="content">
            <div class="l">
                <div class="name">Preview</div>
                <template v-if="configList.length">
                    <div class="show">
                        <div class="card" :class="selectItem.modelName">
                            <div class="logo">
                                <img v-if="uploadItem && uploadItem.imagePath" :src="uploadItem.imagePath" alt="">
                            </div>
                            <div class="member">{{ projectName === '' ? 'xxxx' : projectName }}</div>
                            <div class="number">{{ projectNo === '' ? '0000' : projectNo }}</div>
                        </div>
                        <img class="bg" :src="selectItem.mainImagePath" />
                    </div>
                    <div class="list">
                        <div
                            class="item"
                            :class="{ on: item.modelName === selectItem.modelName }"
                            @click="select(item, index)"
                            v-for="(item, index) in configList"
                            :key="index">
                            <img :src="item.previewImagePath" alt="" />
                        </div>
                    </div>
                </template>
                <div class="wait" v-else>
                    <img width="30" src="../../static/img/icon-loading-gray.png" alt="" />
                </div>
            </div>
            <div class="r">
                <div class="name">Card face</div>
                <div class="face">
                    <input type="file" class="file" @change="uploadImg" accept="image/png,image/jpg,image/jpeg"  />
                    <div class="on" v-if="uploadItem && uploadItem.imagePath">
                        <img src="../../static/img/icon-add-cover-on.svg" alt="" />
                        <img class="img" :src="uploadItem.imagePath" alt="" />
                    </div>
                    <div class="off" v-else>
                        <img src="../../static/img/icon-add-cover-off.svg" alt="" />
                    </div>
                </div>
                <div class="desc">Recommended size 500*500 or more</div>
                <div class="name">Project Name</div>
                <div class="input"><input type="text" maxlength="30" v-model="projectName" placeholder="Your NFT Project Name" /></div>

                <div class="name">Project Description</div>
                <div class="input"><input type="text" maxlength="100" v-model="projectDesc" placeholder="Your NFT Project Description" /></div>

                <div class="name">Collection Size</div>
                <div class="input"><input type="text" v-model="projectSize" placeholder="0" /></div>
                <div class="showNo" v-if="showNoStr">No.{{ projectNo === '' ? '0000' : projectNo }}~{{projectSize}}</div>

                <div class="name">NFTs Price</div>
                <div class="price">
                    <div class="currency" @click="showCurrencyDialog" v-if="currencyItem">
                        <img class="head" :src="currencyItem.iconPath" alt="" />
                        <div class="font">{{currencyItem.tokenSymbol}}</div>
                        <img class="arrow" src="../../static/img/icon-add-arrow.svg" alt="" />
                    </div>
                    <div class="no-select" @click="showCurrencyDialog" v-else>
                        <div class="font">Select a reward</div>
                        <img class="arrow" src="../../static/img/icon-add-arrow-white.svg" alt="" />
                    </div>
                    <div class="input"><input type="number" v-model="projectPrice" @input="changePrice" placeholder="0" /></div>
                    <!-- 货币列表 -->
                    <div class="currency-pop" v-if="currencyDialog">
                        <currency-list @selectCurrencyItem="selectCurrencyItem"></currency-list>
                    </div>
                </div>

                <div class="explain">
                    <ul>
                        <li class="special">The NFT colection is minted on BNB Smart Chain (BEP20)</li>
                        <li>NFT will be released in blind box mode</li>
                        <li>Users need to pay service fees when transferring NFTs</li>
                    </ul>
                </div>
                <div class="footer"></div>
            </div>
        </div>
        <div class="create">
            <button class="on" v-if="isNext" @click="next">Create</button>
            <button class="off" v-else>Create</button>
        </div>
    </div>

    <div class="feedBack" @click="feedback">
        <a href="mailto:service@cybertogether.net">
            <div class="mail">
                <img src="../../static/img/icon-feedback.svg" alt="" />
            </div>
            <div class="font">Feedback</div>
        </a>
    </div>

    <div class="loading" v-if="showLoading">
        <img src="../../static/img/icon-add-loading.svg" alt="" />
    </div>

    <div class="succ" v-if="showSuccess">
        <img class="icon" src="../../static/img/icon-notice-succ.svg" alt="" />
        <div class="notic">We have created your NFT collection</div>
        <button class="btn"  @click="jumpList">Done</button>
    </div>
    <div class="succ-bg" v-if="showSuccess" @click="hideSuccess"></div>

    <div class="mask-bg" v-if="currencyDialog" @click="hideCurrencyDialog"></div>
</template>

<script lang="ts" setup>
import { ref, onMounted, watchEffect } from 'vue';
import Api from '../../static/http/api';
import { postRequest } from '../../static/http';
import { uploadFile } from '../../static/utils/upload'
import { Report } from '../../static/report'
import { businessType, pageSource, objectType } from '../../static/report/enum'
import currencyList from '../../components/currency-list.vue';

const isNext = ref(false);
const showLoading = ref(false);
const showSuccess = ref(false);
const maxSize = ref(1000);
const configList = ref([]);
const selectItem = ref(null);
const uploadItem = ref({});
const currencyDialog = ref(false);
const currencyItem = ref(null);
const projectName = ref('');
const projectDesc = ref('');
const projectSize = ref('');
const projectNo = ref('');
const projectPrice = ref('');
const showNoStr = ref(false);
const buttonType = {
    feedback: 'feedback-button',
    create: 'create-button',
}

const getConfig = () => {
    postRequest(Api.createConfig).then(res => {
        let { code, data } = res;
        if ( code === 0 ) {
            configList.value = data.itemModels;
            maxSize.value = data.maxCollectionSize;
            selectItem.value = data.itemModels[0];
        }
    })
}

const next = () => {
    // show loading
    showLoading.value = true;
    // post 
    postRequest(Api.userNftAdd, {
        params: {
            // 项目图标
            cardFaceImagePath: uploadItem.value && uploadItem.value.objectKey || '',
            // 发行数量
            nftCollectionSize: projectSize.value || '',
            // 选中的模版id
            nftItemImageModel: selectItem.value && selectItem.value.modelName || '',
            // 项目描述
            nftProjectDescription: projectDesc.value || '',
            // 项目名称
            nftProjectName: projectName.value || '',
            // 销售价格
            saleCurrencyAmount: projectPrice.value || '',
            // 销售的货币code
            saleCurrencyCode: currencyItem.value && currencyItem.value.currencyCode || '',
        }
    }).then(res => {
        if (res.code === 0) {
            showSuccess.value = true;
        }
    }).finally(() => {
        showLoading.value = false;
    })

    // Report
    Report({
        baseInfo: {
            pageSource: pageSource.creatorPage,
        },
        params: {
            eventData: {
                businessType: businessType.buttonClick,
                objectType: buttonType.create,
            }
        }
    })
}

const select = (item: any, index: number) => {
    selectItem.value = item;
    let objectType;
    switch(index) {
        case 0:
            objectType = `first-NFT-selection-button`;
            break;
        case 1:
            objectType = `second-NFT-selection-button`;
            break;
        case 2:
            objectType = `third-NFT-selection-button`;
            break;
        case 3:
            objectType = `forth-NFT-selection-button`;
            break;
    }
    // Report
    Report({
        baseInfo: {
            pageSource: pageSource.creatorPage,
        },
        params: {
            eventData: {
                businessType: businessType.buttonClick,
                objectType: objectType,
            }
        }
    })
}

const showCurrencyDialog = () => {
    currencyDialog.value = true;
}

const hideCurrencyDialog = () => {
    currencyDialog.value = false;
}

const selectCurrencyItem = (data: any) => {
    currencyItem.value = data;
    hideCurrencyDialog();
}

const uploadImg = (e: any) => {
    let file = e.target.files[0];
    // 清空file
    e.target.value = '';
    // upload 
    uploadFile(file).then(res => {
        // @ts-ignore
        uploadItem.value = res;
    })
}

const hideSuccess = () => {
    showSuccess.value = false;
}

const jumpList = () => {
    location.href = `/nft/list`
}

const changePrice = (e: any) => {
    projectPrice.value = String(Math.abs(e.target.value))
}

const feedback = () => {
    // Report
    Report({
        baseInfo: {
            pageSource: pageSource.creatorPage,
        },
        params: {
            eventData: {
                businessType: businessType.buttonClick,
                objectType: buttonType.feedback,
            }
        }
    })
}

watchEffect(() => {
    // 数量
    let num = projectSize.value;
    num = num.replace(/\D/g, '');
    if (Number(num) > maxSize.value) {
        num = String(maxSize.value);
    }
    if (num) {
        projectSize.value = String(Number(num));
    } else {
        projectSize.value = '';
    }
    
    // 编号
    if (projectSize.value !== '') {
        projectNo.value = String(1).padStart(String(projectSize.value).length, '0')
    } else {
        projectNo.value = ''
    }
    showNoStr.value = projectNo.value != '';

    // 是否可以创建
    let ifUpload = uploadItem.value && uploadItem.value.objectKey || false;
    let ifName = projectName.value !== '';
    let ifDesc = projectDesc.value !== '';
    let ifSize = projectSize.value !== '' && Number(projectSize.value) > 0;
    let ifCurrency = currencyItem.value && currencyItem.value.currencyCode;
    let ifPrice = projectPrice.value && projectPrice.value !== '' && Number(projectPrice.value) > 0;

    isNext.value = ifUpload && ifName && ifDesc && ifSize && ifCurrency && ifPrice;
})

onMounted(() => {
    // config
    getConfig()
    // Report
    Report({
        baseInfo: {
            pageSource: pageSource.creatorPage,
        },
        params: {
            eventData: {
                businessType: businessType.pageView,
            }
        }
    })
})
</script>


<style lang="less" scoped>
.center {
    overflow-y: auto;
    padding: 0 50px;
    width: 1100px;
    margin: 10px auto 0;
    box-sizing: border-box;
    height: calc(100% - 10px);
    background-color: #fff;
    border-radius: 20px 20px 0 0;
    .title {
        padding: 22px 0;
        font-size: 20px;
        font-weight: 600;
        line-height: 24px;
    }
    .content {
        width: 100%;
        clear: both;
        display: flex;
        justify-content: space-between;
        .name {
            height: 24px;
            font-size: 12px;
            color: #939393;
        }
        .showNo {
            color: #939393;
            margin-top: -10px;
            margin-bottom: 24px;
        }
        .l {
            width: 400px;
            .wait {
                padding: 200px 0;
                text-align: center;
                img {
                    opacity: .6;
                    animation: rotate 1s infinite linear;
                }
            }
            .show {
                position: relative;
                overflow: hidden;
                width: 400px;
                height: 400px;
                border-radius: 10px;
                .card {
                    position: absolute;
                    left: 53px;
                    top: 103px;
                    width: 294px;
                    height: 186px;
                    .logo {
                        position: absolute;
                        top: 50%;
                        left: 50%;
                        transform: translate(-50%, -50%);
                        width: 100px;
                        height: 100px;
                        border-radius: 50%;
                        background-color: #fff;
                        img {
                            width: 100%;
                            height: 100%;
                            border-radius: 50%;
                            object-fit: cover;
                        }
                    }
                    .member {
                        position: absolute;
                        top: 11px;
                        left: 11px;
                        width: 228px;
                        font-size: 12px;
                        text-align: left;
                        font-weight: 800;
                        line-height: 13px;
                    }
                    .number {
                        position: absolute;
                        top: 11px;
                        right: 10px;
                        font-size: 12px;
                        font-weight: 800;
                        line-height: 13px;
                        letter-spacing: 1px;
                    }
                    &.s1 {
                        .member, .number {
                            color: #ffffff;
                        }
                    }
                    &.s2 {
                        .member, .number {
                            color: #4AC3E1;
                        }
                    }
                    &.s3 {
                        .member, .number {
                            color: #606C94;
                        }
                    }
                    &.s4 {
                        .member, .number {
                            color: #504215;
                        }
                    }
                }
                .bg {
                    width: 100%;
                    height: 100%;
                }
            }
            .list {
                margin-top: 20px;
                display: flex;
                .item {
                    width: calc(100% / 4);
                    cursor: pointer;
                    overflow: hidden;
                    margin-right: 13px;
                    border-radius: 5px;
                    img {
                        width: 100%;
                        border-radius: 5px;
                    }
                    &:last-child {
                        margin: 0;
                    }
                    &.on {
                        position: relative;
                        &::before {
                            position: absolute;
                            display: block;
                            top: 0;
                            right: 0;
                            content: '';
                            width: 44px;
                            height: 44px;
                            background-size: 100%;
                            background-image: url('../../static/img/icon-add-select.svg');
                        }
                    }
                }
            }
        }
        .r {
            width: 520px;
            .face {
                position: relative;
                width: 80px;
                margin-bottom: 10px;
                .file {
                    position: absolute;
                    z-index: 2;
                    width: 100%;
                    height: 100%;
                    opacity: 0;
                    cursor: pointer;
                }
                .off {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    cursor: pointer;
                    width: 80px;
                    height: 80px;
                    border-radius: 100px;
                    border: 1px dashed #D0D0D0;
                    background: rgba(0, 0, 0, 0.02);
                }
                .on {
                    position: relative;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    cursor: pointer;
                    width: 80px;
                    height: 80px;
                    overflow: hidden;
                    border-radius: 100px;
                    background: #353535;
                    .img {
                        position: absolute;
                        opacity: .4;
                        width: 100%;
                        height: 100%;
                        border-radius: 100px;
                    }
                }
            }
            .desc {
                color: #B4B4B4;
                font-size: 12px;
                font-weight: 400;
                line-height: 16px;
                margin-bottom: 22px;
            }
            .input {
                display: flex;
                align-items: center;
                justify-content: center;
                height: 41px;
                margin-bottom: 22px;
                border-radius: 5px;
                background: #FFFFFF;
                border: 1px solid #E0E0E0;
                input {
                    border: 0;
                    outline: none;
                    padding: 3px 0;
                    color: #777;
                    font-size: 16px;
                    font-weight: 500;
                    letter-spacing: 0.3px;
                    width: calc(100% - 24px);
                    &::placeholder {
                        color: rgba(0, 0, 0, .3);
                    }
                }
            }
            .price {
                position: relative;
                display: flex;
                justify-content: space-between;
                .currency {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    flex-direction: row;
                    cursor: pointer;
                    height: 43px;
                    padding: 0 14px;
                    margin-right: 13px;
                    border-radius: 25px;
                    background: #F4F4F4;
                    .head {
                        overflow: hidden;
                        width: 20px;
                        height: 20px;
                        margin-right: 8px;
                        border-radius: 50%;
                        background-color: #999;
                    }
                    .font {
                        max-width: 250px;
                        font-size: 15px;
                        font-weight: 500;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                    }
                    .arrow {
                        margin-left: 8px
                    }
                }
                .no-select {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    flex-direction: row;
                    cursor: pointer;
                    height: 43px;
                    padding: 0 14px;
                    margin-right: 13px;
                    border-radius: 25px;
                    background: #1d9bf0;
                    .font {
                        max-width: 250px;
                        color: #fff;
                        font-size: 15px;
                        font-weight: 500;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                    }
                    .arrow {
                        margin-left: 8px
                    }
                }
                .input {
                    flex: 1;
                    margin-bottom: 0;
                }

                .currency-pop {
                    position: absolute;
                    overflow: hidden;
                    z-index: 3;
                    left: 0;
                    bottom: 50px;
                    width: 375px;
                    max-height: 420px;
                    border-radius: 20px;
                    background-color: #fff;
                    box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.25);
                }
            }
            .explain {
                color: #B4B4B4;
                font-size: 12px;
                font-weight: 400;
                line-height: 18px;
                ul {
                    padding: 0;
                    margin-left: 14px;
                }
                .special {
                    color: #E29015;
                }
            }
            .footer {
                height: 80px;
            }
        }
    }
    .create {
        position: fixed;
        left: 50%;
        bottom: 0;
        width: 1100px;
        text-align: right;
        padding: 15px 30px;
        transform: translateX(-50%);
        box-sizing: border-box;
        background-color: #fff;
        border-top: solid 1px #ECECEC;
        .on {
            border: 0;
            height: 50px;
            color: #FFFFFF;
            cursor: pointer;
            padding: 0 50px;
            font-size: 18px;
            font-weight: 700;
            letter-spacing: 0.3px;
            border-radius: 25px;
            background: #1D9BF0;
        }
        .off {
            border: 0;
            height: 50px;
            color: #FFFFFF;
            cursor: pointer;
            padding: 0 50px;
            font-size: 18px;
            font-weight: 700;
            letter-spacing: 0.3px;
            border-radius: 25px;
            background: #E4E4E4;
        }
    }
}

.feedBack {
    position: absolute;
    right: 44px;
    bottom: 88px;
    cursor: pointer;
    font-size: 12px;
    text-align: center;
    a:link, a:visited {
        color: #A8A8A8;
        text-decoration: none;
    }
    .mail {
        display: flex;
        align-items: center;
        justify-content: center;
        margin: auto;
        width: 50px;
        height: 50px;
        margin-bottom: 10px;
        border-radius: 50%;
        background-color: #fff;
    }
    .font {
        opacity: 0.7;
        color: #A8A8A8;
        letter-spacing: 0.3px;
    }
}

.loading {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, .8);
    img {
        width: 100px;
        animation: rotate 1s linear infinite;
    }
}

@keyframes rotate {
    0% {
        transform: rotate(0);
    }
    100% {
        transform: rotate(360deg);
    }
}

.succ {
    position: absolute;
    z-index: 2;
    top: 50%;
    left: 50%;
    width: 500px;
    height: 400px;
    border-radius: 20px;
    background-color: #fff;
    transform: translate(-50%, -50%);
    .icon {
        display: block;
        width: 120px;
        height: 120px;
        margin: 78px auto 27px auto;
    }
    .notic {
        font-size: 20px;
        font-weight: 700;
        text-align: center;
        line-height: 24px;
    }
    .btn {
        display: block;
        border: 0;
        cursor: pointer;
        width: 440px;
        height: 50px;
        color: #fff;
        font-size: 18px;
        font-weight: 700;
        margin: 70px auto 0;
        border-radius: 50px;
        background: #1D9BF0;
    }
}
.succ-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, .8);
}
.mask-bg {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    width: 100%;
    height: 100%;
}
</style>