<?php // echo smc_session()['stop_branch_prefix'];die(); ?>
<style>
    .table thead tr >th{
        background-color: #4c0870;
        color:#fff;
    }
    .table thead tr th[class*='sorting_'] {
        color: #FFFFFF;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd; 

    }
    .btn-default{
        border: none;
        background-color: #eee;
    }
    .btn-default:hover{
        background-color:#ed9421 !important;
        color:#fff;
        border: none;
    }

</style>
<div id="Barcode" style="display:none"></div>


<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('org_branches'); ?></div>

            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <?php
            if (smc_session()['default_setting']['sync_type'] != 2) {
                echo Forms::FloatingAddButton('4c0870', '#addOrgBranch', CommonLanguage::getMessage('add'));
            }
            ?>
            <table class="table table-bordered table-hover demo-table-search" id="tableWithSearch" >
                <thead>
                    <tr>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('branch'); ?></th>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('prefix'); ?></th>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('Action'); ?></th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>



    </div>
</div>
<div class="modal fade" data-backdrop="static" id="addOrgBranch" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width:50%">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><i class="pg-close fa-22"></i></span></button>
                <h4 class="modal-title"><?php print CommonLanguage::getMessage('org_branches') ?></h4>
                <div id="add_org_branch_alert"></div>
            </div>
            <div class="modal-body">
                <form id="AssignUserForm">
                    <div class="row">
                        <div class=" col-md-12 m-t-10 " >
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('Name'); ?><span class="red-star">*</span></label>

                                <input class="form-control full-width" id="branch_name" name="branch_name" type="text"  />
                            </div>
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('prefix'); ?></label>
                                <input class="form-control full-width" id="branch_prefix" <?php echo smc_session()['stop_branch_prefix'] ? 'readonly' : '' ?> name="branch_prefix" type="text"  />
                            </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" class="btn bg-complete white" id="add_org_branch" style="width:auto"><?php print CommonLanguage::getMessage('Save') ?></button>
            </div>
        </div>
    </div>
</div>

<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('user_accounts')); ?>,
            Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>;
    var IsEdit = false, Branch_ID = 0;
    $(function () {


        RebuildDataTable(title, [0, 1], "ACCAjax/org_branches")
        $(".floatingContainer").on("click", function () {
            IsEdit = false;
        })


        $("#add_org_branch").on("click", function () {
            $("#add_org_branch_alert").removeClass('hidden')
            if ($("#branch_name").val() == '') {
                $("#add_org_branch_alert").attr("class", "alert alert-danger").html(Required_Fields)
                return;
            }
            var DataArr = {
                branch_name: $("#branch_name").val(),
                branch_prefix: $("#branch_prefix").val(),
            }
            if (IsEdit) {
                DataArr.branch_id = Branch_ID;
            }

            $.ajax({
                url: IsEdit ? "ACCAjax/edit_org_branch" : "ACCAjax/add_org_branch",
                method: "POST",
                dataType: 'json',
                data: DataArr,
                complete: function (v1, v2) {
                    if (v2 == "success" && v1.responseJSON[0] == 'OK') {
                        $("#add_org_branch_alert").attr("class", "alert alert-success").html(v1.responseJSON[1])
                        RebuildDataTable(title, [0, 1], "ACCAjax/org_branches")
                        setTimeout(function () {
                            $("#add_org_branch_alert").addClass('hidden')
                            $("#addOrgBranch").modal("hide")
                        }, 5000)
                    } else {
                        $("#add_org_branch_alert").attr("class", "alert alert-danger").html(Failed_Submit)
                    }
                }
            })
        })

        $("#tableWithSearch tbody").on('click', '.Edit', function () {
            IsEdit = true;
            $.ajax({
                url: "ACCAjax/getOrgBranch",
                method: "GET",
                dataType: "JSON",
                data: {id: $(this).attr('data-id')},
                complete: function (v1, v2) {
                    var Data = v1.responseJSON;
                    console.log(Data)
                    $("#branch_name").val(Data['branch_name'])
                    $("#branch_prefix").val(Data['branch_prefix'])
                    Branch_ID = Data['branch_id'];
                    $("#addOrgBranch").modal("show")
                }
            })

        })
    })





</script>
