<style>
.bg-complete:hover{
    color:black !important;
}

</style>


<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title pull-left"><?php print CommonLanguage::getMessage('statement_financial_position'); ?></div>
            <br>
            <div class="row">
                <div class="col-md-3 ">
                    <label><?php echo CommonLanguage::getMessage('currency') ?></label>
                    <?php echo Forms::CurrencyDropDown(1, 'complete') ?>
                </div>
                <div class=" col-md-3" >
                    <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                    <input id="From_To_Date" type="text" class="form-control" name="From_To_Date" >
                </div>
            </div>
            <div class="row no-print">
                <div class="col-md-3" >
                    <label><?php echo CommonLanguage::getMessage('begining_stock') ?></label>
                    <select id="stock_type" name="stock_type" class="full-width" data-init-plugin="select2">
                        <option value="0" type="acc"> <?php echo $begining_acc; ?></option>
                        <option value="manual" type="manual"><?php echo CommonLanguage::getMessage('manual') ?></option>
                        <?php foreach ($methods as $method): ?>
                            <option  value= "<?php echo $method['Pricing_Method_ID']; ?>" type="method" ><?php echo $method['Pricing_Method_Name']; ?></option>
                        <?php endforeach; ?>
                        <option  data-type-id= "0" type="last_cost" data-currency="<?php echo smc_session()['local_currency_symbol'] ?>"><?php echo CommonLanguage::getMessage('last_cost') ?></option>

                    </select>

                </div>
                <div class="col-md-3 hidden stock_value_div">
                    <label><?php echo CommonLanguage::getMessage('stock_value') ?></label>
                    <input id="stock_value" type="number" step="any" class="form-control" name="stock_value" >

                </div>
                <div class="col-md-3" >
                    <label><?php echo CommonLanguage::getMessage('ending_stock') ?></label>
                    <select id="end_stock_type" name="stock_type" class="full-width" data-init-plugin="select2">
                        <option value="0" type="acc"><?php echo $end_acc; ?></option>
                        <option value="manual" type="manual"><?php echo CommonLanguage::getMessage('manual') ?></option>
                        <?php foreach ($methods as $method): ?>
                            <option  value= "<?php echo $method['Pricing_Method_ID']; ?>" type="method" ><?php echo $method['Pricing_Method_Name']; ?></option>
                        <?php endforeach; ?>
                        <option  data-type-id= "0" type="last_cost" data-currency="<?php echo smc_session()['local_currency_symbol'] ?>"><?php echo CommonLanguage::getMessage('last_cost') ?></option>

                    </select>



                </div>
                <div class="col-md-3 hidden end_stock_value_div">
                    <label><?php echo CommonLanguage::getMessage('stock_value') ?></label>
                    <input id="end_stock_value" type="number" step="any" class="form-control" name="end_stock_value" >

                </div>
            </div>
            <div class="row">
                <div class="col-md-3">
                    <h5> </h5>

                    <button  type="button" style="color: white !important !important;"  class="btn btn-complete" data-toggle="dropdown" aria-expanded="false">
                        <a class="p-r-10"  style="color: white !important;" onclick="tableToExcel('IncomeStatement', 'name')">Export</a></button>
                    <button id="exportPDF"  style="color: white !important;" type="button" class="btn btn-complete" data-toggle="dropdown" aria-expanded="false">
                        <a class="p-r-10" style="color: white !important;" onclick="">PDF</a></button>

                </div>
                <div class="col-md-3">
                    <h5> </h5>
                    <button id="Generate_Report" type="button" class="btn btn-complete" data-toggle="dropdown" aria-expanded="false">
                        <?php print CommonLanguage::getMessage('generate_report'); ?></button>
                </div>
            </div>

            <br/>
        </div>
        <div class="panel-body">
            <table class="table table-hover demo-table-search" id="IncomeStatement">
                <tbody>
                    <?php // echo Forms::financialPosition($list); ?>                
                </tbody>
            </table>        
        </div>
    </div>
