<?php
include_once(_DIR_EXT.'/HTML/c_page_XML_AJAX.php') ;
include_once('i_daily_briefing_ui.php') ;

class c_page_ajax extends c_page_XML_AJAX
{
    function show_create_briefing_modal()
    {
        if (MEMBER()->rol!=1) {
            $this->add_element('show_notife','Создание инструктажей доступно только администратору',array('color'=>'red'));
            return;
        }

        $this->add_element('show_modal_window','HTML',array('title'=>'Создание инструктажа'));
        ?>
        <form id="form_create_daily_briefing" class="sky-form no_box_shadow">
            <fieldset>
                <section>
                    <label class="label">Наименование нового инструктажа</label>
                    <label class="input">
                        <input type="text" name="obj_name" value="Инструктаж техника" data-required>
                    </label>
                    <input type="hidden" name="briefing_type" value="technician_instruction">
                </section>
            </fieldset>
            <div class="center">
                <button class="button v2" type="submit" cmd="daily_briefing/create_briefing" use_form="form_create_daily_briefing">Создать</button>
                &nbsp;&nbsp;
                <button class="cancel" type="button">Отмена</button>
            </div>
        </form>
        <?php
    }

    function create_briefing()
    {
        if (MEMBER()->rol!=1) {
            $this->add_element('show_notife','Создание инструктажей доступно только администратору',array('color'=>'red'));
            return;
        }

        $obj_name=trim((string)$_POST['obj_name']);
        if ($obj_name==='') {
            $this->add_element('show_notife','Укажите наименование инструктажа',array('color'=>'red'));
            return;
        }

        $briefing_type=trim((string)$_POST['briefing_type']);
        if ($briefing_type==='') $briefing_type='technician_instruction';

        $briefing_id=DAILY_BRIEFING()->create_briefing($obj_name,array('briefing_type'=>$briefing_type));
        if ($briefing_id) $this->go_url='/cab/daily_briefing/card/'.$briefing_id.'/';
        else $this->add_element('show_notife','Не удалось создать инструктаж',array('color'=>'red'));
    }

    function register_arrive()
    {
        $rec_briefing=DAILY_BRIEFING()->get_briefing_by_id((int)$_POST['briefing_id']);
        if (!$rec_briefing['pkey']) {
            $this->add_element('show_notife','Нет доступа к этому инструктажу',array('color'=>'red'));
            return;
        }
        $result=DAILY_BRIEFING()->register_arrive($_POST['briefing_id'],$_POST['propusk']);
        if ($result['error']) {
            $this->add_element('show_notife',$result['error'],array('color'=>'red'));
            $this->add_element('JSCODE','<script type="text/javascript">$j("#propusk").val("").focus();</script>');
            return;
        }

        ob_start();
        daily_briefing_render_current_pass_panel($result['pass']);
        $this->add_element('daily_briefing_current_pass.HTML',ob_get_clean());

        $this->refresh_briefing_panels($_POST['briefing_id'],$result['pass'],array('refresh_journal'=>(int)!$result['already_exists']));
        $this->add_element('show_notife',$result['message'],array('color'=>($result['pass']['status']==2)?'green':'blue'));
        $this->add_element('JSCODE','<script type="text/javascript">$j("#propusk").val("").focus();</script>');
    }

    function sign_pass()
    {
        $rec_pass=DAILY_BRIEFING()->get_pass_by_id((int)$_POST['pass_id']);
        if (!$rec_pass['pkey']) {
            $this->add_element('show_notife','Запись не найдена',array('color'=>'red'));
            return;
        }
        $rec_briefing=DAILY_BRIEFING()->get_briefing_by_id((int)$rec_pass['briefing_id']);
        if (!$rec_briefing['pkey']) {
            $this->add_element('show_notife','Нет доступа к этому инструктажу',array('color'=>'red'));
            return;
        }
        $result=DAILY_BRIEFING()->save_signature($_POST['pass_id'],$_POST['image'],$_POST['sign_data']);
        if ($result['error']) {
            $this->add_element('show_notife',$result['error'],array('color'=>'red'));
            return;
        }

        $rec_pass=$result['pass'];
        ob_start();
        daily_briefing_render_current_pass_panel($rec_pass);
        $this->add_element('daily_briefing_current_pass.HTML',ob_get_clean());

        $this->refresh_briefing_panels($rec_pass['briefing_id'],$rec_pass);
        $this->add_element('show_notife','Подпись сохранена, инструктаж завершен',array('color'=>'green'));
        $this->add_element('JSCODE','<script type="text/javascript">$j("#propusk").focus();</script>');
    }

    function refresh_briefing_panels($briefing_id,$rec_pass=array(),$options=array())
    {
        ob_start();
        daily_briefing_render_stage2_progress_inner($briefing_id);
        $this->add_element('daily_briefing_stage2_progress_wrap.HTML',ob_get_clean());

        ob_start();
        daily_briefing_render_conduct_sidebar($briefing_id,$rec_pass);
        $this->add_element('daily_briefing_sidebar_wrap.HTML',ob_get_clean());

        if (!isset($options['refresh_journal']) || $options['refresh_journal']) {
            ob_start();
            daily_briefing_render_recent_journal($briefing_id);
            $this->add_element('daily_briefing_journal.HTML',ob_get_clean());
        }
    }
}
?>
