<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
            if ($permissions->hasPermission('add_invoice_type')) {
                echo Forms::FloatingAddButton('4c0870', '#invoice_type_modal', CommonLanguage::getMessage('add'), 'add_invoice_type');
            }
            ?>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered inventory" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th><?php print CommonLanguage::getMessage('invoice_type_name') ?></th>
                            <th><?php print CommonLanguage::getMessage('invoice_type_name_actual') ?></th>
<!--                            <th>< ?php print CommonLanguage::getMessage('sales_account') ?></th>-->
                            <th><?php print CommonLanguage::getMessage('sort_order') ?></th>
                            <th><?php print CommonLanguage::getMessage('is_shown_menu') ?></th>
                            <th><?php print CommonLanguage::getMessage('Action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="invoice_type_modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <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_invoice_type'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"> <?php echo CommonLanguage::getMessage('edit_invoice_type'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row">
                    <?php echo Forms::AlertDiv('invoice_type_alert'); ?>
                </div>
                <form id="invoice_type_form" method="POST" onsubmit="return false">
                    <input type='hidden' name='invoice_type_id' id='invoice_type_id'/>
                    <div class="row form-group m-t-10">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('invoice_type_name'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('invoice_type_name'); ?>" id="invoice_type_name" name="invoice_type_name" class="form-control " autocomplete="off" required>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('invoice_type_name_actual'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('invoice_type_name_actual'); ?>" id="invoice_type_name_actual" name="invoice_type_name_actual" class="form-control " autocomplete="off" required>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('sort_order'); ?></label>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('sort_order'); ?>" id="sort_order" name="sort_order" class="form-control " autocomplete="off" >
                        </div>
                        <div class="col-md-3">
                            <div class="checkbox check-primary ">
                                <input type="checkbox" id="is_shown_menu" name="is_shown_menu">
                                <label for="is_shown_menu"><?php print CommonLanguage::getMessage('is_shown_menu'); ?></label>
                            </div>
                        </div>

                    </div> 

                    <hr>
                    <div class="row form-group m-t-10">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('cash_account'); ?></label>
                            <input type="hidden"  placeholder="<?php print CommonLanguage::getMessage('cash_account'); ?>" id="cash_account_id" name="cash_account_id" class="full-width " autocomplete="off" >
                        </div>
                        <div class="col-md-3">
                            <div class="checkbox check-primary ">
                                <input type="checkbox" id="prevent_change_cash" name="prevent_change_cash">
                                <label for="prevent_change_cash"><?php print CommonLanguage::getMessage('prevent_change_cash'); ?></label>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('sales_account'); ?></label>
                            <input type="hidden" id="account_id" name="account_id" class="full-width" autocomplete="off" >
                        </div>
                        <div class="col-md-3">
                            <div class="checkbox check-primary ">
                                <input type="checkbox" id="prevent_change_sales" name="prevent_change_sales">
                                <label for="prevent_change_sales"><?php print CommonLanguage::getMessage('prevent_change_sales'); ?></label>
                            </div>
                        </div>

                    </div>

                    <div class="row form-group m-t-10">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('Depot'); ?></label>
                            <input type="hidden" id="depot_id" name="depot_id" class="full-width" autocomplete="off" >
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('depot_options'); ?></label>
                            <input type="hidden" id="depot_options_ids" name="depot_options_ids" class="full-width" autocomplete="off" >
                        </div>


                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('pricing_class'); ?></label>
                            <input type="hidden" id="class_id" name="class_id" class="full-width" autocomplete="off" >
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('class_options'); ?></label>
                            <input type="hidden" id="class_options_ids" name="class_options_ids" class="full-width" autocomplete="off" >
                        </div>

                    </div>
                    <div class="row form-group m-t-10">
                        <div class="col-md-3">
                            <div class="checkbox check-primary ">
                                <input type="checkbox" id="prevent_change_class" name="prevent_change_class">
                                <label for="prevent_change_class"><?php print CommonLanguage::getMessage('prevent_change_class'); ?></label>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('customer'); ?></label>
                            <input type="hidden" id="customer_id" name="customer_id" class="full-width" autocomplete="off" >
                        </div>
                        <div class="col-md-3">
                            <div class="checkbox check-primary ">
                                <input type="checkbox" id="prevent_change_cust" name="prevent_change_cust">
                                <label for="prevent_change_cust"><?php print CommonLanguage::getMessage('prevent_change_cust'); ?></label>
                            </div>
                        </div>

                    </div>
                    <div class="row form-group m-t-10">
                        <div class="col-md-3">
                            <div class="checkbox check-primary ">
                                <input type="checkbox" id="is_full_paid" name="is_full_paid">
                                <label for="is_full_paid"><?php print CommonLanguage::getMessage('is_full_paid'); ?></label>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="checkbox check-primary ">
                                <input type="checkbox" id="hide_payment" name="hide_payment">
                                <label for="hide_payment"><?php print CommonLanguage::getMessage('hide_payment'); ?></label>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="checkbox check-primary ">
                                <input type="checkbox" id="show_fields" name="show_fields">
                                <label for="show_fields"><?php print CommonLanguage::getMessage('show_fields'); ?></label>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <label><?php echo CommonLanguage::getMessage('type'); ?></label>
                            <select id="record_types" name="type" class="full-width" data-init-plugin="select2" >
                                <option value="inv"><?php echo CommonLanguage::getMessage('sales_invoice') ?></option>
                                <option value="pur"><?php echo CommonLanguage::getMessage('purchase_invoice') ?></option>
                                <option value="pnq"><?php echo CommonLanguage::getMessage('purchase_no_qty') ?></option>
                                <option value="oin"><?php echo CommonLanguage::getMessage('sales_orders') ?></option>
                                <option value="opr"><?php echo CommonLanguage::getMessage('purchase_orders') ?></option>
                                <option value="dlc"><?php echo CommonLanguage::getMessage('invoice_delivery_note') ?></option>
                                <option value="dls"><?php echo CommonLanguage::getMessage('purchase_delivery_note') ?></option>
                            </select>
                        </div>

                    </div>
                    <div class="row form-group m-t-10">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('customer_group'); ?></label>
                            <input type="hidden" id="customer_group_id" name="customer_group_id" class="full-width" autocomplete="off" >
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('supplier_group'); ?></label>
                            <input type="hidden" id="supplier_group_id" name="supplier_group_id" class="full-width" autocomplete="off" >
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('salesman'); ?></label>
                            <input type="hidden" id="salesman_ids" name="salesman_ids" class="full-width" autocomplete="off" >
                        </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_invoice_type"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('invoice_type_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        build_select2($("#account_id,#cash_account_id"), "xhr/accIDSearch?type=Payment");
        build_select2($("#depot_id"), "xhr/depotSearch");
        build_select2($("#depot_options_ids"), "xhr/depotSearch", 1);
        build_select2($("#class_id"), "xhr/PricingClassSearch?type=0")
        build_select2($("#class_options_ids"), "xhr/PricingClassSearch?type=0", 1)
        build_select2($("#salesman_ids"), "xhr/employeeSearch", 1)
        build_select2($("#customer_id"), "xhr/customerSearch")
        build_select2($("#customer_group_id"), "xhr/general_group_Search?group_type=cust");
        build_select2($("#supplier_group_id"), "xhr/general_group_Search?group_type=supp");

        RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/invoice_type_list');
        $(".add_invoice_type").on("click", function () {
            $('#is_shown_menu,#prevent_change_cash,#prevent_change_sales,#prevent_change_class,#prevent_change_cust,#is_full_paid,#hide_payment,#show_fields').prop('checked', false)
            $('#invoice_type_modal .add-label').removeClass('hidden')
            $("#invoice_type_id").val(0);
        });
        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            IncludeDeleteModal(common_delete_modal, {invoice_type_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/invoice_type_list');
            }, "InventoryAjax/deleteInvoiceType");
        });
        $("#tableWithSearch").on("click", ".edit", function () {
            $('#invoice_type_modal .edit-label').removeClass('hidden')
            var invoice_type_id = $(this).parents('div.Action').attr('data-id');
            $("#invoice_type_id").val(invoice_type_id);
            CommonViewById('InventoryAjax/get_invoice_type_by_id', {entry_id: invoice_type_id},
                    '#invoice_type_loader', function (response_data) {
                        var response = response_data;
                        $("input[name=is_shown_menu]").prop('checked', response.show_on_menu == "1" ? true : false);
                        $("input[name=is_full_paid]").prop('checked', response.is_full_paid == "1" ? true : false);
                        $("input[name=prevent_change_cust]").prop('checked', response.prevent_change_cust == "1" ? true : false);
                        $("input[name=prevent_change_cash]").prop('checked', response.prevent_change_cash == "1" ? true : false);
                        $("input[name=prevent_change_sales]").prop('checked', response.prevent_change_sales == "1" ? true : false);
                        $("input[name=prevent_change_class]").prop('checked', response.prevent_change_class == "1" ? true : false);
                        $("input[name=hide_payment]").prop('checked', response.hide_payment == "1" ? true : false);
                        $("input[name=show_fields]").prop('checked', response.show_fields == "1" ? true : false);
                        $("#sort_order").val(response.sort_order);
                        $("#invoice_type_name").val(response.invoice_type_name);
                        $("#invoice_type_name_actual").val(response.invoice_type_name_actual);
                        var sales_account = (response.Account_Name != '' ? response.Account_Name : '') + (response.Account_Code != '' && sales_account != '' ? ' - ' : '') + (response.Account_Code != '' ? response.Account_Code : '');

                        $("#account_id").val(response.account_id).attr('data-select-name', sales_account).trigger('change');
                        var cash_account = (response.cash_account_name != '' ? response.cash_account_name : '') + (response.cash_account_code != '' && cash_account != '' ? ' - ' : '') + (response.cash_account_code != '' ? response.cash_account_code : '');

                        $("#cash_account_id").val(response.cash_account_id).attr('data-select-name', cash_account).trigger('change');
                        var depot_name = (response.depot_code != '' ? response.depot_code : '') + (response.depot_name != '' && depot_name != '' ? ' - ' : '') + (response.depot_name != '' ? response.depot_name : '');
                        $("#depot_id").val(response.depot_id).attr('data-select-name', depot_name).trigger('change');
                        $("#customer_group_id").val(response.customer_group_id == 0 ? '' : response.customer_group_id).attr('data-select-name', response.customer_group_name).trigger('change');
                        $("#supplier_group_id").val(response.supplier_group_id == 0 ? '' : response.supplier_group_id).attr('data-select-name', response.supplier_group_name).trigger('change');
                        $("#record_types").val(response.type).select2()
                        $("#class_id").val(response.class_id).attr('data-select-name', response.class_name).trigger('change');
                        $("#customer_id").val(response.customer_id).attr('data-select-name', response.customer_name).trigger('change');
                        $('#depot_options_ids').select2('val', [])
                        $('#depot_options_ids').data().select2.updateSelection(response.depot_arr);
                        $('#class_options_ids').select2('val', [])
                        $('#class_options_ids').data().select2.updateSelection(response.class_arr);
                        $('#salesman_ids').select2('val', [])
                        $('#salesman_ids').data().select2.updateSelection(response.emp_arr);
                    });

        });
        $("#submit_invoice_type").on("click", function () {
            if (!$("#invoice_type_form").valid())
                return;
            $("#invoice_type_loader").removeClass('hidden');
            var Data = new FormData(document.getElementById("invoice_type_form"));
            Data.append('show_on_menu', $('#is_shown_menu').is(':checked') ? 1 : 0)
            Data.append('is_full_paid', $('#is_full_paid').is(':checked') ? 1 : 0)
            Data.append('prevent_change_cust', $('#prevent_change_cust').is(':checked') ? 1 : 0)
            Data.append('show_fields', $('#show_fields').is(':checked') ? 1 : 0)
            Data.append('hide_payment', $('#hide_payment').is(':checked') ? 1 : 0)
            Data.append('prevent_change_class', $('#prevent_change_class').is(':checked') ? 1 : 0)
            Data.append('prevent_change_sales', $('#prevent_change_sales').is(':checked') ? 1 : 0)
            Data.append('prevent_change_cash', $('#prevent_change_cash').is(':checked') ? 1 : 0)
            $.ajax({
                url: "InventoryAjax/submitInvoiceType",
                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], 'InventoryAjax/invoice_type_list', 1);
                            hideModalAlert('invoice_type_modal', 'invoice_type_alert');
                        }
                        AddAlert("#invoice_type_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#invoice_type_loader").addClass('hidden');
                }
            });
        });
    });
</script>