<div class="modal fade" id="checklist-schedule-answers-modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" style="width:60%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('add_checklist_answers'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_checklist_answers'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="checklist_schedule_answers_form" method="POST" onsubmit="return false">
                    <input type='hidden' name='answer_main_id' id='answer_main_id'/>
                    <div class="row">
                        <?php echo Forms::AlertDiv('checklist_schedule_answers_alert'); ?>
                    </div>
                    <div class="row m-t-10">
                        <div class="col-md-6 form-group">
                            <label><?php print CommonLanguage::getMessage('schedule_title'); ?></label>
                            <span class="red-star">*</span>
                            <input class="full-width" id="schedule_id" name="schedule_id" type="hidden" required />
                        </div>
                    </div>
                    <div class="row m-t-10 questions_main_div hide-close hidden">
                        <h3 class="semi-bold"><?php print CommonLanguage::getMessage('questions'); ?></h3>
                        <div id="questions_content">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button style="color:white !important;" class="btn bg-success-dark text-white" id="submit_checklist_schedule_answers"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('checklistScheduleAnswersLoader'); ?>
        </div>
    </div>
</div>
<script>
    var is_pop_modal = '<?php echo !empty($_GET['is_pop']) ? true :false;?>',
        pop_schedule_id='<?php echo !empty($_GET['schedule_id']) ? $_GET['schedule_id'] : '';?>',
        pop_schedule_name='<?php echo !empty($_GET['schedule_name']) ? $_GET['schedule_name'] : '';?>',
        startDate = today, endDate = today;
    $(function(){
        build_select2($("input[name=schedule_id]"),"xhr/ScheduleSearch?Status_ID=1");
        $('#add_checklist_schedule_answers').on('click',function(){
            $('#answer_main_id').val(0);
            $("input[name=schedule_id]").prop('disabled',false);
            $('#checklist-schedule-answers-modal .add-label').removeClass('hidden');
        });
        $('#schedule_id').on('change',function(){
            buildSchedule()
        });
        if(is_pop_modal){
            $('#add_checklist_schedule_answers').trigger('click');
            $('#schedule_id').val(pop_schedule_id);
            $('#schedule_id').attr('data-select-name',pop_schedule_name);
            build_select2($("input[name=schedule_id]"),"xhr/ScheduleSearch?Status_ID=1");
            buildSchedule()
        }
        $('#submit_checklist_schedule_answers').on('click',function(){
            if (!$("#checklist_schedule_answers_form").valid())
                return;
            var Data = new FormData(document.getElementById("checklist_schedule_answers_form")),answers =[],
                is_empty_answer = false;
            $('.question-row').each(function(){
                var question_type = $(this).attr('data-type'),question_solution = '';
                if(question_type == "1"){
                    if($('input.yes_no:checked',$(this)).length == 0)
                        is_empty_answer= true;
                    question_solution = $('input.yes_no:checked',$(this)).val();   
                }else if(question_type == "2"){
                    if(!$('input[name=question_number]',$(this)).val())
                        is_empty_answer = true;
                    question_solution = $('input[name=question_number]',$(this)).val();
                }else if(question_type == "3"){
                    if(!$('input[name=range]',$(this)).val())
                        is_empty_answer = true;
                    question_solution = $('input[name=range]',$(this)).val();
                }else  if(question_type == "4"){
                    if(!$('input[name=question_text]',$(this)).val())
                        is_empty_answer = true;
                    question_solution = $('input[name=question_text]',$(this)).val();
                }else{
                    if(!$('select.single_multiple_select',$(this)).val())
                        is_empty_answer = true; 
                    else{
                        if(question_type == "5"){
                            question_solution = $('select.single_multiple_select',$(this)).val();
                        }else{
                            question_solution = $('select.single_multiple_select',$(this)).val().join();
                        }
                    }
                }
                answers.push({checklist_question_id:$(this).attr('data-id'),
                              question_solution:question_solution});
            });
            Data.append('answers',JSON.stringify(answers));
            if(is_empty_answer){
                AddAlert("#checklist_schedule_answers_alert",'<?php echo CommonLanguage::getMessage('make_sure_answer_questions');?>', "danger");
                return;
            }
            $("#checklistScheduleAnswersLoader").removeClass('hidden');
            $.ajax({
                url: "MaintenanceAjax/submitChecklistScheduleAnswers",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0,1,2,3,4,5], 'MaintenanceAjax/getChecklistScheduleAnswerList');
                            hideModalAlert('checklist-schedule-answers-modal', 'checklist_schedule_answers_alert');
                        }
                        AddAlert("#checklist_schedule_answers_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#checklistScheduleAnswersLoader").addClass('hidden');
                }
            });
        });

        $('#tableWithSearch').on('click','.edit_checklist_schedule_answers',function(){
            var answer_main_id = $(this).parents('div.Action').attr('data-id');
            $('#answer_main_id').val(answer_main_id);
            $("#checklist-schedule-answers-modal .edit-label").removeClass('hidden');
            CommonViewById('MaintenanceAjax/getScheduleAnswers', {answer_main_id: answer_main_id},
                           '#checklistScheduleAnswersLoader', function (response) {
                var main = response['main'],details=response['details'];
                $("input[name=schedule_id]").val(main.schedule_id)
                $("input[name=schedule_id]").attr('data-select-name',main.schedule_title)
                build_select2($("input[name=schedule_id]"),"xhr/ScheduleSearch?is_edited="+main.Product_ID);
                $("input[name=schedule_id]").prop('disabled',true)
                fillQuestionDetails(details,'#questions_content','.questions_main_div');
            });
        });
    });
    function buildSchedule(){
        $("#checklistScheduleAnswersLoader").removeClass('hidden');
        $.ajax({
            url: "MaintenanceAjax/getScheduleQuestions?schedule_id="+$('#schedule_id').val(),
            dataType: "JSON",
            complete: function (jqXHR, status) {
                if (status == 'success') {
                    fillQuestionDetails(jqXHR.responseJSON,'#questions_content','.questions_main_div');
                }
                $("#checklistScheduleAnswersLoader").addClass('hidden');
            }
        }); 
    }
</script>