</div>
<script type="text/javascript">
    var fromdate = <?php echo json_encode($from_day) ?>, today = <?php echo json_encode($today) ?>;
    var is_actual = <?php echo $is_actual ? 1 : 0; ?>,
            from_lang = <?php echo json_encode(CommonLanguage::getMessage('From')); ?>, date_lang = <?php echo json_encode(CommonLanguage::getMessage('Date')); ?>, currency_lang = <?php echo json_encode(CommonLanguage::getMessage('currency')); ?>,
            title_lang = <?php echo json_encode(CommonLanguage::getMessage('statement_financial_position')); ?>,
            list = [],
            org_name = <?php echo json_encode($org2[0]['Organization_Name']); ?>;
    $(function () {
        build_table()
        $("input[name=From_To_Date]").daterangepicker({
            locale: {
                format: 'YYYY-MM-DD'
            },
            startDate: fromdate

        }, function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                    window.endDate = end.format("YYYY-MM-DD")

        });
        $("#stock_type").on("change", function () {
            if ($(this).val() === 'manual')
                $(".stock_value_div").removeClass('hidden');
            else
                $(".stock_value_div").addClass('hidden');
        });
        $("#end_stock_type").on("change", function () {
            if ($(this).val() === 'manual')
                $(".end_stock_value_div").removeClass('hidden');
            else
                $(".end_stock_value_div").addClass('hidden');
        });
        $("#Generate_Report").on("click", function () {
            build_table();
        });

        $("#exportPDF").on("click", function () {

            var date = (window.startDate ? window.startDate : fromdate) + ' to ' + (window.endDate ? window.endDate : today),
                    actual = is_actual ? 'Actual' : '';
            var data_from = window.startDate ? window.startDate : fromdate,
                    data_to = window.endDate ? window.endDate : today,
                    data_cur = $("#CurrencyDropDown button input").val(),
                    data_actual = is_actual;
            var url = "ACCAjax/printReport?html=" + list + "&org=" + org_name + "&title=" + title_lang + "&date=" + date + "&actual=" + actual + "&currency=" + $("#CurrencyDropDown button span:eq(0)").html() + '&income=0&data_from=' + data_from + '&data_to=' + data_to + '&data_cur=' + data_cur + '&data_actual=' + data_actual;
            printExternal(url)


        })
    })
    function build_table() {
        var Data = {
            From_Date: window.startDate ? window.startDate : fromdate,
            To_Date: window.endDate ? window.endDate : today,
            Currency_ID: $("#CurrencyDropDown button input").val(),
            actual: is_actual,
            stock_value: $("#stock_value").val(),
            stock_type: $("#stock_type").val(),
            end_stock_value: $("#end_stock_value").val(),
            end_stock_type: $("#end_stock_type").val(),
            stock_method: $("#stock_type option:selected").attr("type"),
            end_stock_method: $("#end_stock_type option:selected").attr("type"),
        }

        $.ajax({
            url: "ACCAjax/StmtFinancialPosAjax",
            data: Data,
            method: "GET",
            dataType: "json",
            complete: function (v1, v2) {
                if (v2 == "success") {

                    $("#IncomeStatement tbody").empty().html(v1.responseJSON[0])
                    list = v1.responseJSON[1]

                }

            }
        })
    }
    var tableToExcel = (function () {
        var uri = 'data:application/vnd.ms-excel;base64,'
                , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
                , base64 = function (s) {
                    return window.btoa(unescape(encodeURIComponent(s)))
                }
        , format = function (s, c) {
            return s.replace(/{(\w+)}/g, function (m, p) {
                return c[p];
            })
        }
        return function (table, name) {
            if (!table.nodeType)
                table = document.getElementById(table)
            var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
            window.location.href = uri + base64(format(template, ctx))
        }
    })()
//function printExternal(url) {
//        $("#print_iframe").remove();
//        $("<iframe id='print_iframe' style='position:absolute;top:0px;left:0;width:800px;height:800px;display:none;' src='" + url + "' onload='document.getElementById(\"print_iframe\").contentWindow.print();'  ></iframe>").appendTo("body");
//
//
//    }

</script>