<div class="modal fade" id="checklist-schedule-modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <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('new_checklist_schedule'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_checklist_schedule'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="checklist_schedule_form" method="POST" onsubmit="return false">
                    <input type='hidden' name='schedule_id' id='schedule_id'/>
                    <div class="row">
                        <?php echo Forms::AlertDiv('checklist_schedule_alert'); ?>
                    </div>
                    <div class="row m-t-10">
                        <div class="col-md-3 form-group">
                            <label><?php print CommonLanguage::getMessage('title'); ?></label>
                            <span class="red-star">*</span>
                            <input class="form-control" id="schedule_title" name="schedule_title" type="text" required />
                        </div>
                        <div class="col-md-3 form-group">
                            <label><?php print CommonLanguage::getMessage('start_date'); ?></label>
                            <span class="red-star">*</span>
                            <input class="form-control" id="schedule_start_date" value="" name="schedule_start_date" autocomplete="off"  type="text" required />
                        </div>
                        <div class="col-md-3 form-group">
                            <label><?php print CommonLanguage::getMessage('end_date'); ?></label>
                            <input class="form-control" id="schedule_end_date" name="schedule_end_date" autocomplete="off"  type="text" />
                        </div>
                        <div class="col-md-3 form-group">
                            <label><?php print CommonLanguage::getMessage('checklist_name'); ?></label>
                            <span class="red-star">*</span>
                            <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Maintenance/checklist?is_pop=1&open_modal=1" style="color:#4c0870;cursor:pointer"></span>
                            <input class="full-width" id="checklist_id" name="checklist_id" autocomplete="off"  type="hidden" required />
                        </div>
                    </div> 
                    <div class="row m-t-10">
                        <div class="col-md-3 form-group">
                            <label><?php print CommonLanguage::getMessage('line'); ?></label>
                            <span class="red-star">*</span>
                            <input class="full-width" id="line_id" name="line_id" autocomplete="off"   type="hidden" required />
                        </div>
                        <div class="col-md-3 form-group">
                            <label><?php print CommonLanguage::getMessage('machine'); ?></label>
                            <span class="red-star">*</span>
                            <input class="full-width" id="Product_ID" name="Product_ID" autocomplete="off"   type="hidden" required />
                        </div>
                        <div class="col-md-3 form-group">
                            <label><?php print CommonLanguage::getMessage('part'); ?></label>
                            <input class="full-width" id="part_id" name="part_id" autocomplete="off"   type="hidden" />
                        </div>
                        <div class="col-md-3 form-group">
                            <label><?php print CommonLanguage::getMessage('schedule_type'); ?></label>
                            <span class="red-star">*</span>
                            <select required id="schedule_type_id" name="schedule_type_id" class="full-width " data-init-plugin="select2" required>
                                <option value="">Choose type</option>
                                <option value="1">Period</option>
                                <option value="2">Property</option>
                            </select>
                        </div>
                    </div> 
                    <div class="row m-t-10">
                        <div class="col-md-3 form-group period  hide-close hidden">
                            <label><?php print CommonLanguage::getMessage('period'); ?></label>
                            <span class="red-star">*</span>
                            <div class="radio radio-success-dark ">
                                <input type="radio" value="1"  name="period" id="hourly" >
                                <label for="hourly"><?php echo CommonLanguage::getMessage('hourly');?></label>
                                <input type="radio" value="2"  name="period" id="daily" >
                                <label for="daily"><?php echo CommonLanguage::getMessage('Daily');?></label>
                                <input type="radio" value="3"  name="period" id="weekly" >
                                <label for="weekly"><?php echo CommonLanguage::getMessage('weekly');?></label>
                                <input type="radio" value="4"  name="period" id="monthly" >
                                <label for="monthly"><?php echo CommonLanguage::getMessage('monthly');?></label>
                            </div>
                        </div>
                        <div class="col-md-3 form-group hourly-period period-options hide-close hidden">
                            <label class=""><?php echo CommonLanguage::getMessage('nb_of_hours');?></label>
                            <i class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('nb_hours_rep_schd');?>"></i>
                            <input class="form-control" id="nb_of_hours" name="nb_of_hours"  type="number"/>
                        </div>
                        <div class="col-md-3 form-group daily-period period-options hide-close hidden">
                            <label class=""><?php echo CommonLanguage::getMessage('nb_of_days');?></label>
                            <i class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('nb_days_rep_schd');?>"></i>
                            <input class="form-control" id="nb_of_days" name="nb_of_days"  type="number"/>
                        </div>
                        <div class="col-md-8 form-group weekly-period period-options hide-close hidden">
                            <label class=""><?php echo CommonLanguage::getMessage('week_days');?></label>
                            <i class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('week_days_rep_schd');?>"></i>
                            <div class="checkbox checkbox-success-dark ">
                                <input type="checkbox" value="1"  name="week_days" id="monday" >
                                <label for="monday"><?php echo CommonLanguage::getMessage('Monday');?></label>
                                <input type="checkbox" value="2"  name="week_days" id="tuesday" >
                                <label for="tuesday"><?php echo CommonLanguage::getMessage('Tuesday');?></label>
                                <input type="checkbox" value="3"  name="week_days" id="wednesday" >
                                <label for="wednesday"><?php echo CommonLanguage::getMessage('Wednesday');?></label>
                                <input type="checkbox" value="4"  name="week_days" id="thursday" >
                                <label for="thursday"><?php echo CommonLanguage::getMessage('Thursday');?></label>
                                <input type="checkbox" value="5"  name="week_days" id="friday" >
                                <label for="friday"><?php echo CommonLanguage::getMessage('Friday');?></label>
                                <input type="checkbox" value="6"  name="week_days" id="saturday" >
                                <label for="saturday"><?php echo CommonLanguage::getMessage('Saturday');?></label>
                                <input type="checkbox" value="7"  name="week_days" id="sunday" >
                                <label for="sunday"><?php echo CommonLanguage::getMessage('Sunday');?></label>
                            </div>
                        </div>
                        <div class="col-md-3 form-group monthly-period period-options hide-close hidden">
                            <label class=""><?php echo CommonLanguage::getMessage('nb_of_months');?></label>
                            <i class="fa fa-info-circle"  title="<?php echo CommonLanguage::getMessage('nb_month_rep_schd');?>"></i>
                            <input class="form-control" id="nb_of_months" name="nb_of_months" min="1"  type="number"/>
                        </div>
                        <div class="col-md-3 form-group hide-close hidden">
                            <label class=""><?php echo CommonLanguage::getMessage('day_no');?></label>
                            <input class="form-control" id="day_no" name="day_no" min="1" max="31" type="number"/>
                        </div>
                        <div class="col-md-3 form-group property hide-close hidden">
                            <label class=""><?php echo CommonLanguage::getMessage('property');?></label>
                            <input class="full-width" id="property_id" name="property_id"  type="hidden"/>
                        </div>
                        <div class="col-md-3 form-group property hide-close hidden">
                            <label class=""><?php echo CommonLanguage::getMessage('property_value');?></label>
                            <input class="form-control" id="property_value" name="property_value"  type="number"/>
                        </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"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('checklistScheduleLoader'); ?>
        </div>
    </div>
