<?php
if (($permissions->hasPermission('addAccruedExpense') && !$Is_Actual) || ($permissions->hasPermission('addAccruedExpenseActual') && $Is_Actual)) {
    include("Views/Accounting/addAccruedExpense.php");
}
include('Views/Accounting/payForAccruedExpense.php');
?>
<style>
    .dropdown-toggle {
        background-color:#4c0870;
        border-color : #4c0870;
    }
    .dropdown-toggle:hover{
        background-color:#4c0870;
        border-color : #4c0870;
    }
    #tableWithSearch thead tr >th{
        background-color:#4c0870;
        color:#fff;
        width: 6% !important;
    }
    #tableWithSearch  thead tr th[class*='sorting_'] {
        color: #fff;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd;  
    }
    .btn-group {
        /*        background-color:#ed9421 !important;*/
        color:#fff;
    }
    .btn-default{
        border: none;
        background-color: #eee;
    }
    .btn-default:hover{
        background-color:#ed9421 !important;
        color:#fff;
        border: none;
    }
    .table-responsive {
        overflow-x: visible; 
    }
    .dropdown-menu>li>a:hover, .dropdown-menu>li>a:focus{
        background-color:#ed9421 !important; ;
        color:#fff;
    }
    .btn-default:hover{
        background-color:#ed9421 !important;
        color:#fff;
        border: none;
    }
    .complete ,.btn-group  .active{
        background-color:#ed9421 !important;
        color:#fff;
    }
    .MonthPicker,.YearPicker,.FromToPicker{
        display:none;
    }
    #selected_entry{
        height:90px;
        color: black;
        margin-top:20px;
        font-size: 16px;
        padding-top: 2%;
    }
    .select_table td{
        padding-left: 70px;
    }
