<?php echo $header; ?><?php echo $column_left; ?>

<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <h1><?php echo $heading_title; ?></h1>
            <ul class="breadcrumb">
                <?php foreach ($breadcrumbs as $breadcrumb) { ?>
                <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
                <?php } ?>
            </ul>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-bar-chart"></i> <?php echo $text_list; ?></h3>
            </div>
            <div class="panel-body">
                <div class="well">
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label" for="input-status">Özellik</label>
                                <select class="attribute" name="attribute" style="width:100%;" multiple="multiple">
                                    <?php

                    if($product_attributes){
                        foreach($product_attributes as $product_attribute){
                          if(in_array($product_attribute['attribute_id'],$filter_attribute)){
                               echo '<option selected value="'.$product_attribute['attribute_id'].'">
                                    '.$product_attribute['name'].'</option>';
                                    }else{
                                    echo '
                                    <option value="'.$product_attribute['attribute_id'].'">
                                        '.$product_attribute['name'].'
                                    </option>
                                    ';
                                    }
                                    }
                                    }

                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label" for="input-status">Seçenek</label>
                                <select class="option" name="attribute" style="width:100%;" multiple="multiple">
                                    <?php

                    if($product_options){
                        foreach($product_options as $product_option){
                          if(in_array($product_option['option_value_id'],$filter_option)){
                               echo '<option selected value="'.$product_option['option_value_id'].'">
                                    '.$product_option['name'].'</option>';
                                    }else{
                                    echo '
                                    <option value="'.$product_option['option_value_id'].'">'.$product_option['name'].'
                                    </option>
                                    ';
                                    }
                                    }
                                    }

                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label" for="input-product-model">Kategori</label>
                                <select class="category" name="category" style="width:100%;" multiple="multiple">
                                    <?php
                  if($categories){
                      foreach($categories as $key => $value){
                                    if(in_array($key,$filter_category)){
                                    echo '
                                    <option selected value="'.$key.'">'.$value.'</option>
                                    ';
                                    }else{
                                    echo '
                                    <option value="'.$key.'">'.$value.'</option>
                                    ';
                                    }
                                    }
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label" for="input-product-model">İl</label>
                                <select class="form-control" name="filter_zone_id" style="width:100%;">
                                    <option
                                    <? ($filter_zone_id==null) ? 'selected' : '' ?> value="">Tüm İller</option>
                                    <?php
                  if($zones){
                      foreach($zones as $zone){
                        if($zone['zone_id'] == $filter_zone_id){
                          echo '<option selected value="'.$zone['zone_id'].'">'.$zone['name'].'</option>';
                                    }else{
                                    echo '
                                    <option value="'.$zone['zone_id'].'">'.$zone['name'].'</option>
                                    ';
                                    }
                                    }
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="col-sm-12">
                            <div class="form-group">
                                <label class="control-label"
                                       for="input-product-model"><?php echo $entry_product_model; ?></label>
                                <input type="text" name="filter_product_model"
                                       value="<?php echo $filter_product_model; ?>"
                                       placeholder="<?php echo $entry_product_model; ?>" id="input-product-model"
                                       class="form-control"/>
                            </div>

                        </div>

                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label"
                                       for="input-date-start"><?php echo $entry_date_start; ?></label>
                                <div class="input-group date">
                                    <input type="text" name="filter_date_start"
                                           value="<?php echo $filter_date_start; ?>"
                                           placeholder="<?php echo $entry_date_start; ?>" data-date-format="YYYY-MM-DD"
                                           id="input-date-start" class="form-control"/>
                                    <span class="input-group-btn">
                  <button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
                  </span></div>
                            </div>

                            <div class="form-group">
                                <label class="control-label"
                                       for="input-sort-order"><?php echo $entry_sort_order; ?></label>
                                <select name="filter_sort_order" id="input-sort-order" class="form-control">
                                    <option value="0"
                                    <?php if ($filter_sort_order == "0") { ?> selected="selected" <?php } ?>
                                    >Yok</option>
                                    <option value="1"
                                    <?php if ($filter_sort_order == "1") { ?> selected="selected" <?php } ?>>Fiyata
                                    Göre</option>

                                    <option value="2"
                                    <?php if($filter_sort_order == "2") { ?> selected="selected" <?php } ?>>Miktar
                                    ile</option>

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


                        <div class="col-sm-6">


                            <div class="form-group">
                                <label class="control-label" for="input-date-end"><?php echo $entry_date_end; ?></label>
                                <div class="input-group date">
                                    <input type="text" name="filter_date_end" value="<?php echo $filter_date_end; ?>"
                                           placeholder="<?php echo $entry_date_end; ?>" data-date-format="YYYY-MM-DD"
                                           id="input-date-end" class="form-control"/>
                                    <span class="input-group-btn">
                  <button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
                  </span></div>
                            </div>


                            <div class="form-group">
                                <label class="control-label" for="input-status"><?php echo $entry_status; ?></label>
                                <select name="filter_order_status_id" id="input-status" class="form-control">
                                    <option value="0"><?php echo $text_all_status; ?></option>
                                    <?php foreach ($order_statuses as $order_status) { ?>
                                    <?php if ($order_status['order_status_id'] == $filter_order_status_id) { ?>
                                    <option value="<?php echo $order_status['order_status_id']; ?>"
                                            selected="selected"><?php echo $order_status['name']; ?></option>
                                    <?php } else { ?>
                                    <option value="<?php echo $order_status['order_status_id']; ?>"><?php echo $order_status['name']; ?></option>
                                    <?php } ?>
                                    <?php } ?>
                                </select>
                            </div>
                            <button type="button" id="button-filter" class="btn btn-primary pull-right"><i
                                        class="fa fa-filter"></i> <?php echo $button_filter; ?></button>
                            <!-- TODO:: Exele Aktar. -->
                            <button type="button" id="button-exel" data-value="test" class="btn btn-danger pull-right"
                                    style="margin-right: 5px"><i class="fa fa-file-excel-o"></i> Exel'e Aktar
                            </button>

                        </div>


                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>

                        <tr>
                            <td class="text-left">Özellik Adı</td>
                            <td class="text-left">Seçenek Adı</td>
                            <td colspan="1" class="text-left"><?php echo $column_model; ?></td>
                            <td class="text-right"><?php echo $column_quantity; ?></td>
                            <td class="text-right"><?php echo $column_total; ?></td>
                        </tr>
                        <tr>
                            <td class="text-left"></td>
                            <td class="text-left"></td>
                            <td colspan="1" class="text-left"><?php echo $filter_product_model; ?></td>
                            <td class="text-right"><?php echo isset($total_quantities)?$total_quantities:""; ?></td>
                            <td class="text-right"><?php echo isset($total_money)?$total_money:""; ?></td>
                        </tr>

                        <tr>
                            <td class="text-left">Özellik Adı</td>
                            <td class="text-left">Seçenek Adı</td>
                            <td class="text-left"><?php echo $column_model; ?></td>
                            <td class="text-right"><?php echo $column_quantity; ?></td>
                            <td class="text-right"><?php echo $column_total; ?></td>
                        </tr>
                        </thead>
                        <tbody>
                        <?php if ($products) { ?>
                        <?php foreach ($products as $product) { ?>
                        <tr>
                            <td class="text-left"><?php echo $product['name']; ?></td>
                            <td class="text-left"><?php echo $product['option_value_name']; ?></td>
                            <td class="text-left"><?php echo $product['model']; ?></td>
                            <td class="text-right"><?php echo $product['quantity']; ?></td>
                            <td class="text-right"><?php echo $product['total']; ?></td>

                        </tr>
                        <?php } ?>
                        <?php } else { ?>
                        <tr>
                            <td class="text-center" colspan="4"><?php echo $text_no_results; ?></td>
                        </tr>
                        <?php } ?>
                        </tbody>
                    </table>
                </div>
                <div class="row">
                    <div class="col-sm-6 text-left"><?php echo $pagination; ?></div>
                    <div class="col-sm-6 text-right"><?php echo $results; ?></div>
                </div>
            </div>
        </div>
    </div>


    <script type="text/javascript">

        $(document).ready(function () {
            $('.attribute').select2();
            $('.category').select2();
            $('.option').select2();
        });

        $('#button-exel').on('click', function () {
            let url = 'index.php?route=report/product_sales_for_product_attribute/excelDownload&token=<?=$token?>';
            filterUrl(url)
        })

        $('#button-filter').on('click', function () {
            let url = 'index.php?route=report/product_sales_for_product_attribute&token=<?php echo $token; ?>';
            filterUrl(url)
        });

        function filterUrl(uri) {
            var url = uri
            var filter_date_start = $('input[name=\'filter_date_start\']').val();
            if (filter_date_start) {
                url += '&filter_date_start=' + encodeURIComponent(filter_date_start);
            }
            var filter_attribute = $('.attribute').select2('val');
            if (filter_attribute != null) {
                url += '&filter_attribute=' + filter_attribute.join();
            }
            var filter_option = $('.option').select2('val');
            if (filter_option != null) {
                url += '&filter_option=' + filter_option.join();
            }
            var filter_category = $('.category').select2('val');
            if (filter_category != null) {
                url += '&filter_category=' + filter_category.join();
            }
            var filter_date_end = $('input[name=\'filter_date_end\']').val();
            if (filter_date_end) {
                url += '&filter_date_end=' + encodeURIComponent(filter_date_end);
            }
            var filter_order_status_id = $('select[name=\'filter_order_status_id\']').val();
            if (filter_order_status_id != 0) {
                url += '&filter_order_status_id=' + encodeURIComponent(filter_order_status_id);
            }
            var filter_product_model = $('input[name=\'filter_product_model\']').val();
            if (filter_product_model) {
                url += '&filter_product_model=' + encodeURIComponent(filter_product_model);
            }
            var filter_sort_order = $('select[name=\'filter_sort_order\']').val();
            if (filter_sort_order != 0) {
                url += '&filter_sort_order=' + encodeURIComponent(filter_sort_order);
            }
            var filter_zone_id = $('select[name=\'filter_zone_id\']').val();
            if (filter_zone_id != '') {
                url += '&filter_zone_id=' + encodeURIComponent(filter_zone_id);
            }
            window.location = url
        }

        //--></script>

    <script type="text/javascript">

        $('.date').datetimepicker({
            pickTime: false
        });

        //--></script>

</div>
<?php echo $footer; ?>