</div>
<script>
 
    $(function(){
        $("#schedule_start_date").datetimepicker({format: 'YYYY-MM-DD HH:mm:ss',defaultDate:new Date(),minDate:new Date()});
        $("#schedule_end_date").datetimepicker({format: 'YYYY-MM-DD HH:mm:ss',useCurrent: false});
        $('#schedule_end_date').data("DateTimePicker").minDate(new Date());
        $("#schedule_start_date").on("dp.change", function (e) {
            $('#schedule_end_date').data("DateTimePicker").minDate(e.date);
        });
        $('#schedule_type_id').on('click',function(){
            if($(this).val() == 1){
                $('.period').removeClass('hidden')
                $('.property').addClass('hidden')
            }else if($(this).val() == 2){
                $('input[name=period]').prop('checked',false)
                $('.period,.period-options').addClass('hidden')
                $('.property').removeClass('hidden')
            }else{
                $('.hide-close').addClass('hidden')
            }
        });
        $('input[name=period]').on('click',function(){
            $('.period-options').addClass('hidden');
            var type = $('input[name=period]:checked').attr('id');
            $('.'+type+'-period').removeClass('hidden');
        });
        $('#submit_checklist_schedule').on('click',function(){
            if (!$("#checklist_schedule_form").valid())
                return;
            var Data = new FormData(document.getElementById("checklist_schedule_form")),week_days_array=[]
            if($('#schedule_type_id').val() == 1){
                if($('input[name=period]:checked').length == 0){
                    AddAlert("#checklist_schedule_alert",'<?php echo CommonLanguage::getMessage('choose_period_alert');?>', 'danger');
                    return;
                }

                var period_value = $('input[name=period]:checked').val();
                if(period_value == "1"){
                    if(!$('#nb_of_hours').val()){
                        AddAlert("#checklist_schedule_alert",'<?php echo CommonLanguage::getMessage('enter_hours');?>', 'danger');
                        return;
                    }
                }else if(period_value == "2"){
                    if(!$('#nb_of_days').val()){
                        AddAlert("#checklist_schedule_alert",'<?php echo CommonLanguage::getMessage('enter_days');?>', 'danger');
                        return;
                    }
                }else if(period_value == "3"){
                    if($('input[name=week_days]:checked').length == 0){
                        AddAlert("#checklist_schedule_alert",'<?php echo CommonLanguage::getMessage('choose_week_days');?>', 'danger');
                        return;
                    }
                    $('input[name=week_days]:checked').each(function(){
                        week_days_array.push($(this).val())
                    });
                    Data.append('week_days',week_days_array);
                }else{
                    if(!$('#nb_of_months').val()){
                        AddAlert("#checklist_schedule_alert",'<?php echo CommonLanguage::getMessage('enter_nb_months');?>', 'danger');
                        return;
                    }
                    //                    if(!$('#day_no').val()){
                    //                        AddAlert("#checklist_schedule_alert",'< ?php echo CommonLanguage::getMessage('enter_day_no');?>', 'danger');
                    //                        return;
                    //                    }
                }
            }else if($('#schedule_type_id').val() == 2){
                if(!$('#property_id').val()){
                    AddAlert("#checklist_schedule_alert",'<?php echo CommonLanguage::getMessage('choose_property_alert');?>', 'danger');
                    return;
                }
                if(!$('#property_value').val()){
                    AddAlert("#checklist_schedule_alert",'<?php echo CommonLanguage::getMessage('choose_property_value_alert');?>', 'danger');
                    return;
                }
            }
            $("#checklistScheduleLoader").removeClass('hidden');
            $.ajax({
                url: "MaintenanceAjax/submitChecklistSchedule",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            hideModalAlert('checklist-schedule-modal', 'checklist_schedule_alert');
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0,1,2,3,4,5], 'MaintenanceAjax/getChecklistSchedule');
                        }
                        AddAlert("#checklist_schedule_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#checklistScheduleLoader").addClass('hidden');
                }
            });
        });
        
        build_select2($("input[name=checklist_id]"),"xhr/CommonSearch?col_name=checklist_name&col_id=checklist_id&table=tbl_checklist"); build_select2($("input[name=line_id]"),"xhr/CommonSearch?col_name=line_name&col_id=line_id&table=tbl_line"); 
        
        build_select2($("input[name=part_id]"),"xhr/PartSearch?Product_ID="+$("input[name=Product_ID]").val());
        build_select2($("input[name=property_id]"),"xhr/PropertySearch?Product_ID="+$("input[name=Product_ID]").val());
        
        $("input[name=Product_ID]").on('change',function(){
            $("input[name=part_id],input[name=property_id]").val('').trigger('change');
            build_select2($("input[name=part_id]"),"xhr/PartSearch?Product_ID="+$("input[name=Product_ID]").val()); 
            build_select2($("input[name=property_id]"),"xhr/PropertySearch?Product_ID="+$("input[name=Product_ID]").val());
        });
        $("input[name=line_id]").on('change',function(){
            $("input[name=part_id],input[name=property_id],input[name=Product_ID]").val('').trigger('change');
             build_select2($("input[name=Product_ID]"),"xhr/MachineLineSearch?Status_ID=1&line_id="+$('#line_id').val());
            build_select2($("input[name=part_id]"),"xhr/PartSearch?Product_ID="+$("input[name=Product_ID]").val()); 
            build_select2($("input[name=property_id]"),"xhr/PropertySearch?Product_ID="+$("input[name=Product_ID]").val());
        });
        $('#new_checklist_schedule').on('click',function(){
            $('#schedule_id').val(0)
            $("#checklist-schedule-modal .add-label").removeClass('hidden');
            build_select2($("input[name=Product_ID]"),"xhr/MachineLineSearch?Status_ID=1&line_id="+$('#line_id').val());
        });
        $('#tableWithSearch').on('click','.edit_checklist_schedule',function(){
            var schedule_id = $(this).parents('div.Action').attr('data-id');
            $('#schedule_id').val(schedule_id)
            $("#checklist-schedule-modal .add-label").removeClass('hidden');
            CommonViewById('MaintenanceAjax/getChecklistScheduleData', {schedule_id: schedule_id},
                           '#checklistScheduleLoader', function (response) {
                var main = response[0];
                $("input[name=schedule_title]").val(main.schedule_title);
                $("input[name=schedule_start_date]").val(main.schedule_start_date);
                $("input[name=schedule_end_date]").val(main.schedule_end_date);
                $("input[name=line_id]").val(main.line_id);
                $("input[name=line_id]").attr("data-select-name", main.line_name);
                build_select2($("input[name=line_id]"),"xhr/CommonSearch?col_name=line_name&col_id=line_id&table=tbl_line"); 
                $("input[name=Product_ID]").val(main.Product_ID);
                $("input[name=Product_ID]").attr("data-select-name", main.product_full_name);
                build_select2($("input[name=Product_ID]"),"xhr/MachineLineSearch?type=1&is_edited="+main.Product_ID+"&line_id="+$('#line_id').val());
                $("input[name=checklist_id]").val(main.checklist_id);
                $("input[name=checklist_id]").attr("data-select-name", main.checklist_name);
                build_select2($("input[name=checklist_id]"),"xhr/CommonSearch?col_name=checklist_name&col_id=checklist_id&table=tbl_checklist");
                $("input[name=part_id]").val(main.part_id);
                $("input[name=part_id]").attr("data-select-name", main.product_part_note);
                build_select2($("input[name=part_id]"),"xhr/PartSearch?Product_ID="+main.Product_ID);
                $('#schedule_type_id').val(main.schedule_type_id).select2();
                if(main.schedule_type_id == 1){
                    $('input[name=period][value='+main.schedule_repeat_id+']').prop('checked',true)
                    var type = $('input[name=period][value='+main.schedule_repeat_id+']').attr('id');
                    $('.'+type+'-period,.period').removeClass('hidden');
                    if(main.schedule_repeat_id == 1){
                        $('#nb_of_hours').val(main.schedule_repeat_value)
                    }else if(main.schedule_repeat_id == 2){
                        $('#nb_of_days').val(main.schedule_repeat_value)
                    }else if(main.schedule_repeat_id == 3){
                        var week_days= main.schedule_repeat_value.split(',')
                        $.each(week_days, function( index, value ) {
                            $('input[name=week_days][value='+value+']').prop('checked',true);
                        });
                    }else{
                        var current_schedule_value = main.schedule_repeat_value.split('##')
                        $('#nb_of_months').val(current_schedule_value[0])
                        //                        $('#day_no').val(current_schedule_value[1])
                    }
                }else{
                    $('#property_id').val(main.schedule_repeat_id)
                    $('#property_id').attr('data-select-name',main.property_name)
                    build_select2($("input[name=property_id]"),"xhr/PropertySearch?Product_ID="+main.Product_ID);
                    $('#property_value').val(main.schedule_repeat_value);
                    $('.property').removeClass('hidden');

                }
            });
        });
    });
</script>