<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print $title; ?></div>
            <?php echo Forms::FloatingAddButton('4c0870', '#add_discount_modal', CommonLanguage::getMessage('add'), 'add_disc'); ?>
            <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('Category') ?></th>
                            <th><?php print CommonLanguage::getMessage('type') ?></th>
                            <th><?php print CommonLanguage::getMessage('percent') ?></th>
                            <th><?php print CommonLanguage::getMessage('start_date') ?></th>
                            <th><?php print CommonLanguage::getMessage('Action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="add_discount_modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-md">
        <div class="modal-content">
            <div class="modal-header text-left">
                <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'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"> <?php echo CommonLanguage::getMessage('Edt'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="add_disc_form" method="POST" onsubmit="return false">
                    <div class="row">
                        <?php echo Forms::AlertDiv('add_discount_alert'); ?>
                    </div>

                    <div class="row form-group m-t-10">
                        <div class="col-md-12">
                            <div class="col-md-3">
                                <label><?php print CommonLanguage::getMessage('Category'); ?></label>
                                <span class="red-star">*</span>
                            </div>
                            <div class="col-md-2">
                                <label><?php print CommonLanguage::getMessage('type'); ?></label>
                                <span class="red-star">*</span>
                            </div>
                            <div class="col-md-2">
                                <label><?php print CommonLanguage::getMessage('percent'); ?></label>
                                <span class="red-star">*</span>
                            </div>
                            <div class="col-md-3">
                                <label><?php print CommonLanguage::getMessage('start_date'); ?></label>
                                <span class="red-star">*</span>
                            </div>
                        </div>
                        <div id="add_disc_main_div">
                            <div class="row m-b-10 add_disc_row">
                                <div class="col-md-3">
                                    <input type="hidden"   name="category_id" class="full-width category_id" autocomplete="off" required >
                                </div>
                                <div class="col-md-2">
                                    <select name="current_type" class="full-width current_type" data-init-plugin="select2">
                                        <option value=""><?php  echo CommonLanguage::getMessage('select');?></option>
                                        <option value="1"><?php  echo CommonLanguage::getMessage('discount');?></option>
                                        <option value="2"><?php  echo CommonLanguage::getMessage('add');?></option>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <input type="number" min="0" max="100" stp="any"   name="percent" class="form-control percent" autocomplete="off" required >
                                </div>
                                <div class="col-md-3">
                                    <input type="text"   name="start_date" class="form-control start_date" autocomplete="off" required>
                                </div>
                                <div class="col-md-2 AddButtons">
                                    <span class="fa fa-plus-circle add_add_disc_row" style="color:#4c0870;margin-top:15px;margin-right:25px"></span>
                                    <span class="fa fa-minus-circle del_add_disc_row" style="color:#ed9421;margin-top:15px;"></span>
                                </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_disc"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('add_discount_loader'); ?>
        </div>
    </div>
</div>
<script>
    var report_setting_id = 0;
    $(function () {
        build_select2($("input[name=category_id]"), "xhr/commonSearch?col_id=Cat_ID&col_name=Cat_Name&table=tbl_product_cat")
        $('input.start_date').datepicker({autoclose: true})
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/add_discount_report_setting_ajax', 1);
        $(".add_disc").on("click", function () {
            $('#add_discount_modal .add-label,.AddButtons').removeClass('hidden');
            $("#report_setting_id").val(0);
            $('input.category_id').val('').trigger('change');
            $('select.current_type').val('').select2();
            $('.add_disc_row:gt(0)').remove();
        });

        $("#add_disc_main_div").on("click", " .fa-plus-circle", function (e) {
            $("#add_disc_main_div  .fa-minus-circle").css("display", "inline-block")
            var Parent = $(this).parents(".add_disc_row"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find(".select2-container").remove();
            NewItem.find('input').val('')
            NewItem.find('select.current_type').select2();
            NewItem.find("input.start_date").datepicker({autoclose: true})
            NewItem.find("input.category_id").attr("data-select-name", "")
            build_select2(NewItem.find("input.category_id"), "xhr/commonSearch?col_id=Cat_ID&col_name=Cat_Name&table=tbl_product_cat")
            $(".select2-container", NewItem).show();
        });
        $("#add_disc_main_div").on("click", ".fa-minus-circle", function (e) {
            if ($(".add_disc_row").length == 1) {
                $('.add_disc_row input').val('')
                $("input.category_id").attr("data-select-name", "")
                build_select2($("input.category_id"), "xhr/commonSearch?col_id=Cat_ID&col_name=Cat_Name&table=tbl_product_cat")
            } else {
                $(this).closest(".add_disc_row").empty().remove();
            }

        });

        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            IncludeDeleteModal(common_delete_modal, {report_setting_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/add_discount_report_setting_ajax');
            }, "InventoryAjax/delete_add_discount_report");
        });
        $("#tableWithSearch").on("click", ".edit", function () {
            $(".AddButtons").addClass("hidden")
            $('#add_discount_modal .edit-label').removeClass('hidden')
            report_setting_id = $(this).parents('div.Action').attr('data-id');
            $(".add_disc_row:gt(0)").empty().remove()
            $("input.category_id").val($(this).attr("data-cat-id")).attr("data-select-name", $(this).attr("data-cat-name")).trigger("change");
            $("input.percent").val($(this).attr("data-percent"));
            $("select.current_type").val($(this).attr("data-type")).select2();
            $("input.start_date").val($(this).attr("data-date"))

        });
        $("#submit_disc").on("click", function () {
            var validate = true;
//            hideModalAlert('add_discount_modal', 'add_discount_alert');
            $("#add_discount_loader").removeClass('hidden');
            var Data = {
                add_disc_arr: []
            };
            $(".add_disc_row").each(function () {
                var category_id = $(this).find('input.category_id').val(),
                    percent = parseFloat($(this).find('input.percent').val()),
                    start_date = $(this).find('input.start_date').val(),
                    current_type = $(this).find('select.current_type').val();
                if (category_id && percent > 0 && start_date && current_type) {
                    Data.add_disc_arr.push({
                        report_setting_id: report_setting_id,
                        category_id: category_id,
                        percent: percent,
                        current_type: current_type,
                        start_date: start_date
                    })
                } else {
                    if ((category_id == '' || percent == 0 || start_date == '' || current_type )
                        && (category_id != '' || percent > 0 || start_date != '' || !current_type)) {
                        validate = false;
                    }
                }
            })
            if (!validate) {
                AddAlert("#add_discount_alert", '<?php echo CommonLanguage::getMessage('Required_Fields') ?>', 'error');
                $("#add_discount_loader").addClass('hidden');

                return;

            }
            //            console.log(Data)
            //            return;
            $.ajax({
                url: report_setting_id > 0 ? "InventoryAjax/edit_add_disc_report" : "InventoryAjax/submit_add_disc_report",
                method: "POST",
                data: Data,
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseText == "OK") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/add_discount_report_setting_ajax', 1);
                            hideModalAlert('add_discount_modal', 'add_discount_alert');
                            AddAlert("#add_discount_alert", '<?php echo CommonLanguage::getMessage('Submitted_Successfully') ?>', 'success');
                        } else {
                            AddAlert("#add_discount_alert", '<?php echo CommonLanguage::getMessage('Failed_Submit') ?>', 'error');
                        }
                    } else {
                        AddAlert("#add_discount_alert", '<?php echo CommonLanguage::getMessage('Failed_Submit') ?>', 'error');
                    }
                    $("#add_discount_loader").addClass('hidden');
                }
            });
        });
    });
</script>