</style>
<script src="<?php echo THEME_PATH; ?>/plugins/moment/moment.min.js"></script>
<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('accrued_list'); ?></div>
            <div class="input-group  col-md-3" >
                <span class="input-group-addon bg-complete"><i class="fa fa-calendar"></i></span>
                <input id="From_To_Date" type="text" class="form-control" name="From_To_Date" >
            </div>
            <?php
            $perm = $Is_Actual ? 'addAccruedExpenseActual' : 'addAccruedExpense';
            if ($permissions->hasPermission($perm))
                echo Forms::FloatingAddButton('4c0870', '#addAccrued', CommonLanguage::getMessage('add_accrued_expenses'));
            ?>
        </div>
        <div class="panel-body">
            <table class="table table-bordered table-hover demo-table-search" id="tableWithSearch">
                <thead>
                    <tr>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('account'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('vendor_name'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('expected_amount'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('real_amount'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('description'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('start_date'); ?></th>
                        <th class="text-center"><?php print CommonLanguage::getMessage('end_date'); ?></th>
                        <th class="text-center no-print"><?php print CommonLanguage::getMessage('Action'); ?></th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
</div>
<script>
    var ToEditID = 0, id;
    var Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            CompanyLang = <?php echo json_encode(CommonLanguage::getMessage('company')); ?>,
            EmailLang = <?php echo json_encode(CommonLanguage::getMessage('Email')); ?>,
            PhoneLang = <?php echo json_encode(CommonLanguage::getMessage('Phone')); ?>,
            AddressLang = <?php echo json_encode(CommonLanguage::getMessage('address')); ?>,
            CountryLang = <?php echo json_encode(CommonLanguage::getMessage('country')); ?>,
            FinancialNbLang = <?php echo json_encode(CommonLanguage::getMessage('financial_nb')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            title = <?php echo json_encode(CommonLanguage::getMessage('accrued_list')); ?>,
            is_actual = <?php echo json_encode($Is_Actual); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>;
    var today = <?php echo json_encode($today) ?>;
    $(function () {
        ChangeTable();
        $("input[name=From_To_Date]").daterangepicker({
        }, function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                    window.endDate = end.format("YYYY-MM-DD")
            ChangeTable();
        });
        $(".floatingContainer ").on("click", function () {
            ToEditID = 0;
            AccruedSupplierSearch();
        })
        $('#tableWithSearch').on('click', '.payAmount', function () {
            id = $(this).parents(".Action").attr("data-id");
        })
        $('#tableWithSearch').on('click', '.EditEntry', function () {
            ToEditID = $(this).parents('.Action').attr('data-id')
            var dataToEdit = {
                is_actual: is_actual,
                ToEditID: ToEditID
            }
            $.ajax({
                url: "ACCAjax/viewAccruedExpenseAjax",
                data: dataToEdit,
                method: "GET",
                dataType: 'json',
                complete: function (v1, v2) {
                    console.log(v1.responseJSON)
                    if (v2 == "success") {
                        var Data = (v1.responseJSON);
                        $('#addAccruedSupplier_ID').val(Data['Supplier_ID'])
                        AccruedSupplierSearch()
                        $('#AccruedStartDate').val(Data['Start_Date'])
                        $('#AccruedEndDate').val(Data['End_Date'])
                        $('#Accrued_Currency').val(Data['Currency_ID'])
                        $('#current_rate').val(Data['current_rate'])
                        $('#Expense_Account').val(Data['Account_ID']).attr('data-select-name', Data['Expense_Name'] + '-' + Data['Expense_Code']);
                        $('#Expense_Accrued').val(Data['Accrued_Account_ID']).attr('data-select-name', Data['Accrued_Name'] + '-' + Data['Accrued_Code']);
                        $('#branch_id').val(Data['branch_id']).attr('data-select-name', Data['branch_name']);
                        $('#job_id').val(Data['job_id']).attr('data-select-name', Data['job_name']);
                        $('#project_id').val(Data['project_id']).attr('data-select-name', Data['project_name']);
                        build_select2($('#Expense_Account'), "xhr/accIDSearch?type=Expense")
                        build_select2($('#Expense_Accrued'), "xhr/accIDSearch?type=AccruedExpense")
                        build_select2($("#branch_id"), "xhr/CommonSearch?col_id=branch_id&col_name=branch_name&table=tbl_organization_branches")
                        build_select2($("#job_id"), "ACCAjax/JobSearch")
                        build_select2($("#project_id"), "ACCAjax/projectSearch")

                        //                        $('#Expense_Account').val(Data['Expense_Code'])
                        //                        $('#select2-chosen-18').html(Data['Expense_Name'] + '-' + Data['Expense_Code'])
                        //                        $('#Expense_Accrued').val(Data['AccruedlExpense_Code'])
                        //                        $('#select2-chosen-19').html(Data['Accured_Name'] + '-' + Data['AccruedExpense_Code'])
                        $('#Expected_Amount').val(Data['Expected_Amount'])
                        $('.AccuredTotal #PrepaidTotalDetails').html(Data['Expected_Amount'])
                        $('.CurrencyIndicator').html(Data['Currency_Symbol'])
                        $('#Accrued_Desc').val(Data['Description'])
                        $('#CurrencyDropDown button span').html(Data['Currency_Symbol'])
                        $('#addAccruedSupplierInfoContent ').html("<strong><span class=Name'>" + Data['Supplier_Name'] + "</span></strong><div><strong>" + CompanyLang + ": </strong><span class='Company'>" + Data['Supplier_Company'] + "</span></div><div><strong>" + EmailLang + ": </strong><span class='Email'>" + Data['Supplier_Email'] + "</span></div><div><strong>" + PhoneLang + ": </strong><span class='Phone'>" + Data['Supplier_Phone'] + "</span></div>")
                        $('#addAccrued').modal('show')
                    }
                }
            })
        })
    })
    function ChangeTable() {
        var DataArr = {
            is_actual: is_actual,
            Acrrued_Expense_From: window.startDate ? window.startDate : today,
            Acrrued_Expense_To: window.endDate ? window.endDate : today
        };
        RebuildDataTable(title, [0, 1, 2, 3, 4, 5], 'ACCAjax/viewAccruedExpenseRequest', DataArr);
    }
</script>
