
<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 class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div id="FinancialYearAlert"></div>
            <div class="panel-title"> <?php print CommonLanguage::getMessage('financial_year'); ?></div>

            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <?php
            if ($permissions->hasPermission('add_financial_year')) {
                echo Forms::FloatingAddButton('4c0870', '#add_financial_year', 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('year'); ?></th>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('start_date'); ?></th>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('end_date'); ?></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="add_financial_year" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <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-label="Close"><span aria-hidden="true"><i class="pg-close fa-22"></i></span></button>
                <h4 class="modal-title"><?php print CommonLanguage::getMessage('financial_year') ?></h4>
                <div id="assignFinancialYearAlert"></div>
            </div>
            <div class="modal-body">
                <form id="AssignFinancialYearForm">
                    <div class="row">
                        <div class=" col-md-12 m-t-10 " >
                            <div class=" col-md-6 m-t-10 " >
                                <label class="labelText"><?php print CommonLanguage::getMessage('year'); ?><span class="red-star">*</span></label>
                                <input type="text" class="form-control" id="current_year" name="current_year" required/>
                            </div>
                        </div>
                        <div class=" col-md-12 m-t-10 " >
                            <div class=" col-md-6 m-t-10 " >
                                <label class="labelText"><?php print CommonLanguage::getMessage('start_date'); ?><span class="red-star">*</span></label>
                                <input type="text" class="form-control" id="start_date" name="start_date" required/>
                            </div>
                            <div class=" col-md-6 m-t-10 " >
                                <label class="labelText"><?php print CommonLanguage::getMessage('end_date'); ?><span class="red-star">*</span></label>
                                <input type="text" class="form-control" id="end_date" name="end_date" required/>
                            </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('trans_types')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            financial_year_alert = <?php echo json_encode(CommonLanguage::getMessage('financial_year_alert')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>;
    var IsEdit = false, IDToEdit = 0;
    $(function () {

        $("#start_date,#end_date").datepicker({autoclose: true});
        $(".floatingContainer").on("click", function () {
            IsEdit = false;
            IDToEdit = 0;
        })

        RebuildDataTable(title, [0], "ACCAjax/financial_year_list")

        $("#assignBtn").on("click", function () {
            $("#assignFinancialYearAlert").removeClass('hidden')

            if ($("#current_year").val() == '' || $("#start_date").val() == '' || $("#end_date").val() == '') {
                $("#assignFinancialYearAlert").attr("class", "alert alert-danger").html(Required_Fields)
                return false;

            }
            var DataArr = new FormData(document.getElementById("AssignFinancialYearForm"));
            DataArr.append("current_year_id", IDToEdit)
            DataArr.append("is_edit", (IsEdit ? 1 : 0))
            $.ajax({
                url: "ACCAjax/add_edit_financial_year",
                method: "POST",
                dataType: 'json',
                data: DataArr,
                processData: false,
                contentType: false,
                complete: function (v1, v2) {
                    if (v2 == "success" && v1.responseJSON == 'OK') {
                        $("#assignFinancialYearAlert").attr("class", "alert alert-success").html(Submitted_Successfully)
                        RebuildDataTable(title, [0], "ACCAjax/financial_year_list")
                        setTimeout(function () {
                            $("#assignFinancialYearAlert").addClass('hidden')
                            $("#add_financial_year").modal("hide")
                        }, 5000)
                    } else {
                        $("#assignFinancialYearAlert").attr("class", "alert alert-danger").html(v1.responseJSON)
                    }
                }
            })
        })

        $("#tableWithSearch tbody").on('click', '.Edit', function () {
            if($(this).attr("data-is-closed")==1 || $(this).attr("data-is-act-closed")==1){
                $("#FinancialYearAlert").attr("class", "alert alert-danger").html(financial_year_alert)
                $(document.body).animate({scrollTop: 0}, 800)
                return;
            }
            IsEdit = true;
            IDToEdit = $(this).parents('.Action').attr('data-id');

            $("#current_year").val($(this).parents("tr").find("td:eq(0)").text())
            $("#start_date").val($(this).parents("tr").find("td:eq(1)").text())
            $("#end_date").val($(this).parents("tr").find("td:eq(2)").text())
            $("#add_financial_year").modal("show")

        })

        $("#tableWithSearch tbody").on('click', '.open_year,.open_act_year', function () {
            actual = $(this).hasClass('open_year') ? 0 : 1;
            $.ajax({
                url: "ACCAjax/open_financial_year",
                method: "POST",
                data: {year_id: $(this).parents('.Action').attr('data-id'), actual: actual},
                complete: function (v1, v2) {
                    if (v2 == "success" && v1.responseText == "OK") {
                        $("#FinancialYearAlert").attr("class", "alert alert-success").html(Submitted_Successfully)
                    } else {
                        $("#FinancialYearAlert").attr("class", "alert alert-danger").html(v1.responseText/*Failed_Submit*/)
                    }
                    $(document.body).animate({scrollTop: 0}, 800)
                }
            })
        })
    })





</script>
