<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-8">
        <h2><?= print_lang($app_lang,'lot_difference_report'); ?></h2>
        <ol class="breadcrumb">
            <li>
                <a class="url-elem" href="<?php echo url('/dashboard'); ?>"><?= print_lang($app_lang,'home'); ?></a>
            </li>
            <li class="active">
                <strong><?= print_lang($app_lang,'lot_difference_report'); ?></strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-4">
        
    </div>
</div>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">

                    <h2 class="text-center font-bold" style="margin: 10px 0 20px;"><?= print_lang($app_lang,'lot_difference_report_listing'); ?></h2>

                    <form id="lot-diff-report-search-form" action="<?= url('/lot-purchase/difference'); ?>">
                    <div class="row">
                        <div class="col-md-9">
                            <div class="row">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <div class="input-group date">
                                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                            <select class="form-control chosen-select" name="currency">
                                                <option value="" selected="">-- <?= print_lang($app_lang,'currency'); ?> --</option>
                                                <?php foreach($currencies as $row){ ?>
                                                    <option value="<?= $row['uid']; ?>" <?php if($search_currency == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']; ?> (<?= $row['symbol']; ?>)</option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <div class="input-group date">
                                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                            <select class="form-control chosen-select" name="supplier">
                                                <option value="" selected="">-- <?= print_lang($app_lang,'supplier'); ?> --</option>
                                                <?php foreach($suppliers as $row){ ?>
                                                    <option value="<?= $row['uid']; ?>" <?php if($search_supplier == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']; ?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <div class="form-group">
                                        <?php if($branch_calander == 'persian'){ ?>
                                            <input type="text" class="form-control persian-date" value="<?= true_date_for_inputs($search_from); ?>" name="from" placeholder="<?= print_lang($app_lang,'from'); ?>" readonly="" style="background: #fff;">
                                        <?php }else{ ?>
                                            <input type="date" class="form-control" name="from" value="<?= true_date_for_inputs($search_from); ?>">
                                        <?php } ?>
                                    </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <?php if($branch_calander == 'persian'){ ?>
                                            <input type="text" class="form-control persian-date" value="<?= true_date_for_inputs($search_to); ?>" name="to" placeholder="<?= print_lang($app_lang,'to'); ?>" readonly="" style="background: #fff;">
                                        <?php }else{ ?>
                                            <input type="date" class="form-control" name="to" value="<?= true_date_for_inputs($search_to); ?>">
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3 text-right">
                            <button class="btn btn-w-m btn-primary custom-font-btn-search" type="submit"><?= print_lang($app_lang,'search'); ?></button>
                            <a href="#" data-date="<?= display_true_date(date('Y-m-d')); ?>" class="today-date-form-trigger btn btn-primary custom-font-btn-search"><?= print_lang($app_lang,'today'); ?></a>
                        </div>
                    </div>
                    </form>
                    <hr>

                    <div class="table-responsive">
                        <table class="table table-bordered table-hover dataTables-view" style="width: 150%;">
                            <thead>
                                <tr>
                                    <th class="text-center"><?= print_lang($app_lang,'sr_#'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'email_image'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'purchased_invoice_num'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'purchased_date'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'supplier'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'fuel_type'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'driver'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'plate_num'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mobile'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'cmr'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'transit'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expense'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'weight_first'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'difference_positive'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'difference_negative'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'gross_weight'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $i = 1;
                                $total_weight = array();
                                $total_difference_positive = array();
                                $total_difference_negative = array();
                                $total_gross_weight = array();

                                foreach($pageData as $sale_row){

                                    // if lot data not found | skip
                                    if(!array_key_exists($sale_row['parent'], $lots)){ continue; }

                                    $lot_p_data = $lots[$sale_row['parent']];

                                    // supplier filter
                                    if(!empty($search_supplier)){ if($search_supplier != $lot_p_data['supplier']){ continue; } }

                                    $lot_c_data = json_decode($lot_p_data['lot_list'],true);
                                    $sold_lot_data = array();
                                    $sold_lot_data_found = false;
                                    foreach($lot_c_data as $c_row)
                                    {
                                        if($sale_row['child'] == $c_row['uid'])
                                        {
                                            $sold_lot_data = $c_row;
                                            $sold_lot_data_found = true;
                                        }
                                    }

                                    // if data not found | skip row
                                    if($sold_lot_data_found === false){ continue; }


                                    $image_path = '/uploads/screenshots/'.$lot_p_data['email_attachment'];
                                    if(is_file(base_path().$image_path))
                                    {
                                        $image_html = '<a data-gallery="" class="btn btn-primary btn-xs" target="_blank" href="'.url('/').$image_path.'">'.print_lang($app_lang,'preview').'</a>';
                                    }
                                    else
                                    {
                                        $image_html = '<span class="label label-info">'.print_lang($app_lang,'no_image').'</span>';
                                    }

                                    if($sale_row['difference'] == 0){ continue; }

                                ?>

                                    <tr class="text-center lotitem<?= $sold_lot_data['uid']; ?>">
                                        <td><?= $i++; ?></td>
                                        <td><?= $image_html; ?></td>
                                        <td><a target="_blank" href="<?= url('/lot-purchase/invoice').'/'.$lot_p_data['uid']; ?>"><?= $lot_p_data['receipt']; ?></a></td>
                                        <td><?= display_true_date($lot_p_data['lot_date']); ?></td>
                                        <td><?= array_key_exists($lot_p_data['supplier'], $suppliers) ? $suppliers[$lot_p_data['supplier']]['name'] : NULL; ?></td>
                                        
                                        <td><?= $sold_lot_data['fuel_type']; ?></td>
                                        <td><?= $sold_lot_data['driver']; ?></td>
                                        <td><?= $sold_lot_data['plate']; ?></td>
                                        <td><?= $sold_lot_data['mobile']; ?></td>
                                        <td><?= $sold_lot_data['cmr']; ?></td>

                                        <td><?= array_key_exists($sold_lot_data['transit_account'], $transits) ? $transits[$sold_lot_data['transit_account']]['name'] : NULL; ?></td>
                                        <td><?= array_key_exists($sold_lot_data['expense_account'], $expenses) ? $expenses[$sold_lot_data['expense_account']]['name'] : NULL; ?></td>
                                        
                                        <td><?= round_decimels_digits_3($sale_row['weight']); $total_weight[] = $sale_row['weight']; ?></td>
                                        <td><?php if($sale_row['difference_type'] == 'plus'){ echo round_decimels_digits_3($sale_row['difference']); $total_difference_positive[] = $sale_row['difference']; } ?></td>
                                        <td><?php if($sale_row['difference_type'] == 'minus'){ echo round_decimels_digits_3($sale_row['difference']); $total_difference_negative[] = $sale_row['difference']; } ?></td>
                                        <td><?= round_decimels_digits_3($sale_row['gross_weight']); $total_gross_weight[] = $sale_row['gross_weight']; ?></td>
                                    </tr>

                                <?php } ?>

                                <tr class="text-center">
                                    <td><?= $i++; ?></td>
                                    <td><?= print_lang($app_lang,'total'); ?></td>
                                    <td>---</td>
                                    <td><?= display_true_date(date('Y-m-d')); ?></td>
                                    <td>---</td>                                    
                                    <td>---</td>
                                    <td>---</td>
                                    <td>---</td>
                                    <td>---</td>
                                    <td>---</td>
                                    <td>---</td>
                                    <td>---</td>

                                    <td><?= round_decimels_digits_3( array_sum( $total_weight ) ); ?></td>
                                    <td><?= round_decimels_digits_3( array_sum( $total_difference_positive ) ); ?></td>
                                    <td><?= round_decimels_digits_3( array_sum( $total_difference_negative ) ); ?></td>
                                    <td><?= round_decimels_digits_3( array_sum( $total_gross_weight ) ); ?></td>
                                </tr>

                            </tbody>
                        </table>    
                    </div>

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