<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDPQfukDXQUdWN4XNMa8asJryBPtJ9iBsQ"></script>
<?php
if ($permissions->hasPermission('newCustomer')) {
    include("Views/Inventory/customer_modal.php");
}
include("Views/Inventory/allergy-intolerance-modal.php");
include("Views/Inventory/disable_enable_modal.php");
include 'Views/Inventory/customer_map_modal.php';
?>
<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('customer_list'); ?></div>
            <div class="row m-t-10">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('salesman'); ?></label>
                    <!-- <?php $Employee_ID  =  smc_session()['user_details']['Employee_ID']; ?> -->
                    <!-- value="<?php echo smc_session()['user_details']['limit_customer_invoice'] == 1 ? $Employee_ID : ''; ?>" data-select-name="<?php echo smc_session()['user_details']['limit_customer_invoice'] == 1 ? smc_session()['user_logged_full_name'] : ''; ?>" -->
                    <input id="salesman" name="salesman" type="hidden" class="full-width salesman " />
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('status'); ?></label>
                    <select id="customer_status" name="customer_status" class="full-width" data-init-plugin="select2">
                        <option value=""><?php echo CommonLanguage::getMessage('All'); ?></option>
                        <option value="1"><?php echo CommonLanguage::getMessage('enabled'); ?></option>
                        <option value="0"><?php echo CommonLanguage::getMessage('disabled'); ?></option>
                    </select>
                </div>

                <div class="col-md-3">
                    <label><?php print CommonLanguage::getMessage('address'); ?></label>
                    <input type="hidden" id="customer_address" name="customer_address" class="full-width">
                </div>
                <?php if (smc_session()['default_setting']['sync_type'] != 2) {
                    echo Forms::FloatingAddButton('4c0870', '#customer_modal', CommonLanguage::getMessage('add'), 'new_customer');
                }
                ?>
        </div>

            <div class="row m-t-10 <?php echo $permissions->hasPermission('Branches') ? '':'hidden'; ?>">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="search_branch_id" type="hidden" class="full-width" name="search_branch_id" >
                </div>
                
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('compare_balance'); ?></label>
                    <select id="compare_balance" name="compare_balance" class="full-width" data-init-plugin="select2">
                        <option value=">"><?php echo CommonLanguage::getMessage('>'); ?></option>
                        <option value="<"><?php echo CommonLanguage::getMessage('<'); ?></option>
                        <option value="="><?php echo CommonLanguage::getMessage('='); ?></option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label><?php print CommonLanguage::getMessage('choose_balance'); ?></label>
                    <input type="text" placeholder="<?php print CommonLanguage::getMessage('1'); ?>" id="search_balance" name="search_balance" class="form-control " required>
                </div>

            </div>
        </div>

        <div class="panel-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered inventory " id="tableWithSearch">
                            <thead>
                                <tr>
                                    <th class="text-center" style="width:5%"><?php print CommonLanguage::getMessage('Code'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('Name'); ?></th>                                   
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('branch_name'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('company'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('total_invoices'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('balance'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('Phone'); ?></th>
                                    <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('address'); ?></th>
                                    <th class="text-center" style="width:15%"><?php print CommonLanguage::getMessage('status'); ?></th>
                                    <th class="text-center no-print" style="width:10%"><?php print CommonLanguage::getMessage('Action'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="DeleteFromList">
    <div class="modal-dialog modal-sm">
        <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 bold text-left"><?php echo CommonLanguage::getMessage('Confirm_Delete'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row">
                    <?php echo Forms::AlertDiv('DeleteCustomer_modalAlert'); ?>
                </div>
                <div class="row">
                    <div class="col-md-12 ">
                        <div class="col-md-12" style="margin-top: 20px;">
                            <p><?php print CommonLanguage::getMessage('About_Delete') ?><strong class="Name"></strong></p>
                            <p><?php print CommonLanguage::getMessage('Proceed_Delete') ?></p>
                        </div>
                    </div>
                </div>
            </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-danger width-auto" id="Customer_Delete"><?php print CommonLanguage::getMessage('delete') ?></button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="change-customer-password-modal" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content-wrapper">
            <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"><?php echo CommonLanguage::getMessage('change_password'); ?></h5>
                </div>
                <div class="modal-body">
                    <form id="change-customer-password-form" action="javascript:void(0);">
                        <?php echo Forms::AlertDiv('change_customer_password_alert'); ?>
                        <div class="form-group-attached">
                            <div class="row">
                                <input type="hidden" name="current_customer_id" id="current_customer_id" />
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class=""><?php echo CommonLanguage::getMessage('password'); ?></label>
                                        <span class="red-star">*</span>
                                        <input class="current_customer_password full-width form-control empty" id="current_customer_password" name="current_customer_password" type="password" required />
                                    </div>
                                </div>
                            </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" id="submit_customer_password" class="btn btn-primary "><?php print CommonLanguage::getMessage('Save'); ?></button>
                </div>
                <?php echo Forms::ModalLoader('changeCustomerPasswordDisabler'); ?>
            </div>
        </div>
    </div>
</div>
<iframe id="barcode_frame" name="ifrm" style="width:0;height:0;visibility: hidden;"></iframe>
<script>
    var CurentCustomer_ToDelete;
    var Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
        is_new = <?php echo json_encode($is_new); ?>,
        title = <?php echo json_encode(CommonLanguage::getMessage('customer_list')); ?>;
    $(function() {
        build_select2($("#customer_address"), "xhr/customerAddressSearch");
        build_select2($("#search_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");

        RebuildDataTable(title, [0, 1, 2, 3, 4, 5, 6], 'InventoryAjax/CustomerListRequest', {
            salesman_id: $('#salesman').val(),
            status_id: $('#customer_status').val(),
            customer_address: $("#customer_address").val(),
            search_branch_id: $("#search_branch_id").val(),
        });
        console.log('is_new')
        console.log(is_new)
        if (is_new == 1) {
            $(".new_customer a").trigger("click");
        }
        $('#tableWithSearch').on('click', '.disable_enable_item', function() {
            var status_id = $(this).attr('data-id'),
                target_item_value = $(this).parents('div.Action').attr('data-id');
            $('#current_status_id').val(status_id == 1 ? 0 : 1);
            $('#target_item_value').val(target_item_value);
            $('#target_item').val('customers');
            $('#target_item_id').val('Customer_ID');
            status_id == 0 ? $('#enable-disable-modal .add-label,#enable-disable-modal .about_enable_item').removeClass('hidden') : $('#enable-disable-modal .edit-label,#enable-disable-modal .about_disable_item').removeClass('hidden');

        });

        build_select2($("#salesman"), "xhr/employeeSearch");
        $("#salesman,#customer_status,#customer_address,#search_branch_id,#search_balance").on('change', function() {
            RebuildDataTable(title, [0, 1, 2, 3, 4, 5, 6], 'InventoryAjax/CustomerListRequest', {
                salesman_id: $('#salesman').val(),
                status_id: $('#customer_status').val(),
                customer_address: $("#customer_address").val(),
                search_branch_id: $("#search_branch_id").val(),
                compare_balance: $("#compare_balance").val(),
                search_balance: $("#search_balance").val(),
            });
        });
        /*To delete a customer or vendor*/
        var CurentCustomer_ToDelete;
        $("#tableWithSearch").on("click", ".Action .Delete", function() {
            //alert("OK")
            var me = $(this).parents("tr");
            var name = me.find("td:eq(1)").text();
            $("#DeleteFromList").find(".Name").text(name).end().modal("show")
            CurentCustomer_ToDelete = $(this).parents("tr").find('.Action').attr('data-id')
        });
        $("#tableWithSearch").on('click', ".print_qrcode", function() {
            var data = {
                customer_id: $(this).attr("data-id"),
                current_value: $(this).attr("data-code")
            };
            $('#barcode_frame').attr('src', "InventoryAjax/print_customer_qrcode?" + $.param(data));
            //            $.ajax({
            //                url: "InventoryAjax/print_customer_qrcode",
            //                data: {customer_id:$(this).parents(".Action").attr("data-id")},
            //                complete: function (v1, v2) {
            //
            //                }
            //            })
        })
        $("#tableWithSearch").on('click', ".change_customer_password", function() {
            $("#current_customer_id").val($(this).parents('div.Action').attr('data-id'))
        });
        $("#Customer_Delete").on("click", function() {
            $.ajax({
                type: 'GET',
                url: 'InventoryAjax/deleteCustomerAjax',
                data: {
                    GetCustomerID: CurentCustomer_ToDelete
                },
                complete: function(data, vv) {
                    if (vv == "success") {
                        if (data.responseText == 'OK') {
                            AddAlert("#DeleteCustomer_modalAlert", Submitted_Successfully, 'success');
                            RebuildDataTable(title, [0, 1, 2, 3, 4, 5, 6], 'InventoryAjax/CustomerListRequest', {
                                salesman_id: $('#salesman').val(),
                                status_id: $('#customer_status').val(),
                                customer_address: $("#customer_address").val()
                            });
                        } else {
                            AddAlert("#DeleteCustomer_modalAlert", data.responseText, 'danger');
                        }
                        hideModalAlert('DeleteFromList', 'DeleteCustomer_modalAlert');
                    }
                }
            });
        });
        $('#submit_customer_password').on('click', function() {
            if (!$("#change-customer-password-form").valid())
                return;
            var Data = new FormData(document.getElementById("change-customer-password-form"));
            $("#changeCustomerPasswordDisabler").removeClass('hidden');
            $.ajax({
                url: "InventoryAjax/updateCustomerPassword",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function(jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable(title, [0, 1, 2, 3, 4, 5, 6], 'InventoryAjax/CustomerListRequest', {
                                salesman_id: $('#salesman').val(),
                                status_id: $('#customer_status').val(),
                                customer_address: $("#customer_address").val()
                            });
                            hideModalAlert('change-customer-password-modal', 'change_customer_password_alert');
                        }
                        AddAlert("#change_customer_password_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#changeCustomerPasswordDisabler").addClass('hidden');
                }
            });
        });
    });
</script>
