<style>
    .open>.dropdown-menu {
        display: block;
        width: 100%;
    }
    .table thead tr >th{
        background-color:#4c0870;
        color:#fff;
    }
    .table thead tr th[class*='sorting_'] {
        color: #fff;
    }
    .table tbody tr td {
        color:#000;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd;  
    }
    .table-responsive {
        overflow-x: visible;
    }
    .b-r {
        border-left-width: 1px;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd;  
    }
    .WidgetMenu{
        width:140px;
        text-align:center;
        border:1px solid #4c0870;
        padding-bottom:10px !important;
    }
    .WidgetMenu > .Mini{
        display:inline-block !important;
        color:#555;
        padding:0 !important;
    }
    .WidgetMenu > li:not(.Mini){
        display:block !important;
        padding:5px 0;
    }
    .WidgetMenu > li:not(.Caption) > a{
        padding: 3px 8px;
        line-height: normal;
        opacity:0.7;
    }
    .ReportSelector{font-size: 1.2em;}
    .WidgetMenu > .Caption:not(:first-child){
        margin-top:10px;
    }
    .WidgetMenu > .Caption{
        text-align:left;
        font-size: 14px;
        display: block !important;
        padding: 4px;
        text-indent:5px;
        line-height: normal !important;
        color:#ccc !important;
    }
    .WidgetMenu div > .Selected{
        background:#4c0870;
        color:#fff !important;
        -webkit-border-radius:3px;
        -moz-border-radius:3px;
        border-radius:3px;
    }
    .dropdown a{
        color: #4c0870  !important;
    }
    .WidgetMenu div> .Selected >a{color:#fff !important;}
    .ForceShow{display:block}
    .ForceHide{display:none !important;}
    .HideMe{
        display:none !important;
        position:absolute;
        top:-1000px;
    }
    #MyTable .list>li{
        cursor:pointer;
    }
    .alert-dismissible
    {
        margin-top:20px;
        margin-left:58px;
        border: 1px dashed #4c0870;
        background-color: #fff;
        color: #4E5E6A;
        font-size:16px;
        width: 50%;
    }
    body{
        overflow:auto
    }
