<template>
<el-dialog v-model="dialogVisible" width="637" class="dialog-box" v-if="data"
    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('home.leverageChange') }}</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 style="display: flex;width: 100%;">
            <div class="account">{{ t('home.account') + ': ' + data.mt4_login }}</div>
            <div class="account">{{ t('home.oldLeverage') + ': ' }}<span style="color: #29bbe4;">{{ data.mt4_leverage_old }}</span></div>
        </div>
        <el-form ref="postRef" :model="form" :rules="rules" autocapitalize="off" size="large" label-position="top" style="user-select: none;" >
            <el-form-item :label="t('home.newLeverage')" prop="mt4_leverage_new" >
                <el-select
                    v-model="form.mt4_leverage_new"
                    :placeholder="t('common.selectChoose')"
                    :clearable="false"
                    style="width: 100%;"
                    >
                    <el-option v-for="(item, index) in leverageList" :disabled="data.mt4_leverage_old == item.leverage_id" :key="index" :label="item.leverage_id" :value="item.leverage_id" ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item :label="t('home.remark')" prop="apply_opinion" >
                <el-input v-model="form.apply_opinion" />
            </el-form-item>
            <el-form-item :label="t('home.pwd')" prop="safe_pass" >
                <el-input type="password" v-model="form.safe_pass" show-password />
            </el-form-item>
            <el-form-item >
                <el-button @click="save" style="width: 100%;" type="primary">{{ t('form.apply') }}</el-button>
            </el-form-item>
        </el-form>
    </div>
</el-dialog>

</template>

<script setup>
import { useLocale } from 'element-plus'
import { ref, defineModel, watch } from 'vue';
import { serverLang } from '@/utils/i18n'
import { editMtLeverage, queryApplyLeverageList } from '@/api/account'

const { proxy } = getCurrentInstance();
const { t } = useLocale()
const dialogVisible = defineModel("show")
const emit = defineEmits(['change'])
const props = defineProps({
    data: {
        type: Object,
        default: null
    }
})

const loading = ref(false)

const d = reactive({
  form: {
    mt4_leverage_new: undefined,
    safe_pass: undefined,
    apply_opinion: undefined
  },
  rules: {
    mt4_leverage_new: [{ required: true, message: "", trigger: "blur" }],
    safe_pass: [{ required: true, message: "", trigger: "blur" }],
  }
});

const { form, rules } = toRefs(d);

const leverageList = ref([])

function loadLeverageList() {
    const { mt4_server, mt4_login } = props.data
    queryApplyLeverageList({
        mt_server: mt4_server,
        mt_login: mt4_login
    }).then(resp => {
        if (resp.data) {
            leverageList.value = resp.data
        }
    })
}

function save() {
    proxy.$refs["postRef"].validate(valid => {
        if (valid) {
            loading.value = true
            const { mt4_server, mt4_login, mt4_leverage_old } = props.data
            const { safe_pass, mt4_leverage_new, apply_opinion } = form.value
            editMtLeverage({
                mt4_server, mt4_login, mt4_leverage_old, mt4_leverage_new, apply_opinion, safe_pass,
                qcc_language: serverLang()
            }).then(resp => {
                loading.value = false
                dialogVisible.value = false
                window.showTip({
                    title: t('common.success.action'),
                    status: 'success'
                })
                // emit('change', {})
            })
        }
    })
}

watch(() => props.data, (n, o)  => {
    if (n) {
        loadLeverageList()
    }
}, {deep: true})

watch(dialogVisible, (n, o) => {
    if(!n) {
        proxy.resetForm("postRef");
    }
})
</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 {
            font-size: 26px;
            font-weight: 700;
        }

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

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

        .account {
            font-size: 18px;
            font-weight: 700;
            color: rgba(51, 51, 51, 1);
            margin-bottom: 14px;
            width: 50%;
        }
    }
}
</style>