<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php echo CommonLanguage::getMessage('general_criteria'); ?></div>
            <?php 
            echo Forms::FloatingAddButton('4c0870', '#general-criteria-modal', CommonLanguage::getMessage('Add'), 'new_general_criteria'); ?>
            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered inventory" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th><?php print CommonLanguage::getMessage('criteria_name') ?></th>
                            <th><?php print CommonLanguage::getMessage('criteria_type') ?></th>
                            <th><?php print CommonLanguage::getMessage('criteria_details') ?></th>
                            <th><?php print CommonLanguage::getMessage('Action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="general-criteria-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 semi-bold hidden"><?php echo CommonLanguage::getMessage('add_general_criteria');?></h5>
                <h5 class="edit-label h-labels text-left semi-bold hidden"><?php echo CommonLanguage::getMessage('edit_general_criteria');?></h5>
            </div>
            <div class="modal-body">
                <form id="general_criteria_form" method="POST" onsubmit="return false">
                    <input type="hidden" name="criteria_id" id="criteria_id"/>
                    <div class="row">
                        <?php echo Forms::AlertDiv('general_criteria_alert'); ?>
                        <div class="col-md-12 AddCriteriaRow m-b-10 form-group add-row"  >
                            <div class="col-md-12">
                                <div class="col-xs-4 p-l-0">
                                    <label ><?php print CommonLanguage::getMessage('criteria_name'); ?></label>
                                    <input type="text"  placeholder="<?php print CommonLanguage::getMessage('criteria_name'); ?>" name="criteria_name" class="form-control ">
                                </div>
                                <div class="col-xs-2 p-l-0">
                                    <label ><?php print CommonLanguage::getMessage('Category'); ?></label>
                                    <input type="hidden" name="category_id" class="full-width category_id"/>
                                </div>
                                <div class="col-xs-2 p-l-0">
                                    <label ><?php print CommonLanguage::getMessage('criteria_type'); ?></label>
                                    <select name="criteria_type" class="full-width  criteria_type" data-init-plugin="select2" required>
                                        <option value="1"><?php print CommonLanguage::getMessage('checkbox'); ?></option>
                                        <option value="2"><?php print CommonLanguage::getMessage('radio'); ?></option>
                                    </select>
                                </div>

                                <div class="col-xs-2 p-l-0 criteria_count">
                                    <label><?php print CommonLanguage::getMessage('criteria_max_count'); ?></label>
                                    <input type="text"  name="criteria_max_count" class="form-control " placeholder="<?php print CommonLanguage::getMessage('criteria_max_count'); ?>" >
                                </div>
                                <div class="col-xs-2 AddButtons">
                                    <span class="fa fa-plus-circle" style="color:#4c0870;margin-top:15px;margin-right:25px"></span>
                                    <span class="fa fa-minus-circle" style="color:#ed9421;margin-top:15px;"></span>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="criteria_detail_divs add-row-sub" >
                                    <div class="row">
                                        <div class="col-xs-4 p-l-0">
                                            <label ><?php print CommonLanguage::getMessage('criteria_detail'); ?></label>
                                        </div>
                                        <div class="col-xs-4 p-l-0">
                                            <label ><?php print CommonLanguage::getMessage('criteria_detail_price'); ?></label>
                                        </div>
                                    </div>
                                    <div class="criteria-items">
                                        <div class="row m-t-10 cr-row" data-criteria-detail-id="0">
                                            <div class="col-xs-4 p-l-0">
                                                <input type="text"   name="criteria_detail_name" class="form-control criteria_detail_name">
                                            </div>
                                            <div class="col-xs-4 p-l-0">
                                                <input type="text"   name="criteria_detail_price" class="form-control criteria_detail_price">
                                            </div>
                                            <div class="col-xs-4 p-l-0">
                                                <span class="fa fa-plus-circle" style="color:#4c0870;margin-top:15px;margin-right:25px"></span>
                                                <span class="fa fa-minus-circle" style="color:#ed9421;margin-top:15px;"></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button class="btn btn-primary" id="submit_general_criteria"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('generalCriteriaLoader'); ?>
        </div>
    </div>
</div>
<script>
    $(function(){
        build_select2($("input[name=category_id]"), "xhr/catSearch");
        RebuildDataTable($('.panel-heading .panel-title').html(), [0,1,2], 'InventoryAjax/generalCriteria');
        $('.new_general_criteria').on('click',function(){
            $('#criteria_id').val(0);
            $('.criteria_detail_divs .criteria-items .cr-row').attr('data-criteria-detail-id',0);
            $('.criteria_detail_divs .criteria-items .cr-row:gt(0)').remove()
            $('.criteria_detail_divs .criteria-items .fa-minus-circle').removeClass('prevent-delete-details')
            $('.AddButtons .fa-plus-circle,.AddButtons .fa-minus-circle').css({"display":"inline-block"});
            $('input[name=category_id]').val('').trigger('change')
            $('#general-criteria-modal .add-label').removeClass('hidden');
        });
        $("#general_criteria_form").on("click", ".AddButtons .fa-plus-circle", function (e) {
            var Parent = $(this).parents(".AddCriteriaRow"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input,textarea").val('');
            $(".select2-container", NewItem).empty().remove()
            build_select2($("input[name=category_id]",NewItem), "xhr/catSearch");
            NewItem.find(".criteria_type").show().select2()
            $(".select2-container", NewItem).show()
            $('.criteria_count', NewItem).removeClass('hidden')
        });
        $("#general_criteria_form").on("click", ".AddButtons .fa-minus-circle", function (e) {
            if($(".AddCriteriaRow").length == 1)
                return;
            $(this).closest(".AddCriteriaRow").empty().remove();
        });
        $("#general_criteria_form").on("click", ".criteria-items .fa-plus-circle", function (e) {
            var Parent = $(this).parents(".cr-row"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            $('.fa-minus-circle',NewItem).removeClass('prevent-delete-details')
            NewItem.attr('data-criteria-detail-id',0);
        });
        $("#general_criteria_form").on("click", ".criteria-items .fa-minus-circle", function (e) {
            var parent =$(this).parents('.AddCriteriaRow');
            if($(".cr-row", parent).length == 1)
                return;
            if($(this).hasClass('prevent-delete-details')){
                AddAlert('#general_criteria_alert','<?php echo CommonLanguage::getMessage('unable_delete_criteria_details');?>','danger')
                return;
            }
            $(this).parents(".cr-row").remove();
        });
        $('#tableWithSearch').on('click','.edit_criteria',function(){
            $('.AddButtons .fa-plus-circle,.AddButtons .fa-minus-circle').css({"display":"none"});
            $('#general-criteria-modal .edit-label').removeClass('hidden');
            $('#criteria_id').val($(this).attr('data-id'))
            CommonViewById('RestaurantAjax/getProductCriteria',{criteria_id:$(this).attr('data-id'),Product_ID:0},
                           '#generalCriteriaLoader',function(response){
                var details = response[0].detail,current_row = $(".criteria-items .cr-row:eq(0)").clone();
                $('#general-criteria-modal input[name=criteria_name]').val(response[0].criteria_name);
                $('#general-criteria-modal select[name=criteria_type]').val(response[0].criteria_type).select2();
                if(response[0].category_name){
                    $("input[name=category_id]").val(response[0].category_id).attr('data-select-name',response[0].category_name)
                    build_select2($("input[name=category_id]"), "xhr/catSearch"); 
                }
                response[0].criteria_type == 1 ? $('.criteria_count').removeClass('hidden') : $('.criteria_count').addClass('hidden')
                $('#general-criteria-modal input[name=criteria_max_count]').val(response[0].criteria_max_count);
                if(details.length > 0){
                    $(".criteria-items .cr-row").remove();
                    $.each(details,function(index,value){
                        current_row = current_row == null ? $(".criteria-items .cr-row:eq(0)").clone() : current_row;
                        $('input[name=criteria_detail_name]',current_row).val(details[index].criteria_detail_name);
                        $("input[name=criteria_detail_price]",current_row).val(details[index].criteria_detail_price);
                        current_row.attr('data-criteria-detail-id',details[index].criteria_detail_id)
                        $(".criteria-items").append(current_row);
                        if(details[index].details_id)
                            $(".fa-minus-circle",current_row).addClass("prevent-delete-details")
                        else
                            $(".fa-minus-circle",current_row).removeClass("prevent-delete-details")
                        current_row = null
                    }); 
                }

            });
        })

        $("#general_criteria_form").on("change", 'select[name=criteria_type]', function () {
            var Parent = $(this).parents('.AddCriteriaRow');
            $(this).val() == 1 ? $(".criteria_count", Parent).removeClass('hidden') : ($(".criteria_count", Parent).addClass('hidden') && $(".criteria_count > input", Parent).val(1))
        }); 
        $("#submit_general_criteria").on("click", function () {
            var is_criteria_details_empty = false, is_criteria_max_count_empty = false,is_criteria_name_empty = false;
            Data = {criteria:[],criteria_id:$("#criteria_id").val(),criteria_detail_ids:[]};
            $('.AddCriteriaRow').each(function () {
                if ($("input[name=criteria_name]", $(this)).val() != '') {
                    if ($('select[name=criteria_type]', $(this)).val() == 1 && $('input[name=criteria_max_count]', $(this)).val() == '') {
                        is_criteria_max_count_empty = true
                    }
                    var criteria_detail_arr = [];
                    $(".cr-row",$(this)).each(function(){
                        if($(".criteria_detail_name",$(this)).val() != ''){
                            criteria_detail_arr.push({
                                criteria_detail_name: $(".criteria_detail_name",$(this)).val(),
                                criteria_detail_price: $(".criteria_detail_price",$(this)).val(),
                                criteria_detail_id: $(this).attr('data-criteria-detail-id')
                            });  
                            Data.criteria_detail_ids.push($(this).attr('data-criteria-detail-id'));
                        }
                    })
                    Data.criteria.push({
                        criteria_name: $("input[name=criteria_name]", $(this)).val(),
                        criteria_type: $("select[name=criteria_type]", $(this)).val(),
                        criteria_max_count: $("input[name=criteria_max_count]", $(this)).val(),
                        criteria_details: criteria_detail_arr,
                        category_id:$("input[name=category_id]", $(this)).val()
                    })

                    if(criteria_detail_arr.length==0){
                        is_criteria_details_empty = true;
                    }
                }else
                    is_criteria_name_empty = true;
            });
            if (is_criteria_name_empty) {
                AddAlert('#general_criteria_alert', '<?php echo CommonLanguage::getMessage('fill_criteria_name'); ?>', 'danger')
                return;
            }
            if (is_criteria_max_count_empty) {
                AddAlert('#general_criteria_alert', '<?php echo CommonLanguage::getMessage('fill_criteria_max_count'); ?>', 'danger')
                return;
            }
            if (is_criteria_details_empty) {
                AddAlert('#general_criteria_alert', '<?php echo CommonLanguage::getMessage('fill_criteria_details'); ?>', 'danger')
                return;
            }
            $("#generalCriteriaLoader").removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/submitGeneralCriteria",
                method: "POST",
                data: Data,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0,1,2], 'InventoryAjax/generalCriteria');
                            hideModalAlert('general-criteria-modal', 'general_criteria_alert');
                            $('.criteria_detail_divs .criteria-items .cr-row').attr('data-criteria-detail-id',0)
                        }
                        AddAlert("#general_criteria_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#generalCriteriaLoader").addClass('hidden');
                }
            });
        });
    });
</script>