<script>
import {
    callFn, getBackData, initEvent, CountDown, getCookie, USER_DATA_TYPE
} from 'nimble-lib';
import LoginTmp from './LoginTmp';
import attrSync from '../../../../Mixins/attrSync';

export default {
    extends: LoginTmp,
    props: {
        /**
         * 手机号
         */
        username: {
            type: [String, Number]
        },
        btnTxt: {
            type: String
        },
        // 倒计时suffix文案
        countDownTxt: {
            default: '秒后重发'
        },
        // 倒计时时间(毫秒)
        timeCount: {
            type: Number,
            default: 60 * 1000
        },
        codeBtnTxt: {
            type: String,
            default: '获取验证码'
        },
        /**
         * 登录模式
         */
        pattern: {

        }
    },
    data () {
        return {
            myCodeBtnTxt: this.codeBtnTxt,
            lock: false, // 登录按钮是否可以点击
            lockCode: false, // 获取验证码按钮是否锁定
            readonly: false,
            myUsername: this.username,
            code: null,
            password: null,
            myCopyrightChecked: null
        };
    },
    mixins: [
        /**
         * 选中协议
         */
        attrSync('copyrightChecked')
    ],
    computed: {
        validPhone() {
            return callFn(this.validData, ['vPhone', this.myUsername]);
        },
        validCode() {
            return callFn(this.validData, ['vCode', this.code]);
        },
        validCopyright() {
            return callFn(this.validData, ['vCopyright', this.myCopyrightChecked]);
        },
        submitState() {
            return !this.lock && !this.validPhone && (!this.validCode || this.password) && !this.validCopyright;
        },
        getCodeState() {
            return !this.lock && !this.validPhone && !this.lockCode;
        }
    },
    methods: {
        getCode(evt, cb) {
            const _that = this;
            const _validPhone = _that.validPhone;
            if (!evt) {
                evt = initEvent('click');
            }
            evt._contentType = 'getCode';
            _that.$emit('click', {
                Event: evt
            });
            if (!_validPhone) {
                _that.lock = true;
                getBackData(_that.getUserData, [USER_DATA_TYPE.GET_IDENTITY_CODE, {
                    username: _that.myUsername
                }]).then((res) => {
                    _that.lock = false;
                    if (res) {
                        _that.handleCountDown();
                        const _evt = initEvent('after');
                        _evt._contentType = 'getCode';
                        _that.$emit('after', {
                            Event: _evt,
                            data: res
                        });
                    }
                    callFn(cb, [res], _that);
                    // _that.setCode(res);
                }, () => {
                    _that.lock = false;
                });
            } else {
                getBackData(_that.getUserData, ['message', this.validPhone, {
                    type: 'toast'
                }]);
            }
        },
        /**
         * 处理倒计时
         * @param {Function} setTxtCb 设置文案方法
         * @param {Function} finishCb 结束回调
         */
        handleCountDown () {
            const _that = this;
            _that.countDown && _that.countDown.stop();
            const suffix = _that.countDownTxt;
            _that.lockCode = true;
            _that.countDown = CountDown({
                timeCount: _that.timeCount,
                formatType: 's',
                callback(data) {
                    if (data && typeof data === 'string') {
                        _that.myCodeBtnTxt = data + suffix;
                    } else {
                        _that.lockCode = false;
                        _that.myCodeBtnTxt = _that.codeBtnTxt;
                    }
                }
            });
        },
        /**
         * 验证并登录
         * @param {Event} evt 事件对象
         * @param {Boolean} isSmsCode 是否使用短信验证吗登录
         */
        checkAndlogin (evt, isSmsCode) {
            const _that = this;
            evt._contentType = 'submit';
            _that.$emit('submit', {
                Event: evt
            });
            const _params = {
                username: _that.myUsername,
                code: _that.code,
                password: _that.password,
                pattern: _that.pattern
            };
            if (isSmsCode) {
                let sourcechannel = getCookie('sourcechannel');
                if (sourcechannel) {
                    const arr = sourcechannel.split('_');
                    if (arr && arr.length > 0) {
                        sourcechannel = arr[0];
                    }
                } else {
                    sourcechannel = 'kH5';
                }
                _params.sourcechannel = sourcechannel;
            }
            if (_that.submitState) {
                _that.lock = true;
                getBackData(_that.getUserData, [USER_DATA_TYPE.AUTH_LOGIN_BINGDING, _params]).then((res) => {
                    _that.lock = false;
                    const _evt = initEvent('confirm');
                    _evt._contentType = 'confirm';
                    _that.$emit('confirm', {
                        Event: _evt,
                        data: res
                    });
                }, () => {
                    _that.lock = false;
                });
            } else {
                getBackData(_that.getUserData, ['message', this.validPhone || this.validCode || this.validCopyright, {
                    type: 'toast'
                }]);
            }
        }
    }
};
</script>
