<?php
include_once('./_common.php');

$navigaterArr[] = "SUU";
$navigaterArr[] = "로또번호 생서기";
include_once(G5_PATH.'/_head.php');

function numMaker() {
    $loArr = array("4", "7", "8", "12", "18", "19", "22", "25", "35", "36", "38", "40", "43", "45");
    $a = array_rand($loArr, 6);

    $nums = "";
    foreach($a as $k=>$v) {
        $nums .= "<li class='mn'>".$loArr[$v]."</li>";
    }

    return $nums;
}
?>

<style>
#carinfo_basic {
    width:100%;
    max-width:1300px;
    margin:50px auto 0px auto;
    text-align: center;
    -ms-user-select: none;
    -moz-user-select: -moz-none;
    -khtml-user-select: none;
    -webkit-user-select: none;
    user-select: none;
    padding: 50px 0 50px 0;
}
#carinfo_basic select { border-radius: 5px; width: 48%; height: 40px; padding: 0px 10px; font-size: 15px; font-weight: bold; }
#carinfo_basic button { width: 100%; height: 40px; font-size: 15px; border: 0px; background: #0176B7; color: #fff; border-radius: 5px; margin-top: 10px; }
#carinfo_basic table caption { line-height: 180%; font-size: 13px; }
#carinfo_basic .numWrap {  width: 360px; margin: 20px auto; text-align: center;}
#carinfo_basic .numWrap > li { display: inline-block;  text-align: center; width: 46px; height: 46px; font-size: 17px; line-height: 46px; font-weight: bold; margin: 3px;}
#carinfo_basic .nums { border: 1px solid #555; border-radius: 50%; cursor: pointer;  }
#carinfo_basic .nums:hover { background: orangered; color: #eee; }
#carinfo_basic .active { background: #1EA362 !important; color: #fff; }

#btnWrap { max-width: 360px; margin: 0px auto; }
#nos { width: 360px; margin: 30px auto; }
#nos > ul { width: 100%; margin-bottom: 10px;  }
#nos > ul:hover { background: #cbcbcb; border-radius: 5px; }
#nos > ul > li { display: inline-block; text-align: center; border: 0px solid #555; border-radius: 50%; width: 46px; height: 46px; line-height: 46px; font-size: 17px; font-weight: bold; cursor: pointer; margin: 7px 7px; }
#nos .ns1 { background: #FF6F28; color: #fff; }
#nos .ns2 { background: #4444FF; color: #fff; }
#nos .ns3 { background: #FF6060; color: #fff; }
#nos .ns4 { background: #3E3E3E; color: #fff; }
#nos .ns5 { background: #00BB00; color: #fff; }
</style>

<script>
$(document).ready(function() {
    $('.nums').click(function() {

        var cls = $(this).attr('class');
        var rst = cls.match(/active/);

        if(rst) {
            $(this).removeClass('active');
            $(this).addClass('none');
        }
        else {
            $(this).addClass('active');
            $(this).removeClass('none');
        }

    });

    $('#createNumBtn').click(function() {

        var type = $('#type').val();
        var cnt = $('#cnt').val();
        var ids = []; // 선택 번호 배열
        var dupli = []; // 중복체크위한 배열
        var rst = []; // 생성된 번호
        var txtNo = 0; // 생선된 번호의 TEXT
        var dupliChk = 0; // 중복 횟수
        var cls = "";

        $('#nos').empty();

        switch(type) {
            case "1" : // 선택한 번호로
                ids = $(".active").map(function() {
                    return $(this).attr('data-no');
                }).get();
                break;
            case "2" : // 미선택 번호로
                ids = $(".none").map(function() {
                    return $(this).attr('data-no');
                }).get();
                break;
            case "3" : // 모든 번호
                ids = $(".nums").map(function() {
                    return $(this).attr('data-no');
                }).get();
                break;
        }

        if(type==1 && ids.length < 6) {
            alert('[1001] 번호를 6개 이상 선택해 주세요.');
            return false;
        }

        if(type==2 && ids.length < 6) {
            alert('[1001] 선택된 번호가 너무 많습니다.\n미선택 번호가 6개 이상이 되어야 합니다.');
            return false;
        }

        for(var i=0; i < cnt; i++) {

            rst = randomNums(ids, 6);
            txtNo = rst.join('|');

            if (dupli.indexOf(txtNo) === -1) {

                dupli.push(txtNo);

                var v = '<ul class="lottoNos">';
                $.each(rst, function(x, data) {
                    //console.log(data);

                    if(data <= 10) {
                        cls = 'ns1';
                    }

                    if(data >= 11 && data <= 20) {
                        cls = 'ns2';
                    }

                    if(data >= 21 && data <= 30) {
                        cls = 'ns3';
                    }

                    if(data >= 31 && data <= 40) {
                        cls = 'ns4';
                    }

                    if(data >= 41 && data <= 45) {
                        cls = 'ns5';
                    }

                    v += '<li class="'+cls+'">'+data+'</li>';
                });
                v += '</ul>';
                $('#nos').append(v);

            }
            else { // 배열에 동일한 번호가 있는 경우 반복횟수를 -1

                // console.log('[ '+i+' ] 번째 중복')
                i--;
                dupliChk ++;

            }

            // 같은 번호가 연속 10번 중복이면 작업을 중지한다.
            if(dupliChk >= 10) {
                alert('더 이상 번호를 생성할 수 없습니다.');
                break;
            }

        }

    });
});


/**
 * 배열값중 특정 갯수만큼 랜덤으로 뽑기
 */
function randomNums(arr, selectingNumber) {

    let dupliNum = []; // 중복방지 체크를 위한 배열
    let lottoNum = []; // 생성된 로또 번호를 담을 배열
    for (i=0; i < selectingNumber; i++) {

        randomNum = Math.floor(Math.random() * arr.length)
        if (dupliNum.indexOf(randomNum) === -1) {
            dupliNum.push(randomNum)
            lottoNum.push(arr[randomNum])
        }
        else { // 배열에 동일한 번호가 있는 경우 반복횟수를 -1
            i--;
        }
    }

    // 배열 오름차순으로 정렬
    lottoNum.sort(function(a, b) {
        return a - b;
    });

   return lottoNum
}
</script>

<div id="carinfo_basic">
    <ul class="numWrap">
        <?php for($i=1; $i <= 45; $i++) { ?>
        <li class="nums none" data-no="<?php echo $i; ?>"><?php echo $i; ?></li>
        <?php } ?>
        <li>&nbsp;</li>
        <li>&nbsp;</li>
        <li>&nbsp;</li>
    </ul>

    <div id="btnWrap">
        <select name="type" id="type" class="type">
            <option value="1">선택한 번호로</option>
            <option value="2">미선택 번호로</option>
            <option value="3">모든 번호로</option>
        </select>
        <select name="cnt" id="cnt" class="cnt">
            <?php for($i=5; $i <= 50; $i++) { ?>
            <option value="<?php echo $i; ?>">로또번호 <?php echo $i; ?>개 생성</option>
            <?php } ?>
        </select>
        <button type="button" id="createNumBtn">번호생성</button>
    </div>

    <div id='nos'></div>
</div>

<?php
include_once(G5_PATH.'/_tail.php');