</style>
<div class="wrapper">
    <div class="panel invoice">
        <div class="panel-body">
            <div class="row">
                <h1><?php print CommonLanguage::getMessage('general_setting'); ?></h1>
                <div class="col-md-12 input-group m-b" style="padding-bottom: 15px;">
                    <div class="col-md-3">
                        <select required id="Setting" name="Setting" class="full-width " data-init-plugin="select2">
                            <?php foreach ($allSetting as $setting) { ?>
                            <option value="<?php echo $setting['ID']; ?>" <?php echo $setting['ID'] == $id ? "selected" : "" ?> data-table="<?php echo $setting['Table_Name']; ?>" data-col="<?php echo $setting['Column_ID']; ?>" data-ColName="<?php echo $setting['Column_Name'] ?>" data-name="<?php echo $setting['Name']; ?>"><?php echo CommonLanguage::getMessage($setting['Name']); ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>
                <div class="HideMe" id="AjaxIsBusy" style="position:absolute;left:0;right:0;top:0px;height:100%;background:rgba(255,255,255,0.9);z-index:9999;opacity:1"><img class="image-responsive-height demo-mw-50" src="img/demo/progress.svg" alt="Progress" width="200px" style="position: absolute;left: 50%;top: 50%;margin: -25px 0 0 -25px;" /></div>
                <?php echo Forms::FloatingAddButton('4c0870', '#EditSettingEntry', CommonLanguage::getMessage('add')); ?> 
                <div class="col-md-12" id="MyTable" >
                    <table class="table table-bordered table-hover demo-table-search" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('Name'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
                <br>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="DeleteFromList"  data-backdrop="static">
    <div class="modal-dialog modal-sm" >
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title"><?php print CommonLanguage::getMessage('Confirm_Delete'); ?></h4>
                <div id="Delete_modalAlert"><span></span></div>
            </div>
            <div class="modal-body">
                <div class="row m-t-10">
                    <div class="col-md-12">
                        <p><?php print CommonLanguage::getMessage('Proceed_Delete'); ?></p>
                    </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" id="ConfirmDelete" style="width:auto"><?php print CommonLanguage::getMessage('delete'); ?></button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade  " id="EditSettingEntry" data-backdrop="static">
    <div class="modal-dialog  modal-sm">
        <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-14"></i></button>
                <h5><span class="semi-bold" id="modalTitle"><?php print CommonLanguage::getMessage('add'); ?></span></h5>
                <div id="EditSettingEntryAlert"></div>
            </div>
            <div class="modal-body">
                <form id="EditSettingEntryPostForm" method="POST" onsubmit="return false">
                    <div class="row">
                        <div class="col-md-12 input-group">
                            <span class="input-group-addon primary ">
                                <i class="fa fa-ship inputIcon" ></i>
                            </span>
                            <input type="text"  id="LastEntry" name="LastEntry" class="form-control " required>
                        </div>
                    </div>
                    <!--                    <div class="row">
<div class="col-md-12" >
<div id="CurrencyIDContainer">
<div class="form-group form-material floating">
1 is to return only default currencies
<?php echo Forms::CurrencyDropDown(1, 'primary', 'Currency_ID', '1'); ?>
</div>
</div>
</div>
</div>-->
                </form>
            </div>
            <div class="modal-footer">
                <div class="pull-right">
                    <button class="btn btn-default" id="CloseEdit"   > <?php print CommonLanguage::getMessage('Close'); ?> </button>
                    <button class="btn btn-primary" id="SubmitEntry"  >  <?php print CommonLanguage::getMessage('Save'); ?>  </button>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
        Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            Data_Exists = <?php echo json_encode(CommonLanguage::getMessage('Data_Exists')); ?>,
                AddLang = <?php echo json_encode(CommonLanguage::getMessage('add')); ?>,
                    EditLang = <?php echo json_encode(CommonLanguage::getMessage('Edit')); ?>,
                        title = <?php echo json_encode(CommonLanguage::getMessage('general_setting')); ?>,
                            sync_type = <?php echo json_encode(smc_session()['default_setting']['sync_type']); ?>,
                                Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>;
                                var ToDelete, TRtoEdit, IfEdit = false;
    $(function () {
        rebuild_table()
        $(".floatingContainer ").on("click", function () {
            $("#modalTitle").html(AddLang)
            //            if($("#Setting").val() != 10){
            //                $("#CurrencyIDContainer").addClass('hidden')
            //            }else{
            //                $("#CurrencyIDContainer").removeClass('hidden')
            //            }
        })
        $('#Setting').on('change', function () {
            $(".actionButton").removeClass("hidden")
            if (sync_type == 2) {
                if ($("#Setting option:selected").attr('data-name') == 'UOM') {

                    $(".actionButton").addClass("hidden")
                }
            }



            rebuild_table()
        })
        //        $('#tableWithSearch').on('click', '.Action .Delete', function () {
        //            $("#DeleteFromList").modal("show")
        //            ToDelete = $(this).parents("tr").find(".Action").attr('data-id');
        //        });
        $('#tableWithSearch').on('click', '.Delete', function () {
            ToDelete = $(this).parents('div.Action').attr('data-id');
            var dataToDelete = {
                tableDel: $("#Setting option:selected").attr('data-table'),
                columnDel: $("#Setting option:selected").attr('data-col'),
                colNameDel: $("#Setting option:selected").attr('data-colName'),
                GetIDToDelete: ToDelete
            }
            IncludeDeleteModal(common_delete_modal, dataToDelete, function () {
                rebuild_table()
            }, "InventoryAjax/GeneralSettingRequest");
        });
        //        $("#ConfirmDelete").on("click", function () {
        //            var dataToDelete = {
        //                tableDel: $("#Setting option:selected").attr('data-table'),
        //                columnDel: $("#Setting option:selected").attr('data-col'),
        //                colNameDel: $("#Setting option:selected").attr('data-colName'),
        //                GetIDToDelete: ToDelete
        //            }
        //            $.ajax({
        //                type: 'GET',
        //                url: 'InventoryAjax/GeneralSettingRequest',
        //                data: dataToDelete,
        //                complete: function (data, vv) {
        //                    if (vv == "success") {
        //                        if(data.responseText == 'ok'){
        //                            $("#Delete_modalAlert").addClass("alert alert-success").show().html(Submitted_Successfully);
        //                            //                        ToDelete.remove();
        //                            rebuild_table()
        //                            setTimeout(function () {
        //                                $("#DeleteFromList").modal("hide")
        //                                $("#Delete_modalAlert").hide()
        //                            }, 1000)
        //                        }else{
        //                            $("#Delete_modalAlert").addClass("alert alert-danger").show().html(data.responseText);
        //                        }
        //                    }
        //                }
        //            });
        //        })
        $('.actionButton ').on("click", function () {
            IfEdit = false;
        })
        $('#tableWithSearch').on('click', '.Action .Edit', function () {
            ToEdit = $(this).parents("tr").find('td:eq(0)').html();
            TRtoEdit = $(this).parents('tr').find('.Action').attr('data-id');
            IfEdit = true;
            $('#LastEntry').val(ToEdit)
            $("#modalTitle").html(EditLang)
            $("#EditSettingEntry").modal("show")
        })
        $("#SubmitEntry").on("click", function () {
            if (!$("#EditSettingEntryPostForm").valid())
                return;
            var dataToEdit = {
                tableEdit: $("#Setting option:selected").attr('data-table'),
                columnEdit: $("#Setting option:selected").attr('data-col'),
                colNameEdit: $("#Setting option:selected").attr('data-colName'),
                colNewValue: $('#LastEntry').val(),
                currency_id: $("#CurrencyDropDown button input").val(),
                optiontype: $("#Setting").val()
            }
            if (IfEdit) {
                dataToEdit.GetIDToEdit = TRtoEdit;
            }
            $.ajax({
                type: 'GET',
                url: 'InventoryAjax/GeneralSettingRequest',
                data: dataToEdit,
                complete: function (data, vv) {
                    if (vv == "success") {
                        if (data.responseText !== 'error') {
                            if (data.responseText == 'OK') {
                                $("#EditSettingEntryAlert").addClass("alert alert-success").show().html(Submitted_Successfully);
                                rebuild_table()
                                setTimeout(function () {
                                    $("#EditSettingEntry").modal("hide")
                                    $("#EditSettingEntryAlert").hide();
                                }, 1000)
                            }
                        } else {
                            $("#EditSettingEntryAlert").addClass("alert alert-danger").show().html(Failed_Submit);
                        }
                    }
                }});
        })
        $('#CloseEdit').on('click', function () {
            $("#EditSettingEntry").modal("hide")
        })
    })
    function rebuild_table() {
        var dataContent = {
            table: $("#Setting option:selected").attr('data-table'),
            column: $("#Setting option:selected").attr('data-col'),
            colName: $("#Setting option:selected").attr('data-colName'),
            name: $("#Setting option:selected").attr('data-name')
        }
        RebuildDataTable(title, [0], 'InventoryAjax/GeneralSettingList', dataContent);
    }
</script>
