<?php
include_once(_DIR_TO_CLASS.'/c_page.php');
include_once(_DIR_TO_MODULES.'/m_skd.php');

include_once('c_post_shift_poll.php');

class c_page_cab_post_shift_poll_alternative_analytics extends c_page_cab_post_shift_poll{

    public $h1 = 'Альтернативная аналитика';

    function set_head_tags(){
        parent::set_head_tags();
        $this->HEAD['css'][] = _PATH_TO_EXT . '/post_shift_poll/lib/style.css';
        $this->HEAD['js'][] = _PATH_TO_EXT . '/post_shift_poll/lib/script.js';
        $this->HEAD['js'][]=_PATH_TO_EXT.'/post_shift_poll/lib/loader.js';
    }


    function block_main(){

        $this->page_title();

        $_org_name_orijin = execSQL('select _org_name, _org_id from view_personals  group by _org_name'); // Должности и количство людей на них

        if($_GET['_org_name']){
            $_work_names = execSQL('select _otdel_name, _otdel_id from view_personals WHERE _org_id="'.$_org_name_orijin[$_GET['_org_name']]['_org_id'].'"'); // Должности и количство людей на них
            $wk_n = array_values($_work_names);
        }

        $_org_name = array_values($_org_name_orijin);



        ?>
        <div class="card_container mb-0">
            <form>


                <style>
                    .bg-tr-2{
                        background: #ffffff !important;
                    }

                    .bg-tr-2:hover{
                        background: #3cabac !important;
                    }
                    .bg-tr-1{
                        background: #f3f3f3;
                    }

                    .title_tab_table > td{
                        padding: 6px 15px !important;
                        cursor: pointer;
                        text-align: left;
                    }

                    .sort_ic_a{
                        text-align: center;
                        border-radius: 3px;
                        color: #00a1a5;
                        float: right;
                    }

                    .more_info{
                        position: fixed;
                        display: none;
                        pointer-events: none;
                        top: 0px;
                        left: 0px;
                        z-index: 1000;
                        background: rgb(35 32 48 / 50%);
                        width: 100%;
                        height: 100%;
                    }

                    .in_card_title{
                        font-size: 13px;
                        font-weight: 600;
                    }

                    .sel_in{
                        width: 333px;
                        margin-top: 10px;
                        margin-bottom: 15px;
                        padding: 11px;
                        border-radius: 6px;
                        border: 1px solid #eee;
                        font-size: 13px;
                    }

                    .d-inter{
                        display: inline-block;
                        margin-right: 15px;
                    }

                    .bt_s_3 {
                        display: inline-block;
                        background: #2da5da;
                        border: 0px;
                        font-size: 12px;
                        font-weight: 600;
                        color: #fff;
                        padding: 10px 31px !important;
                        height: 40px;
                        border-radius: 7px;
                        transition: .3s;
                    }

                    .sel_in-c{
                        padding: 10px !important;
                        border-radius: 5px !important;
                        border: 1px solid #eeeeee !important;
                    }


                    /* list check */


                    .multiselect {
                        width: 200px;
                    }
                    .selectBox {
                        position: relative;
                    }
                    .selectBox select {
                        width: 100%;
                        font-weight: bold;
                    }
                    .overSelect {
                        position: absolute;
                        left: 0;
                        right: 0;
                        top: 0;
                        bottom: 0;
                    }
                    #checkboxes {
                        display: none;
                        border: 1px #dadada solid;
                    }
                    #checkboxes label {
                        display: block;
                    }
                    #checkboxes label:hover {
                        background-color: #1e90ff;
                    }

                    .srd{
                        background: #e4ff0059;
                        color: #000;
                        font-weight: 900;
                        width: 20px;
                        height: 20px;
                        overflow: hidden;
                        font-size: 10px;
                        border-radius: 16px;
                        text-align: center;
                        cursor: help;
                        vertical-align: middle;
                        display: inherit;
                    }

                    .none_text{

                    }
                </style>


                <script>
                    var expanded = false;

                    function showCheckboxes() {
                        var checkboxes = document.getElementById("checkboxes");
                        if (!expanded) {
                            checkboxes.style.display = "block";
                            expanded = true;
                        } else {
                            checkboxes.style.display = "none";
                            expanded = false;
                        }
                    }
                </script>


                <div class="d-inter">
                    <div class="in_card_title">Организация:</div>
                    <input type="text" class="form-control autocomplete sel_in sel_in-c" autocomplete="off" list="_org_name" placeholder="Необходимо выбрать" name="_org_name" value='<?=$_GET['_org_name']?>'>
                    <input type="hidden" name="testId"/>
                    <datalist id="_org_name">

                        <?
                        $t = 1;
                        if (sizeof($_org_name)) foreach ($_org_name as $key => $value){
                            echo '<option data-id="'.$t.'" value=\''.$value['_org_name'].'\'">';
                            $t++;
                        }
                        ?>
                    </datalist>
                </div>



                <?
                if($wk_n){
                    ?>
                <div class="d-inter">
                    <div class="in_card_title">Подразделение:</div>
                    <input type="text" class="form-control autocomplete sel_in sel_in-c" autocomplete="off" list="_pos" placeholder="Необходимо выбрать" name="_pos" value='<?=$_GET['_pos']?>'>
                    <input type="hidden" name="testId"/>
                    <datalist id="_pos">
                        <?
                    $t = 1;

                    if (sizeof($wk_n)) foreach ($wk_n as $key => $value){
                        echo '<option data-id="'.$t.'" value=\''.$value['_otdel_name'].'\'>';
                        $t++;
                    }
                    ?>
                    </datalist>
                </div>
                <?
                }
                ?>


                <button class="bt_s_3">Сортировать</button>
            </form>
        </div><?




        function getDataAnalitic($_org_id, $otdel_id){
            if($_org_id){
                if($otdel_id){
                    $view_personals = execSQL('
                     SELECT t1.personal_id,t1.answer_ball,t1.c_data_d,t1.c_data_m, t2._org_id,t2._otdel_id
                     FROM post_shift_poll t1 LEFT JOIN view_personals t2 ON t1.personal_id = t2.pkey
                     where t2.clss=211 and t2.enabled=1 and t1.answer_ball > 0 and t2._org_id='.$_org_id.' and t2._otdel_id='.$otdel_id.'
                     group by t2.pkey
                     ');
                }else{
                    $view_personals = execSQL('
                     SELECT t1.personal_id,t1.answer_ball,t1.c_data_d,t1.c_data_m,t2._org_id,t2._otdel_id
                     FROM post_shift_poll t1 LEFT JOIN view_personals t2 ON t1.personal_id = t2.pkey
                     where t2.clss=211 and t2.enabled=1 and t1.answer_ball > 0 and t2._org_id='.$_org_id.'
                     group by t2.pkey
                     ');
                }
                return $view_personals;
            }else{
                echo 'NO id _org_id';
            }
        }



        function sortToDay($fil_data,$stepDay){
            $fil_data = array_values($fil_data);
            $sortDateValue = [];

            if (sizeof($fil_data)) foreach ($fil_data as $key => $value) {
                $cDay = date('y.m.d', $value['c_data_d']);
                if(!array_key_exists($cDay, $sortDateValue)) {
                    $sortDateValue[$cDay] = [];
                }
                array_push($sortDateValue[$cDay], $value);
            }

            $keySort = array_keys($sortDateValue);

            function date_sort($a, $b) {
                return strtotime($a) - strtotime($b);
            }

            usort($keySort, "date_sort");
            $keySort = array_reverse($keySort);

            $arrAns = [];
            $r = 0;
            while ($r < $stepDay){ // один цикл = 1 день
                $cnt = 0;
                $sum = 0;
                if (sizeof($sortDateValue[$keySort[$r]])) foreach ($sortDateValue[$keySort[$r]] as $key => $value) {
                    $sum = $sum + $value['answer_ball'];
                    $cnt++;
                }
                if($sum and $cnt){
                    $srBall = $sum / $cnt;
                }else{
                    $srBall = 0;
                }

                $arrDay = ['date' => $keySort[$r], 'ball' => $srBall];
                array_push($arrAns, $arrDay);
                $r++;
            }

            //print_r($arrAns);
            return $arrAns;
        }




        if($_GET['_org_name']){
            // Формируем среднее по организации
            $fil_data = getDataAnalitic($_org_name_orijin[$_GET['_org_name']]['_org_id'],$_work_names[$_GET['_pos']]['_otdel_id']);
            $addRows = sortToDay($fil_data,15);
            ?>


            <script>

                google.charts.load('current', {'packages':['line']});
                google.charts.setOnLoadCallback(drawChart);

                function drawChart() {

                    var data = new google.visualization.DataTable();
                    data.addColumn('string', '15 Дней');
                    data.addColumn('number', '<?=$_GET['_org_name']?> / <?=$_GET['_pos']?>');

                    data.addRows([
                        <?
                        $n = 1;
                        if (sizeof($addRows)) foreach ($addRows as $key => $value) {
                            echo "['".$value['date']."',  ".$value['ball']."],";
                            $n++;
                        }
                        ?>
                    ]);

                    var options = {
                        vAxis: {
                            title: 'Среднее значение',
                            minValue:0,
                            maxValue:6,
                            viewWindow: {
                                min: 0,
                                max: 6
                            }
                        },
                        chart: {
                            title: 'Динамика среднего балла: <?=$_GET['_org_name']?>',
                            subtitle: '<?=$_GET['_pos']?>',
                        },
                        height: 500,

                    };

                    var chart = new google.charts.Line(document.getElementById('linechart_material'));

                    chart.draw(data, google.charts.Line.convertOptions(options));
                }
            </script>
            <div id="linechart_material" style="margin-top: 50px;"></div>


            <?php
        }else{
            echo '<div class="none_text">Необходимо выбрать организацию</div>';
        }





    }


    function panel_report(){

        $report_obj = new c_page_cab_post_shift_poll_alternative_analytics();
        $report_obj->setting();
    }
}




