<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-8">
        <h2><?= print_lang($app_lang,'lot_moved_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_moved_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_moved_difference_report_listing'); ?></h2>

                    <form id="lot-diff-report-search-form" action="<?= url('/lot-purchase/moved-difference'); ?>">
                    <div class="row">
                        <div class="col-md-9">
                            <div class="row">
                                <div class="col-md-7">
                                    <div class="row">
                                        <div class="col-md-4 pd-5">
                                            <div class="form-group">
                                                <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 class="col-md-4 pd-5">
                                            <div class="form-group">
                                                <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 class="col-md-4 pd-5">
                                            <div class="form-group">
                                                <select class="form-control chosen-select" name="display">
                                                    <option value="" disabled="">-- <?= print_lang($app_lang,'display'); ?> --</option>
                                                    <option value="normal" <?php if($search_display == 'normal'){ echo 'selected="selected"'; } ?>><?= print_lang($app_lang,'normal'); ?></option>
                                                    <option value="hidden" <?php if($search_display == 'hidden'){ echo 'selected="selected"'; } ?>><?= print_lang($app_lang,'hidden'); ?></option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-5">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <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-6">
                                            <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>
                        </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">
                            <thead>
                                <tr>
                                    <th class="text-center"><?= print_lang($app_lang,'sr_#'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'date'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'supplier'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'driver'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'cmr'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'invoice'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'cost'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'weight'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'weight_plus'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'weight_minus'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'gross_weight'); ?></th>
                                    <?php if($search_display == 'normal'){ ?>
                                        <th class="text-center"><?= print_lang($app_lang,'hide'); ?></th>
                                    <?php }else{ ?>
                                        <th class="text-center"><?= print_lang($app_lang,'show'); ?></th>
                                    <?php } ?>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $i = 1;
                                $total_cost = array();
                                $total_weight = array();
                                $total_weight_plus = array();
                                $total_weight_minus = array();
                                $total_gross_weight = array();

                                foreach($pageData as $row){

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

                                    $lot = $lots[$row['lot_id']];
                                    

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

                                    if($lot['purchase_type'] == 'iran_to_border')
                                    {
                                        $invoice_url = url('/lot-purchase/invoice').'/'.$lot['uid'].'?type=iran_to_border';
                                        $lot_child_data = json_decode($lot['division_list'],true);
                                    }
                                    else
                                    {
                                        $invoice_url = url('/lot-purchase/invoice').'/'.$lot['uid'].'?type=iran_to_afghanistan';
                                        $lot_child_data = json_decode($lot['lot_list'],true);
                                    }

                                    $list_item = array();
                                    $list_item_found = false;
                                    foreach($lot_child_data as $c_row)
                                    {
                                        if($row['lot_child_id'] == $c_row['uid'])
                                        {
                                            $list_item = $c_row;
                                            $list_item_found = true;
                                        }
                                    }

                                    // if data not found | skip row
                                    if($list_item_found === false){ continue; }
                                    if($row['lot_move_diff'] == 0){ continue; }

                                    $lot_hidden_items = explode(',',$lot['moved_hidden_items']);
                                    // hidden filter
                                    if($search_display == 'normal')
                                    {
                                        // skip hidden
                                        if(in_array($list_item['uid'], $lot_hidden_items)){ continue; }
                                    }
                                    else
                                    {
                                        // show hidden
                                        if(!in_array($list_item['uid'], $lot_hidden_items)){ continue; }
                                    }

                                ?>

                                    <tr class="text-center">
                                        <td><?= $i++; ?></td>
                                        <td><?= display_true_date($lot['lot_date']); ?></td>
                                        <td><?= array_key_exists($lot['supplier'], $suppliers) ? $suppliers[$lot['supplier']]['name'] : NULL; ?></td>
                                        <td><?= $list_item['driver']; ?></td>
                                        <td><?= $list_item['cmr']; ?></td>
                                        <td><a target="_blank" href="<?= $invoice_url; ?>"><?= $lot['receipt']; ?></a></td>
                                       
                                        
                                        <td><?= amount_number_format_3($list_item['cost']); $total_cost[] = $list_item['cost']; ?></td>
                                        <td><?= amount_number_format_3($list_item['gross_weight']); $total_weight[] = $list_item['gross_weight']; ?></td>
                                        <td>
                                            <?php if($row['lot_move_diff_type'] == 'plus'){ ?>
                                                <a href="<?= url('/lot-purchase/edit').'/'.$lot['uid']; ?>"><?php echo amount_number_format_3($row['lot_move_diff']); ?></a>
                                            <?php $total_weight_plus[] = $row['lot_move_diff']; } ?>
                                        </td>
                                        <td>
                                            <?php if($row['lot_move_diff_type'] == 'minus'){ ?>
                                                <a href="<?= url('/lot-purchase/edit').'/'.$lot['uid']; ?>"><?php echo amount_number_format_3($row['lot_move_diff']); ?></a>
                                            <?php $total_weight_minus[] = $row['lot_move_diff']; } ?>
                                        </td>
                                        <td><?= amount_number_format_3($row['lot_move_gross_weight']); $total_gross_weight[] = $row['lot_move_gross_weight']; ?></td>
                                        <td>
                                            <input type="checkbox" class="lot-moved-item-check" value="<?= $lot['uid'].'__'.$list_item['uid']; ?>">
                                        </td>

                                    </tr>

                                <?php } ?>

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

                                    <td><?= amount_number_format_3( array_sum( $total_cost ) ); ?></td>
                                    <td><?= amount_number_format_3( array_sum( $total_weight ) ); ?></td>
                                    <td><?= amount_number_format_3( array_sum( $total_weight_plus ) ); ?></td>
                                    <td><?= amount_number_format_3( array_sum( $total_weight_minus ) ); ?></td>
                                    <td><?= amount_number_format_3( array_sum( $total_gross_weight ) ); ?></td>
                                    <td>---</td>
                                </tr>

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

                    <div class="row">
                        <div class="col-md-12 text-right">
                            <?php if($search_display == 'normal'){ ?>
                                <a href="<?= url('/lot-purchase-moved/hide-items'); ?>" class="btn btn-sm btn-primary hide-selected-lots-moved"><i class="fa fa-eye-slash">&nbsp; </i><?= print_lang($app_lang,'hide'); ?></a>
                            <?php }else{ ?>
                                <a href="<?= url('/lot-purchase-moved/show-items'); ?>" class="btn btn-sm btn-primary show-selected-lots-moved"><i class="fa fa-eye">&nbsp; </i><?= print_lang($app_lang,'show'); ?></a>
                            <?php } ?>
                        </div>
                    </div>

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