<template>
    <div :class="curClassName" :style="curStyle">
        <auth-puzzle-vcode :show="show" @success="onSuccess" @fail="onFail" />
        <i-button :type="type" @click="executeOpen">验证<Icon v-show="showIcon" type="md-checkmark" /></i-button>
    </div>
</template>

<script lang="ts">
import { Vue, Component, Prop } from "vue-property-decorator";
import AuthPuzzleVcode from "./vue-puzzle-code/vue-puzzle-code.vue";
@Component({
    components: {
        "auth-puzzle-vcode": AuthPuzzleVcode,
    },
})
export default class AppLoginCaptcha extends Vue {
    /**
     * 模型
     *
     * @type {string}
     * @memberof AppLoginCaptcha
     */
    @Prop() public layoutModelDetails: any;

    /**
     * 值
     *
     * @type {string}
     * @memberof AppLoginCaptcha
     */
    @Prop() public value!: string;

    /**
     * 名称
     *
     * @type {string}
     * @memberof AppLoginCaptcha
     */
    @Prop() public name!: string;

    /**
     * 下标
     *
     * @type {number}
     * @memberof AppLoginCaptcha
     */
    @Prop() public index?: number;

    /**
     * 是否显示
     */
    public show: boolean = false;

    /**
     * 按钮类型
     */
    public type: string = 'default';

    /**
     * 按钮类型
     */
    public showIcon: boolean = false;

    /**
     * 项名称
     *
     * @type {*}
     * @memberof AppLoginCaptcha
     */
    get itemName() {
        return (this.index || this.index === 0) ? `${this.name}_${this.index}` : this.name;
    }

    /**
     * 当前容器样式
     * 
     * @memberof AppLoginCaptcha
     */
    get curStyle() {
        const layoutModel = this.layoutModelDetails[this.itemName];
        if (layoutModel) {
            return layoutModel.getElementStyle();
        }
    }

    /**
     * 类名
     *
     * @memberof AppLoginCaptcha
     */
    get curClassName() {
        const layoutModel = this.layoutModelDetails[this.itemName];
        if (layoutModel) {
            return `app-login-captcha ${this.itemName} ${layoutModel.sysCss}`;
        }
    }

    /**
     * 初始化
     */
    public created() {
        if (this.value) {
            this.type = 'success';
        }
    }

    /**
     * 打开
     */
    public executeOpen() {
        this.show = true;
    }

    /**
     * 成功
     */
    public onSuccess() {
        this.show = false;
        this.type = 'success';
        this.showIcon = true;
        this.$emit("valueChange", { name: this.name, value: true });
    }

    /**
     * 失败
     */
    public onFail() {
        this.showIcon = false;
        this.$emit("valueChange", { name: this.name, value: false });
    }
}
</script>

<style lang="less">
.app-login-captcha {
    height: 40px;
    width: 100%;

    .ivu-btn {
        width: 100%;
        height: 100%;

        &:hover {
            border: 1px solid #dcdee2;
            color: #515a6e;
        }
        .ivu-icon {
            margin-left: 8px;
        }
    }
}
</style>
