import lodash from "lodash";
import moment from "moment";
import '@/modules/mgt/components/eventlisteners.js';

const filter_mixin = {
    filters: {
        _yyyy_mm_dd(val) {
            if (lodash.isNil(val) || lodash.trim(val) == '') return '';
            const orgin = val;
            if (orgin.length < 8) {
                return orgin;
            }
            if (!Number(orgin)) return orgin;
            const date = orgin.substring(0, 8);
            if (moment(date).isValid()) {
                return moment(date).format('YYYY[-]MM[-]DD');
            }
            return orgin;
        }
    },
}

const utils_mixin = {
    methods: {
        /** * 이메일 형식 체크 * * @param 데이터 */
        emailCheck(email, rtnArrYn) {
            if (this.isNull(rtnArrYn)) {
                rtnArrYn = 'N';
            }
            // var regExp = /(^[A-Za-z0-9_\.\-]+)@([A-Za-z0-9\-]+\.[A-Za-z0-9\-]+)/;
            var regExp = /^([0-9a-zA-Z_\.\-]([-_.]?[0-9a-zA-Z_\.\-])*)@([0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*.[a-zA-Z]{2,3}$)/i;//이메일 정규식

            if (regExp.test(email) == false) {
                // 이메일 형식이 알파벳+숫자@알파벳+숫자.알파벳+숫자 형식이 아닐경우
                if (rtnArrYn == 'Y') {
                    return email;
                }
                return false;
            } else {
                var myArray = regExp.exec(email);
                if (rtnArrYn == 'Y') {
                    return myArray;
                }
                return true;
            }

        },
        /** * 전화번호 포맷으로 변환 * * @param 데이터 */
        formatPhone(phoneNum, fmt, rtnArrYn) {
            if (this.isNull(fmt)) {
                fmt = '';
            }
            if (this.isNull(rtnArrYn)) {
                fmt = 'N';
            }
            if (this.isPhone(phoneNum)) {
                var rtnNum;
                var regExp = /(02)([0-9]{3,4})([0-9]{4})$/;
                var myArray;
                if (regExp.test(phoneNum)) {
                    myArray = regExp.exec(phoneNum);
                    rtnNum = myArray[1] + fmt + myArray[2] + fmt + myArray[3];
                    if (rtnArrYn == 'Y') {
                        return myArray;
                    }
                    return rtnNum;
                } else {
                    regExp = /(0[3-9]{1}[0-9]{1})([0-9]{3,4})([0-9]{4})$/;
                    if (regExp.test(phoneNum)) {
                        myArray = regExp.exec(phoneNum);
                        rtnNum = myArray[1] + fmt + myArray[2] + fmt + myArray[3];
                        if (rtnArrYn == 'Y') {
                            return myArray;
                        }
                        return rtnNum;
                    } else {
                        return phoneNum;
                    }
                }
            } else {
                return phoneNum;
            }
        },
        /** * 핸드폰번호 포맷으로 변환 * * @param 데이터 */
        formatMobile(phoneNum, fmt, rtnArrYn) {

            if (this.isNull(fmt)) {
                fmt = '';
            }
            if (this.isNull(rtnArrYn)) {
                fmt = 'N';
            }
            if (this.isMobile(phoneNum)) {

                var rtnNum;
                var regExp = /(01[016789])([0-9]{3,4})([0-9]{4})$/;
                var myArray;

                if (regExp.test(phoneNum)) {

                    myArray = regExp.exec(phoneNum);
                    rtnNum = myArray[1] + fmt + myArray[2] + fmt + myArray[3];
                    if (rtnArrYn == 'Y') {
                        return myArray;
                    }
                    return rtnNum;
                } else {
                    return phoneNum;
                }
            } else {
                return phoneNum;
            }
        },

        /** * 전화번호 형식 체크 * * @param 데이터 */
        isPhone(phoneNum) {
            var regExp = /(02)([0-9]{3,4})([0-9]{4})$/;
            if (regExp.test(phoneNum)) {
                return true;
            } else {
                regExp = /(0[3-9]{1}[0-9]{1})([0-9]{3,4})([0-9]{4})$/;
                if (regExp.test(phoneNum)) {
                    return true;
                } else {
                    return false;
                }
            }
        },

        /** * 핸드폰번호 형식 체크 * * @param 데이터 */
        isMobile(phoneNum) {
            var regExp = /(01[016789])([0-9]{3,4})([0-9]{4})$/;
            var myArray;
            if (regExp.test(phoneNum)) {
                myArray = regExp.exec(phoneNum);
                return true;
            } else {
                return false;
            }
        },
        isMobile2(phoneNum) {
            var regExp = /(1[016789])([0-9]{3,4})([0-9]{4})$/;
            var myArray;
            if (regExp.test(phoneNum)) {
                myArray = regExp.exec(phoneNum);
                return true;
            } else {
                return false;
            }
        },

        isNull(obj) {
            if (lodash.isNil(obj) || lodash.trim(obj) == '') {
                return true;
            }
            return false;
        },
        getParent(name) {
            let p = this.$parent;
            while (typeof p !== 'undefined') {
                if (p.$options.name == name) {
                    return p;
                } else {
                    p = p.$parent;
                }
            }
            return false;
        },
        getJsonObj(str) {
            return JSON.parse(JSON.stringify(str));
        },
    }
};

var chkPattern2 = {
    data: function () {
        return {}
    },
    methods: {
        selSesStorage(keyLike) {
            if (this.isNull(keyLike)) {
                return null;
            }
            if (sessionStorage.length > 0) {
                let keyList = [];
                for (let i = 0; i < sessionStorage.length; i++) {
                    const keyNm = sessionStorage.key(i);
                    if (keyNm.indexOf(keyLike) > -1) {
                        keyList.push({name: keyNm, value: sessionStorage.getItem(keyNm)});
                    }
                }
                if (keyList.length > 0) {
                    return keyList;
                }
                return null;
            }
            return null;
        },

        delSesStorage(keyList) {
            if (this.isNull(keyList)) {
                return null;
            }
            if (keyList.length > 0) {
                keyList.map((o) => (sessionStorage.removeItem(o.name)));
                return true;
            }
        },

        setGridMouseDownActive() {
            const ele = document.querySelector(`div.tui-grid-container.tui-grid-show-lside-area`);
            if (window.getEventListeners(ele).mousedown) {
                ele.removeEventListener('mousedown', window.getEventListeners(ele).mousedown[0].listener);
            }
        },
        restrictChars: function ($event, regExp, hanYn) {

            if (this.isNull(hanYn)) {
                hanYn = 'N';
            }
            if (hanYn === 'N' && $event.type === 'keydown') {
                if ($event.keyCode === 229) {
                    $event.preventDefault();
                    return false;
                }
            }

            if ($event.type === 'keypress') {
                //한글 처리 불가
                if (regExp.test(String.fromCharCode($event.charCode))) {
                    return true;
                } else {
                    $event.preventDefault();
                    return false;
                }
            }

            if (hanYn === 'N' && ($event.type === 'keyup' || $event.type === 'input' || $event.type === 'change' || $event.type === 'blur')) {
                $event.target.value = $event.target.value.replace(/[ㄱ-ㅎㅏ-ㅣ가-힣]/g, '');
                $event.preventDefault();
                return false;
            }
            return true;
        },
        setLenth: function (e, len) {
            this.cut(e, len);
        },
        onlyCustom: function (e, strRegExp, hanYn) {
            var regExp_g = new RegExp(strRegExp, 'g');
            this.cut(e);
            return this.restrictChars(e, regExp_g, hanYn);
        },
        onlyCommon: function (strRegExp, e, len, isEventCall, hanYn) {
            var regExp_g = new RegExp(strRegExp, 'g');
            if (isEventCall === 'N') {
                if (!this.cut(e, len, isEventCall)) {
                    return false;
                }
                if (!regExp_g.test(e.value)) {
                    return false;
                }
                return true;
            }
            this.cut(e, len);
            return this.restrictChars(e, regExp_g, hanYn);
        },
        onlyNum: function (e, len, isEventCall) {
            var strRegExp = '^[0-9]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall);
        },
        onlyEng: function (e, len, isEventCall) {
            var strRegExp = '^[A-Za-z]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall);
        },
        onlyLowerEng: function (e, len, isEventCall) {
            var strRegExp = '^[a-z]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall);
        },
        onlyUpperEng: function (e, len, isEventCall) {
            var strRegExp = '^[A-Z]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall);
        },
        onlyEmail: function (e, len, isEventCall) {
            var strRegExp = '^[a-zA-Z0-9_\.\-@._-]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall);
        },
        onlyName: function (e, len, isEventCall) {
            var strRegExp = '^[ㄱ-ㅎㅏ-ㅣ가-힣a-zA-Z]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall, 'Y');
        },
        onlyTitle: function (e, len, isEventCall) {
            var strRegExp = '^[ㄱ-ㅎㅏ-ㅣ가-힣a-zA-Z0-9]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall, 'Y');
        },
        onlyText: function (e, len, isEventCall) {
            var strRegExp = '^[ㄱ-ㅎㅏ-ㅣ가-힣a-zA-Z0-9_-]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall, 'Y');
        },
        onlyPassword: function (e, len, isEventCall) {
            var strRegExp = '^[A-Za-z0-9!@#$%^&*]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall);
        },
        onlyId: function (e, len, isEventCall) {
            var strRegExp = '^[A-Za-z0-9_\.\-]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall);
        },
        onlyIp: function (e, len, isEventCall) {
            var strRegExp = '^[0-9,.*]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall);
        },
        onlyRoleNm_Space: function (e, len, isEventCall) {
            var strRegExp = '^[ㄱ-ㅎㅏ-ㅣ가-힣a-zA-Z0-9]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall, 'Y');
        },
        onlyRoleId_UnderBar: function (e, len, isEventCall) {
            var strRegExp = '^[a-zA-Z0-9_]*$';
            return this.onlyCommon(strRegExp, e, len, isEventCall);
        },
        cut: function (ele, len, isValidChk) {
            let e = ele;
            if (typeof ele.target != "undefined") {
                e = ele.target;
            }
            let max = this.isNull(len) ? e.attributes.maxlength.value : len;
            let str = e.value;

            if (this.bytes(str) > max) {
                if (this.isNull(isValidChk)) {
                    e.value = this.cutBytes(str, max);
                }
                return false;
            }
            return true;
        },
        cutBytes: function (str, len) {
            while (1 === 1) {
                if (this.bytes(str) <= len) {
                    return str;
                }
                str = str.slice(0, -1);
            }
        },
        bytes: function (str) {
            var length = ((s, b, i, c) => {
                // for(b=i=0;c=s.charCodeAt(i++);b+=c>>11?3:c>>7?2:1);  // 한글 3바이트
                // for(b=i=0;c=s.charCodeAt(i++);b+=c>>11?2:c>>7?1:1);     //한글 2바이트
                b = 0, i = 0;
                while (1 === 1) {
                    c = s.charCodeAt(i++);
                    if (isNaN(c)) {
                        break;
                    }
                    b += c >> 11 ? 2 : c >> 7 ? 1 : 1;
                }
                return b
            })(str);
            return length;
        },
        checkPhone: function (str) {
            str = str.replace(/[-\s]+/g, '');
            if (str.charAt(0) != "0") {
                str = "0" + str;
            }
            if (str.length < 10 || str.length > 12) {
                return "";
            }
            if (isNaN(str)) {
                return "";
            }
            if (str.substr(0, 2) != "01" && str.substr(0, 3) != "070" && str.substr(0, 4) != "0505" && str.substr(0, 4) != "0503") {
                return "";
            }
            return str;

        },
    }
};
export {utils_mixin, chkPattern2, filter_mixin};