@extends('master.app')
@section('title', 'Report')
@section('pageCSS')
    <style id="table_style">
        table {
            width: 100%;
            border-collapse: collapse;
            margin: auto;
            border: 0.5px solid black;
        }

        td,
        th {
            border: 0.5px solid black;
            text-align: center;
        }

        td {
            font-size: 12px;
            padding: 0px;

            height: 20px;
            text-align: center;
        }

        .report-logo {
            margin-left: 480px;
            height: 100px;
            width: 100px;
            padding-top: 0px;
            margin-bottom: 0px;
        }

        h6 {
            padding: 0px;
            margin: 0px;
        }

        p {
            padding: 0px;
            margin: 0px;
        }
    </style>
@endsection
@section('body')
    <main class="app-content">
        <section class="">
            <div class="">
                <div class="card">
                    <div class="card-body">
                        <div class="row g-0">
                            <div class="col-md-12 gradient-custom "
                                style="border-top-left-radius: .5rem; border-bottom-left-radius: .5rem;">
                                <div class="card">
                                    <div class="card-header text-dark">
                                        <h4>Your Stock In History Report</h4>
                                    </div>
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-md-3 mb-4">
                                                <div class="form-outline datepicker">
                                                    <input type="date" class="form-control" id="fromDate"
                                                        value="{{ date('Y-m-d') }}" id="fromDate" name="fromDate" />
                                                    <label for="exampleDatepicker1" class="form-label">From</label>
                                                </div>
                                            </div>
                                            <div class="col-md-3 mb-4">
                                                <div class="form-outline datepicker">
                                                    <input type="date" class="form-control" id="toDate"
                                                        value="{{ date('Y-m-d') }}" id="toDate" name="toDate" />
                                                    <label for="exampleDatepicker1" class="form-label">To</label>
                                                </div>
                                            </div>


                                            <div class="col-md-3 mb-4" id="category">
                                                <div class="form-group">
                                                    <select id="category_id" name="category_id" class="select2 form-control"
                                                        autofocus style="width: 100%;">
                                                        <option value="">Choose Category</option>
                                                        <option value="all">All</option>
                                                        @foreach ($categories as $row)
                                                            <option value="{{ $row->id }}">{{ $row->title_en }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                            </div>

                                            <div class="col-md-12 ">
                                                <button class="btn btn-primary btn-rounded btn-sm" id="btn-find"
                                                    style="float: left;">Find</button>
                                                <button style="float: left;" class="btn btn-primary btn-rounded btn-sm"
                                                    id="btn-print" onclick="PrintTable();">Print</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="card-body p-4 print_div" id="print_div">

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

                    </div>
                </div>
        </section>
    </main>
@endsection
@section('pageJs')
    <script type="text/javascript">
        $(".app-menu").removeClass("active");
        $("#nav_stocki").addClass("active");
        $("#stocki").addClass("is-expanded");
        $("#stock_history").addClass("active");
    </script>
    <script>
        $("#btn-find").click(function() {
            $.ajax({
                url: '{{ route('stock_history.report_find') }}',
                type: 'GET',
                data: {
                    'fromDate': $("#fromDate").val(),
                    'toDate': $("#toDate").val(),
                    'category_id': $("#category_id").val(),
                },
                success: function(data) {
                    $(".print_div").html(data);
                },
                error: function() {
                    swal("Warrning", "Error occured!", "error");
                },
            });
        });

        $("#type_id").change(function() {
            var type_id = $("#type_id").val();
            if (type_id == 1) {
                $("#category").show();
                $("#unit").hide();
                $("#weight").hide();
            }
            if (type_id == 2) {
                $("#category").hide();
                $("#unit").show();
                $("#weight").hide();
            }
            if (type_id == 3) {
                $("#category").hide();
                $("#unit").hide();
                $("#weight").show();
            }
        });

        function PrintTable() {
            var printWindow = window.open('', '', 'height=1000,width=1000');
            var table_style = document.getElementById("table_style").innerHTML;
            printWindow.document.write('<style type = "text/css">');
            printWindow.document.write(table_style);
            printWindow.document.write('</style>');
            var divContents = document.getElementById("print_div").innerHTML;
            printWindow.document.write(divContents);
            setTimeout(() => {
                printWindow.print();
                printWindow.close();

            }, 1000);
        }
    </script>
@endsection
