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

            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <?php
            echo Forms::FloatingAddButton('4c0870', '#assignUserAccount', 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('Name'); ?></th>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('cash_account'); ?></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 bs-example-modal-md " id="assignUserAccount" 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('user_accounts') ?></h4>
                <div id="assignUserAlert"></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'); ?></label>

                                <select  id="users"  class="full-width Account_Code " data-init-plugin="select2">
                                    <?php foreach ($users as $user): ?>
                                        <option value="<?php echo $user['User_ID']; ?>"><?php echo $user['User_Name']; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>


                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('cash_account'); ?></label>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#4c0870;cursor:pointer"></span>

                                <input class=" full-width" id="cash_account" name="cash_account" type="hidden"  />

                            </div>


                        </div>
                        <div class=" col-md-12 m-t-10 " >
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('purchase_account'); ?></label>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#4c0870;cursor:pointer"></span>

<!--                                <select  id="purchase_account"  class="full-width purchase_account " data-init-plugin="select2">
                                <?php foreach ($purchase_account as $acc): ?>
                                                        <option value="<?php echo $acc['Account_Code']; ?>"><?php echo $acc['Account_Code'] . ' - ' . $acc['Account_Name']; ?></option>
                                <?php endforeach; ?>
                                </select>-->
                                <input class=" full-width" id="purchase_account" name="purchase_account" type="hidden"  />
                            </div>


                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('purchase_ship'); ?></label>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#4c0870;cursor:pointer"></span>

<!--                                <select  id="purchase_shipping"  class="full-width purchase_shipping " data-init-plugin="select2">
                                <?php foreach ($purchase_account as $acc): ?>
                         <option value="<?php echo $acc['Account_Code']; ?>"><?php echo $acc['Account_Code'] . ' - ' . $acc['Account_Name']; ?></option>
                                <?php endforeach; ?>
     </select>-->
                                <input class=" full-width" id="purchase_shipping" name="purchase_shipping" type="hidden"  />

                            </div>


                        </div>

                        <div class=" col-md-12 m-t-10 " >
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('sales_account'); ?></label>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#4c0870;cursor:pointer"></span>

<!--                                <select  id="sales_account"  class="full-width sales_account " data-init-plugin="select2">
                                <?php foreach ($sales_account as $acc): ?>
                                                    <option value="<?php echo $acc['Account_Code']; ?>"><?php echo $acc['Account_Code'] . ' - ' . $acc['Account_Name']; ?></option>
                                <?php endforeach; ?>
                                </select>-->
                                <input class=" full-width" id="sales_account" name="sales_account" type="hidden"  />

                            </div>


                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('sales_ship'); ?></label>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#4c0870;cursor:pointer"></span>

<!--                                <select  id="sales_shipping"  class="full-width sales_shipping " data-init-plugin="select2">
                                <?php foreach ($sales_account as $acc): ?>
                                                    <option value="<?php echo $acc['Account_Code']; ?>"><?php echo $acc['Account_Code'] . ' - ' . $acc['Account_Name']; ?></option>
                                <?php endforeach; ?>
                                </select>-->
                                <input class=" full-width" id="sales_shipping" name="sales_shipping" type="hidden"  />

                            </div>


                        </div>
                        <div class=" col-md-12 m-t-10 " >
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('withdrawal_account'); ?></label>
                                <select  id="withdrawal_accounts"  class="full-width Account_Code " data-init-plugin="select2">
                                    <?php foreach ($deposit_account as $acc): ?>
                                        <option value="<?php echo $acc['Account_ID']; ?>"><?php echo $acc['Account_Name'] . ' - ' . $acc['Account_Code']; ?></option>
                                    <?php endforeach; ?>
                                </select>

                            </div>
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('deposit_account'); ?></label>
                                <select  id="deposit_accounts"  class="full-width Account_Code " data-init-plugin="select2">
                                    <?php foreach ($deposit_account as $acc): ?>
                                        <option value="<?php echo $acc['Account_ID']; ?>"><?php echo $acc['Account_Name'] . ' - ' . $acc['Account_Code']; ?></option>
                                    <?php endforeach; ?>
                                </select>

                            </div>
                        </div>
                        <div class=" col-md-12 m-t-10 " >
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('other_accounts'); ?></label>
                                <select  id="Other_Account"  class="full-width Account_Code " data-init-plugin="select2">
                                    <?php foreach ($other_accounts as $acc): ?>
                                        <option value="<?php echo $acc['Account_ID']; ?>"><?php echo $acc['Account_Name'] . ' - ' . $acc['Account_Code']; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('cheque_account'); ?></label>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#4c0870;cursor:pointer"></span>
                                <input class=" full-width" id="cheque_account" name="cheque_account" type="hidden"  />

                            </div>
                        </div>
                        <div class=" col-md-12 m-t-10 " >
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('pricing_class'); ?></label>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/GeneralSetting/1?is_pop=1" style="color:#4c0870;cursor:pointer"></span>
                                <input class=" full-width" id="default_class" name="default_class" type="hidden"  />

                            </div>


                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('prefix'); ?></label>
                                <input class=" form-control full-width" id="user_prefix" <?php echo smc_session()['stop_user_prefix'] ? 'readonly' : '' ?> name="user_prefix" type="text"  />

                            </div>


                        </div>
                        <div class=" col-md-12 m-t-10 " >
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('Depot'); ?></label>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Stock/ListDepot?is_pop=1" style="color:#4c0870;cursor:pointer"></span>
                                <input class=" full-width" id="default_depot" name="default_depot" type="hidden"  />

                            </div>
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('branch'); ?></label>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="ACC/org_branches?is_pop=1" style="color:#4c0870;cursor:pointer"></span>
                                <input class=" full-width" id="default_branch" name="default_branch" type="hidden"  />

                            </div>

                        </div>
                        <div class=" col-md-12 m-t-10 " >


                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('max_discount'); ?></label>
                                <input class=" form-control full-width" id="max_discount" name="max_discount" type="number" step="any" max="100" min="0" />

                            </div>
                            <div class="col-md-6">
                                <label class="labelText"><?php print CommonLanguage::getMessage('edit_price'); ?></label>
                                <input id="edit_price" name="edit_price" type="checkbox"  />

                            </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="assignBtn" style="width:auto"><?php print CommonLanguage::getMessage('Save') ?></button>
            </div>
        </div>
    </div>
</div>

<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('user_accounts')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>;
//    var IsEdit = false;
    $(function () {
                new Switchery($("#edit_price").get(0), {color: $.Pages.getColor("complete")})

        build_select2($("#cash_account"), ("xhr/accIDSearch?type=cash"))
        build_select2($("#cheque_account"), ("xhr/accIDSearch?type=check"))
        build_select2($("#purchase_account"), ("xhr/accIDSearch?type=purchase"))
        build_select2($("#purchase_shipping"), ("xhr/accIDSearch?type=purchase"))
        build_select2($("#sales_account"), ("xhr/accIDSearch?type=sales"))
        build_select2($("#sales_shipping"), ("xhr/accIDSearch?type=sales"))
        build_select2($("#default_class"), "xhr/PricingClassSearch")
        build_select2($("#default_depot"), "xhr/depotSearch")
        build_select2($("#default_branch"), "xhr/CommonSearch?col_id=branch_id&col_name=branch_name&table=tbl_organization_branches")


        RebuildDataTable(title, [0, 1], "ACCAjax/UserAccAccounts")

        $("#assignBtn").on("click", function () {
            $("#assignUserAlert").removeClass('hidden')
            var DataArr = {
                User_ID: $("#users").val(),
                Cash_Account: $("#cash_account").val(),
                Cheque_Account: $("#cheque_account").val(),
                purchase_account: $("#purchase_account").val(),
                sales_account: $("#sales_account").val(),
                purchase_shipping: $("#purchase_shipping").val(),
                sales_shipping: $("#sales_shipping").val(),
                deposite_account: $("#deposit_accounts").val(),
                withdrawal_Account: $("#withdrawal_accounts").val(),
                Other_Account: $("#Other_Account").val(),
                default_class: $("#default_class").val(),
                default_depot: $("#default_depot").val(),
                default_branch: $("#default_branch").val(),
                user_prefix: $("#user_prefix").val(),
                max_discount: $("#max_discount").val(),
                edit_price : $("#edit_price").is(":checked") ? 1 : 0
            }

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

        $("#tableWithSearch tbody").on('click', '.Edit', function () {
//            IsEdit = true;
            $.ajax({
                url: "ACCAjax/getUserAccounts",
                method: "GET",
                dataType: "JSON",
                data: {id: $(this).attr('data-id')},
                complete: function (v1, v2) {
                    var Data = v1.responseJSON;
                    $("#users").val(Data['User_ID']).select2()
                    $("#cash_account").val(Data['cash_account_id']).attr("data-select-name", Data['c_name'])
                    $("#cheque_account").val(Data['cheque_account_id']).attr("data-select-name", Data['cheque_name'])
                    $("#purchase_account").val(Data['purchase_account_id']).attr("data-select-name", Data['p_name'])
                    $("#sales_account").val(Data['sales_account_id']).attr("data-select-name", Data['s_name'])
                    $("#purchase_shipping").val(Data['pur_ship_account_id']).attr("data-select-name", Data['ps_name'])
                    $("#sales_shipping").val(Data['sales_ship_account_id']).attr("data-select-name", Data['ss_name'])
                    $("#deposit_accounts").val(Data['deposit_account_id']).select2()
                    $("#Other_Account").val(Data['other_account_id']).select2()
                    $("#withdrawal_accounts").val(Data['withdrawal_account_id']).select2()
                    $("#default_class").val(Data['class_id']).attr("data-select-name", Data['Class_Name'])
                    $("#default_depot").val(Data['depot_id']).attr("data-select-name", Data['Depot_Name'])
                    $("#default_branch").val(Data['branch_id']).attr("data-select-name", Data['branch_name'])
                    $("#user_prefix").val(Data['serial_prefix'])
                    $("#max_discount").val(Data['max_discount'])
                    $("#edit_price").prop("checked",Data['edit_price'] ==1 ? true : false)
                    build_select2($("#cash_account"), ("xhr/accIDSearch?type=cash"))
                    build_select2($("#cheque_account"), ("xhr/accIDSearch?type=check"))
                    build_select2($("#purchase_account"), ("xhr/accIDSearch?type=purchase"))
                    build_select2($("#purchase_shipping"), ("xhr/accIDSearch?type=purchase"))
                    build_select2($("#sales_account"), ("xhr/accIDSearch?type=sales"))
                    build_select2($("#sales_shipping"), ("xhr/accIDSearch?type=sales"))
                    build_select2($("#default_class"), "xhr/PricingClassSearch")
                    build_select2($("#default_depot"), "xhr/depotSearch")
                    build_select2($("#default_branch"), "xhr/CommonSearch?col_id=branch_id&col_name=branch_name&table=tbl_organization_branches")
                    $("#assignUserAccount").modal("show")
                }
            })

        })
    })





</script>
