<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"> <?php print CommonLanguage::getMessage('shipping_companies'); ?></div>
            <?php
            if ($permissions->hasPermission('add_shipping_company')) {
                echo Forms::FloatingAddButton('4c0870', '#shipping_company_modal', CommonLanguage::getMessage('add'));
            }
            ?>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('shipping_company_name'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('shipping_account'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('shipping_fees'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade bs-example-modal-md " data-backdrop="static" id="shipping_company_modal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-md"  >
        <div class="modal-content">
            <div class="modal-header clearfix 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_shipping_company'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_shipping_company'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('shipping_alert'); ?></div>
                <form id="shipping_company_form">
                    <input type="hidden" name="shipping_company_id" id="shipping_company_id" />
                    <div class="row form-group">
                        <div class=" col-md-12  " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('shipping_company_name'); ?>
                                <span class="red-star">*</span>
                            </label>
                            <input type="text" class="form-control" autocomplete="off" id="shipping_company_name" name="shipping_company_name" required/>
                        </div>

                    </div>
                    <div class="row form-group">
                        <div class=" col-md-6  " style="display: grid;" >
                            <label class="labelText"><?php print CommonLanguage::getMessage('is_invoice_total_on_shipping'); ?></label>
                            <input type="checkbox"  id="is_invoice_total_on_shipping" />
                        </div>
                        <div class=" col-md-6  " style="display: grid;" >
                            <label class="labelText"><?php print CommonLanguage::getMessage('generate_trans_on_status'); ?></label>
                            <input type="checkbox"  id="trans_on_change_status" />
                        </div>

                    </div>
                    <div class="row form-group">
                        <div class=" col-md-6 " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('api'); ?>
                            </label>
                            <input type="text" class="form-control" id="api_folder" name="api_folder" />
                        </div>
                        <div class=" col-md-6 " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('api_version'); ?>
                            </label>
                            <input type="text" class="form-control" id="api_version" name="api_version" />
                        </div>
                    </div>

                    <div class="row form-group">
                        <div class=" col-md-6  " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('shipping_account'); ?>
                            </label>
                            <span class="fa fa-plus-circle openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#6c5ab3;cursor:pointer"></span>
                            <input type="hidden" class="full-width" id="shipping_account_id" name="shipping_account_id"/>
                        </div>
                        <div class=" col-md-6  " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('shipping_fees'); ?>
                            </label>
                            <input type="number" class="form-control" id="shipping_company_fees" name="shipping_company_fees" />
                        </div>
                    </div> 
                    <div class="row form-group">
                        <div class=" col-md-6  " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('sales_account'); ?>
                            </label>
                            <span class="fa fa-plus-circle openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#6c5ab3;cursor:pointer"></span>
                            <input type="hidden" class="full-width" id="sales_account_id" name="sales_account_id"/>
                        </div>
                        <div class=" col-md-6  " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('main_account'); ?>
                            </label>
                            <span class="fa fa-plus-circle openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#6c5ab3;cursor:pointer"></span>
                            <input type="hidden" class="full-width" id="main_account_id" name="main_account_id"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class=" col-md-6  " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('user_name'); ?>
                            </label>
                            <input type="text" class="form-control" id="user_name" name="user_name" autocomplete="off"/>
                        </div>
                        <div class=" col-md-6  " >
                            <label class="labelText"><?php print CommonLanguage::getMessage('password'); ?>
                            </label>
                            <input type="text" class="form-control" id="password" name="password" autocomplete="off"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-12">
                            <label class="labelText"><?php print CommonLanguage::getMessage('token'); ?>
                            </label>
                            <input type="text" class="form-control" id="token"  name="token" autocomplete="off"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-12">
                            <label class="labelText"><?php print CommonLanguage::getMessage('note'); ?>
                            </label>
                            <textarea class="form-control" name="note" id="note" autocomplete="off"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close') ?></button>
                <button type="button" class="btn btn-primary white" id="submit_shipping_company"><?php print CommonLanguage::getMessage('Save') ?></button>
            </div>
            <?php echo Forms::ModalLoader('shipping_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], "InventoryAjax/shipping_company_ajax");
        build_select2($('#shipping_account_id,#sales_account_id,#main_account_id'), "xhr/allAccountsSearch");
        $(".floatingContainer").on("click", function () {
            $('#shipping_company_id').val(0);
            $('#shipping_company_modal .add-label').removeClass('hidden');
        });

        $("#is_invoice_total_on_shipping,#trans_on_change_status").on("click", function () {
            if ($(this).is(":checked")) {
                if ($(this).attr("id") == 'is_invoice_total_on_shipping')
                    $("#trans_on_change_status").prop("checked", false)
                else
                    $("#is_invoice_total_on_shipping").prop("checked", false)
            }
        })

        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id');
            IncludeDeleteModal(common_delete_modal, {shipping_company_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], "InventoryAjax/shipping_company_ajax")
            }, "InventoryAjax/deleteShippingCompany");
        });
        $('#tableWithSearch').on('click', '.edit', function () {
            $('#shipping_company_modal .edit-label').removeClass('hidden');
            $('#shipping_company_id').val($(this).parents('div.Action').attr('data-id'));
            CommonViewById('InventoryAjax/getShippingCompanyData', {shipping_company_id: $('#shipping_company_id').val()},
                    '#shipping_loader', function (response) {
                        $('#api_folder').val(response.api_folder);
                        $('#api_version').val(response.api_version);
                        $('#shipping_company_name').val(response.shipping_company_name);
                        $('#shipping_company_fees').val(response.shipping_company_fees);
                        $('#user_name').val(response.user_name);
                        $('#password').val(response.password);
                        $('#token').val(response.token);
                        $('#note').val(response.note);
                        $('#shipping_account_id').val(response.shipping_account_id);
                        $('#shipping_account_id').attr('data-select-name', response.shipping_account_name);
                        $('#sales_account_id').val(response.sales_account_id);
                        $('#sales_account_id').attr('data-select-name', response.sales_account_name);
                        $('#main_account_id').val(response.main_account_id);
                        $('#main_account_id').attr('data-select-name', response.main_account_name);
                        $('#is_invoice_total_on_shipping').prop('checked', response.is_invoice_total_on_shipping == 1 ? true : false)
                        $('#trans_on_change_status').prop('checked', response.trans_on_change_status == 1 ? true : false)
                        build_select2($('#shipping_account_id,#sales_account_id,#main_account_id'), "xhr/allAccountsSearch");
                    });
        });
        $("#submit_shipping_company").on("click", function () {
            if (!$("#shipping_company_form").valid())
                return;
            var Data = new FormData(document.getElementById("shipping_company_form"));
            Data.append('is_invoice_total_on_shipping', $('#is_invoice_total_on_shipping').is(":checked") ? 1 : 0)
            Data.append('trans_on_change_status', $('#trans_on_change_status').is(":checked") ? 1 : 0)
            $("#shipping_loader").removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/submitShippingCompany",
                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/shipping_company_ajax");
                            hideModalAlert('shipping_company_modal', 'shipping_alert');
                        }
                        AddAlert("#shipping_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#shipping_loader").addClass('hidden');
                }
            });
        });
    });
</script>