
<style>
    .table thead tr >th{
        background-color:#4c0870;
        width:16% !important;
        color:#fff;
    }
    .table thead tr th[class*='sorting_'] {
        color: #fff;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd;  
    }
    #selected_entry{
        height:90px;
        color: black;
        margin-top:20px;
        font-size: 16px;
        padding-top: 2%;
    }
    .select_table td{
        padding-left: 70px;
    }
</style>
<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($is_actual ? 'exchange_rate_actual' : 'exchange_rate_list'); ?></div>
            <div id="submitExchange" class="alert alert-success hidden"></div>
            <div class="clearfix"></div>
        </div>
        <div class="panel-body form-group">
            <div class="row">
                <div class="col-md-3">
                    <label style="color:black !important;"><?php echo CommonLanguage::getMessage('to_date'); ?></label>
                    <input id="ex_date" class="form-control" type="text" value="<?php echo $today; ?>"/>
                </div>
            </div>
            <br>
            <div class="row">
                <table class="table table-bordered table-hover demo-table-search" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th class="text-center" style="width:5%"><?php print CommonLanguage::getMessage('account_code'); ?></th>
                            <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('account_name'); ?></th>
                            <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('type'); ?></th>
                            <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('branch'); ?></th>
                            <?php if (smc_session()['default_setting']['activate_accounting_job'] == 1) { ?>
                                <th class="text-center " style="width:5%" ><?php print CommonLanguage::getMessage('job'); ?></th>
                            <?php }if (smc_session()['default_setting']['activate_project'] == 1) { ?>
                                <th class="text-center " style="width:5%" ><?php print CommonLanguage::getMessage('project'); ?></th>
                            <?php } ?>
                            <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('balance'); ?></th>
                        </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>
            <div class="container-sm-height">
                <button class=" btn  btn-cons m-b-10 pull-right" id="SubmitButton" disabled="disabled" style="color:#fff;background-color:#4c0870 ;padding-top: 10px;top: 20px;min-width:253px;" type="button"><i class="pg-form"></i> <span class="bold"><?php print CommonLanguage::getMessage('Save'); ?></span></button>
            </div>

        </div>
    </div>
</div>

<script>
    var Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            local_symbol = <?php echo json_encode(smc_session()['local_currency_symbol']); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            title =<?php print json_encode(CommonLanguage::getMessage('exchange_rate_list')); ?>,
            activate_job =<?php echo json_encode(smc_session()['default_setting']['activate_accounting_job']) ?>,
            activate_project =<?php echo json_encode(smc_session()['default_setting']['activate_project']) ?>,
            is_actual =<?php echo json_encode($is_actual) ?>;
    var data_array = [];
    $(function () {

        $("#ex_date").datepicker({autoclose: true});
        build_table()
        $("#ex_date").on("changeDate", function () {
            build_table()
        })
        $("#SubmitButton").on("click", function () {
            if ($("#ex_date").val() == '') {
                alert('Must Fill Date!');
                return false;
            }
//        $("#tableWithSearch tbody tr").each(function(e){
            var Data = {
                Records: data_array,
                Submit_Close: 1,
                actual: is_actual,
                date: $("#ex_date").val()
            };
            $.ajax({
                url: "ACCAjax/currencyExchangeRateAjax",
                method: "POST",
                data: Data,
                complete: function (v1, v2) {
                    if (v2 == "success" && v1.responseText == 'OK') {
                        $("#submitExchange").attr("class", "alert alert-success").html(Submitted_Successfully)
                        build_table()
                    } else {
                        $("#submitExchange").attr("class", "alert alert-danger").html(v1.responseText/*Failed_Submit*/)
                    }
                    $(document.body).animate({scrollTop: 0}, 800)
                }
            })
            //})
        })

    })
    function build_table() {
        $("#SubmitButton").attr('disabled', 'disabled');
        var Data = {
            actual: is_actual,
            date: $("#ex_date").val()
        };
        $.ajax({
            url: "ACCAjax/ex_rate_table_ajax",
            method: "GET",
            data: Data,
            dataType: 'json',
            complete: function (v1, v2) {
                var data_arr = v1.responseJSON;
                data_array = data_arr[1];
//                console.log(data_arr)
//                    $("#tableWithSearch tbody").empty()
                var tr = '';
                for (var i = 0; i < data_arr[0].length; i++) {
//                    console.log(data_arr[i])
                    tr += "<tr>" +
                            "<td class=' v-align-middle text-center bold'>" + data_arr[0][i]['Account_Code'] + "</td>" +
                            "<td class=' v-align-middle text-center bold'>" + data_arr[0][i]['Account_Name'] + "</td>" +
                            "<td class=' v-align-middle text-center bold'>" + data_arr[0][i]['Transaction_Type'] + "</td>" +
                            "<td class=' v-align-middle text-center bold'>" + data_arr[0][i]['branch_name'] + "</td>";
                    if (activate_job == 1)
                        tr += "<td class=' v-align-middle text-center bold'>" + data_arr[0][i]['job_name'] + "</td>";
                    if (activate_project == 1)
                        tr += "<td class=' v-align-middle text-center bold'>" + data_arr[0][i]['project_name'] + "</td>";

                    tr += "<td class=' v-align-middle text-center bold'>" + (local_symbol + " " + data_arr[0][i]["Balance"]) + "</td>" +
                            "</tr>";
                }
                $("#tableWithSearch").DataTable().destroy();
                $("#tableWithSearch tbody").empty().html(tr);
                RebuildDataTable(title, [], "", {}, '', '');
                $("#SubmitButton").removeAttr('disabled');
            }
        })
    }
</script>
