<style type="text/css">
    .blink_me {
      animation: blinker 0.6s linear infinite;
    }

    @keyframes blinker {
      50% {
        opacity: 0;
      }
</style>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2><?= print_lang($app_lang,'pending_sales'); ?></h2>
        <ol class="breadcrumb">
            <li>
                <a class="url-elem" href="<?php echo url('/dashboard'); ?>"><?= print_lang($app_lang,'home'); ?></a>
            </li>
            <li>
                <a class="url-elem" href="<?php echo url('/pending-sales'); ?>"><?= print_lang($app_lang,'pending_sales'); ?></a>
            </li>
        </ol>
    </div>
    <div class="col-lg-2">
    </div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox-title">
        <table style="width: 100%;">
            <tr>
                <?php
                    $account_data = fetch_table_where('accounts','uid',$editing_data['client'],true);
                ?>
                <td style="width: 50%; font-size: 18px;"><?= print_lang($app_lang,'out_clinet_name_title'); ?>:&nbsp;&nbsp;<?= ucwords($account_data['name']); ?></td>
                <td style="width: 50%; font-size: 18px;"><?= print_lang($app_lang,'out_invoice_title'); ?>:&nbsp;&nbsp;<a target="_blank" href="<?= url('/sales/invoice').'/'.$editing_data['uid']; ?>"><?= $editing_data['invoice']; ?></a></td>
            </tr>
        </table>
        <?php if(!empty(fetch_table_where('sales_return','sale_id',$editing_data['uid']))){?>
            <div class="alert alert-warning" style="margin-top: 5px;" role="alert">This sale has returns. Please checkout returns before updating items.</div>
        <?php } ?>
    </div>
    <div class="ibox-content">
        <!-- spinner -->
        <div class="sk-spinner sk-spinner-three-bounce">
            <div class="sk-bounce1"></div>
            <div class="sk-bounce2"></div>
            <div class="sk-bounce3"></div>
        </div>
        <!-- spinner -->
        <?php if(check_user_access('system_profit') === TRUE){ ?>
            <input type="hidden" name="system_profit" id="system_profit" value="yes">
        <?php }else{ ?>
            <input type="hidden" name="system_profit" id="system_profit" value="no">
        <?php } ?>

        <?php $edate = display_true_date( explode(' ',$editing_data['created_at'])[0] ); $edate = date('Y-m-d',strtotime($edate)); ?>

        <form role="form" id="sales-out-form" action="<?php echo url('/pending-sales/update').'/'.$editing_data['uid']; ?>">
            <div class="row" style="margin-top: 10px;">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th class="text-center"><?= print_lang($app_lang,'item'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'unit'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'cost'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'sale_price'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'qty'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'bonus'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'out_qty_total'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'out'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'discount'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'discount_2'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'profit'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'total'); ?></th>
                                </tr>
                            </thead>

                            <tbody id="sales-items-table-body" class="<?php if($item_settings['fuel_parts'] == 'enable'){ echo 'fuel-parts-are-enabled'; }?>">
                                <?php
                                $item_details = json_decode($editing_data['items_details'],true);
                                foreach($item_details as $row){
                                    if(strpos($row['item'],"MITNAME_") === FALSE){
                                        $sale_item_data = fetch_table_where('items','uid',$row['item'],true);
                                ?>
                                    <tr class="text-center sales-item-row">
                                        <td class="sales-item-self">
                                            <span><?= isset($sale_item_data['name']) ? $sale_item_data['name'] : NULL; ?></span>
                                            <input type="hidden" name="currency[]" value="<?= $editing_data['currency']; ?>">
                                            <input type="hidden" class="sales_cr_date" name="sdate[]" value="<?= true_date_for_inputs($edate); ?>">
                                            <input type="hidden" class="client_edit_input" name="client[]" value="<?= $editing_data['client']; ?>">
                                            <input type="hidden" class="marketer_edit_input" name="marketer[]" value="<?= $editing_data['marketer']; ?>">s
                                            <input type="hidden" name="invoice[]" value="<?= $editing_data['invoice']; ?>">
                                            <input type="hidden" class="memo_edit_input" name="memo[]" value="<?= $editing_data['memo']; ?>">
                                            <input type="hidden" name="item[]" class="sale-form-item-name" value="<?= $row['item']; ?>">
                                            <?php
                                            
                                                $item_uqid = isset($row['item_uqid']) ? $row['item_uqid'] : randomUniqeId();
                                            ?>
                                            <input type="hidden" name="item_uqid[]" value="<?= $item_uqid; ?>">
                                            <?php
                                                $sale_item_bonus = isset($row['bonus']) ? (float)$row['bonus'] : 0;
                                            ?>
                                            <!-- <input type="hidden" name="bonus[]" value="<?= $sale_item_bonus; ?>"> -->
                                            <input type="hidden" name="batch[]" value="<?= $row['batch']; ?>">
                                            <?php
                                                if(isset($row['mfg']))
                                                {
                                                    if(!empty($row['mfg']))
                                                    {
                                                        $mfg = $row['mfg'];
                                                    }
                                                    else
                                                    {
                                                        $mfg = '';
                                                    }
                                                }
                                                else
                                                {
                                                    $mfg = '';
                                                }
                                            ?>
                                            <input type="hidden" name="mfg[]" value="<?= $mfg; ?>">

                                            <?php
                                                if(isset($row['expiry']))
                                                {
                                                    if(!empty($row['expiry']))
                                                    {
                                                        $expiry = $row['expiry'];
                                                    }
                                                    else
                                                    {
                                                        $expiry = '';
                                                    }
                                                }
                                                else
                                                {
                                                    $expiry = '';
                                                }
                                            ?>
                                            <input type="hidden" name="expiry[]" value="<?= $expiry; ?>">
                                            <input type="hidden" name="warehouse[]" value="<?= $row['warehouse']; ?>">
                                            <input type="hidden" name="narration[]" value="<?= $row['narration']; ?>">

                                            <input type="hidden" name="plate[]" value="<?= isset($row['plate']) ? $row['plate'] : NULL; ?>">
                                            <input type="hidden" name="agent[]" value="<?= isset($row['agent']) ? $row['agent'] : NULL; ?>">

                                            <input type="hidden" name="m2[]" value="<?php if(isset($row['m2'])){echo $row['m2'];}else{echo '';}; ?>" class="item-m2-iput">
                                            <input type="hidden" name="width[]" value="<?php if(isset($row['width'])){echo $row['width'];}else{echo '';}; ?>" class="item-width-iput">
                                            <input type="hidden" name="height[]" value="<?php if(isset($row['height'])){echo $row['height'];}else{echo '';}; ?>" class="item-height-iput">
                                            <input type="hidden" name="cash_client_name[]" class="cash_client_name_input" value="<?= $editing_data['cash_client_name']; ?>">
                                            <input type="hidden" name="cash_client_mobile[]" class="cash_client_mobile_input" value="<?= $editing_data['cash_client_mobile']; ?>">

                                            <?php
                                                $sale_bundle = isset($row['bundle']) ? $row['bundle'] : NULL;
                                                $sale_toop = isset($row['toop']) ? $row['toop'] : NULL;
                                            ?>
                                            <input type="hidden" name="bundle[]" value="<?= $sale_bundle; ?>">
                                            <input type="hidden" name="toop[]" value="<?= $sale_toop; ?>">
                                        </td>
                                        <td class="sales-item-warehouse">
                                            <span>
                                                <?php
                                                    $sale_item_data = isset($sale_item_data['uid']) ? $sale_item_data : array('purchase_unit' => NULL, 'sale_unit' => NULL, 'production_unit' => NULL);
                                                    $sale_item_unit_name = NULL;
                                                    if($row['unit'] == 'purchase')
                                                    {
                                                        $sale_item_unit_name = array_key_exists($sale_item_data['purchase_unit'],$units) ? $units[$sale_item_data['purchase_unit']]['name'] : NULL;
                                                    }
                                                    if($row['unit'] == 'sale')
                                                    {
                                                        $sale_item_unit_name = array_key_exists($sale_item_data['sale_unit'],$units) ? $units[$sale_item_data['sale_unit']]['name'] : NULL;
                                                    }
                                                    if($row['unit'] == 'production')
                                                    {
                                                        $sale_item_unit_name = array_key_exists($sale_item_data['production_unit'],$units) ? $units[$sale_item_data['production_unit']]['name'] : NULL;
                                                    }
                                                    echo $sale_item_unit_name;
                                                ?>
                                            </span>
                                            <input type="hidden" name="unit[]" value="<?= $row['unit']; ?>">
                                        </td>
                                        <td class="sales-item-cost">
                                            <?php if(check_user_access('system_profit') === TRUE){ ?>
                                                <span><?= amount_number_format($row['cost']); ?></span>
                                            <?php }else{?>
                                                <span><?= amount_number_format(0); ?></span>
                                            <?php } ?>
                                            <input type="hidden" name="cost[]" value="<?= $row['cost']; ?>">
                                        </td>
                                        <td class="sales-item-sale-price">
                                            <input type="text" readonly style="border: none; outline: none;" name="sale_price[]" value="<?= $row['sale_price']; ?>" class="sales-editable-iputs">
                                        </td>
                                        <td class="sales-item-qty">
                                            <input type="text" readonly style="border: none; outline: none;" name="qty[]" value="<?= $row['qty'] + $sale_item_bonus; ?>" class="sales-editable-iputs">
                                        </td>

                                        <td class="sales-item-qty">
                                            <input type="text" readonly style="border: none; outline: none;" name="bonus[]" value="<?= $sale_item_bonus; ?>" class="sales-editable-iputs">
                                        </td>

                                        <?php
                                            $sale_item_out_qty = isset($row['out_qty']) ? (float)$row['out_qty'] : 0;
                                        ?>

                                        <td>
                                            <input type="text" readonly style="border: none; outline: none; width: 70px; text-align: center;" name="out_qty[]" value="<?= $sale_item_out_qty; ?>">
                                        </td>

                                        <td>
                                            <input type="text" name="out[]" style="width: 70px;">
                                        </td>

                                        <td class="sales-item-discount">
                                            <input type="text" readonly style="border: none; outline: none;" name="discount[]" value="<?= $row['discount']; ?>" class="sales-editable-iputs">
                                        </td>
                                        <td class="sales-item-discount-2">
                                            <?php if(isset($row['discount_2'])){ $discount_2 = (float)$row['discount_2']; }else{ $discount_2 = 0; }?>
                                            <input type="text" name="discount_2[]" readonly style="border: none; outline: none;" value="<?= $discount_2; ?>" class="sales-editable-iputs">
                                        </td>
                                        <td class="sales-item-profit">
                                            <?php if(check_user_access('system_profit') === TRUE){ ?>
                                                <span><?= amount_number_format($row['profit']); ?></span>
                                            <?php }else{?>
                                                <span><?= amount_number_format(0); ?></span>
                                            <?php } ?>
                                            <input type="hidden" name="profit[]" value="<?= $row['profit']; ?>">
                                        </td>
                                        <td class="sales-item-total">
                                            <span><?= amount_number_format($row['item_total']); ?></span>
                                            <input type="hidden" name="item_total[]" value="<?= $row['item_total']; ?>">
                                        </td>
                                        
                                    </tr>
                                <?php }else{ ?>
                                    <tr class="text-center sales-item-row">
                                        <td class="sales-item-self">
                                            <span><?= str_replace('MITNAME_', '', $row['item']); ?></span>
                                            <input type="hidden" name="currency[]" value="<?= $editing_data['currency']; ?>">
                                            <input type="hidden" name="sdate[]" value="<?= true_date_for_inputs($edate); ?>">
                                            <input type="hidden" name="client[]" value="<?= $editing_data['client']; ?>">
                                            <input type="hidden" name="marketer[]" value="<?= $editing_data['marketer']; ?>">
                                            <input type="hidden" name="invoice[]" value="<?= $editing_data['invoice']; ?>">
                                            <input type="hidden" name="memo[]" value="<?= $editing_data['memo']; ?>">
                                            <input type="hidden" name="item[]" class="sale-form-item-name" value="<?= $row['item']; ?>">
                                            <?php
                                                $item_uqid = isset($row['item_uqid']) ? $row['item_uqid'] : randomUniqeId();
                                            ?>
                                            <input type="hidden" name="item_uqid[]" value="<?= $item_uqid; ?>">
                                            <input type="hidden" name="batch[]" value="<?= $row['batch']; ?>">
                                            <input type="hidden" name="warehouse[]" value="<?= $row['warehouse']; ?>">
                                            <input type="hidden" name="narration[]" value="<?= $row['narration']; ?>">

                                            <input type="hidden" name="plate[]" value="<?= isset($row['plate']) ? $row['plate'] : NULL; ?>">
                                            <input type="hidden" name="agent[]" value="<?= isset($row['agent']) ? $row['agent'] : NULL; ?>">

                                            <input type="hidden" name="m2[]" value="<?php if(isset($row['m2'])){echo $row['m2'];}else{echo '';}; ?>" class="item-m2-iput">
                                            <input type="hidden" name="width[]" value="<?php if(isset($row['width'])){echo $row['width'];}else{echo '';}; ?>" class="item-width-iput">
                                            <input type="hidden" name="height[]" value="<?php if(isset($row['height'])){echo $row['height'];}else{echo '';}; ?>" class="item-height-iput">
                                            <input type="hidden" name="cash_client_name[]" value="<?= $editing_data['cash_client_name']; ?>">
                                            <input type="hidden" name="cash_client_mobile[]" value="<?= $editing_data['cash_client_mobile']; ?>">
                                        </td>
                                        <td class="sales-item-warehouse">
                                            <span><?= $row['unit']; ?></span>
                                            <input type="hidden" name="unit[]" value="<?= $row['unit']; ?>">
                                        </td>
                                        <td class="sales-item-cost">
                                            <?php if(check_user_access('system_profit') === TRUE){ ?>
                                                <span><?= amount_number_format($row['cost']); ?></span>
                                            <?php }else{?>
                                                <span><?= amount_number_format(0); ?></span>
                                            <?php } ?>
                                            <input type="hidden" name="cost[]" value="<?= $row['cost']; ?>">
                                        </td>
                                        <td class="sales-item-sale-price">
                                            <input type="text" readonly style="border: none; outline: none;" name="sale_price[]" value="<?= $row['sale_price']; ?>" class="sales-editable-iputs">
                                        </td>
                                        <td class="sales-item-qty">
                                            <input type="text" readonly style="border: none; outline: none;" name="qty[]" value="<?= $row['qty']; ?>" class="sales-editable-iputs">
                                        </td>

                                        <?php
                                            $sale_item_out_qty = isset($row['out_qty']) ? (float)$row['out_qty'] : 0;
                                        ?>
                                        <td>
                                            <input type="text" readonly style="border: none; outline: none;" style="width: 70px;" name="out_qty[]" value="<?= $sale_item_out_qty; ?>" class="sales-editable-iputs">
                                        </td>

                                        <td>
                                            <input type="text" style="width: 70px;" name="out[]">
                                        </td>
                                        <td class="sales-item-discount">
                                            <input type="text" readonly style="border: none; outline: none;" name="discount[]" value="<?= $row['discount']; ?>" class="sales-editable-iputs">
                                        </td>
                                        <td class="sales-item-profit">
                                            <?php if(check_user_access('system_profit') === TRUE){ ?>
                                                <span><?= amount_number_format($row['profit']); ?></span>
                                            <?php }else{?>
                                                <span><?= amount_number_format(0); ?></span>
                                            <?php } ?>
                                            <input type="hidden" name="profit[]" value="<?= $row['profit']; ?>">
                                        </td>
                                        <td class="sales-item-total">
                                            <span><?= amount_number_format($row['item_total']); ?></span>
                                            <input type="hidden" name="item_total[]" value="<?= $row['item_total']; ?>">
                                        </td>
                                        
                                    </tr>
                                <?php }} ?>
                            </tbody>
                        </table>    
                    </div>

                    <div class="row sales-under-table-totals">
                        <div class="col-md-3 total-sales-cel">
                            <strong><?= print_lang($app_lang,'total_sale'); ?>: </strong>
                            <span><?= round_decimels_digits($editing_data['total_sales_amount']); ?></span>
                            <input type="hidden" name="total_sales_amount" value="<?= $editing_data['total_sales_amount']; ?>">
                        </div>
                        <div class="col-md-3 total-diss-cel">
                            <strong><?= print_lang($app_lang,'total_discount'); ?>: </strong>
                            <span><?= round_decimels_digits($editing_data['total_discount_amount']); ?></span>
                            <input type="hidden" name="total_discount_amount" value="<?= $editing_data['total_discount_amount']; ?>">
                            <input type="hidden" name="total_discount_amount_1" class="sales-discount-amount-1" value="<?= $editing_data['total_discount_amount_1']; ?>">
                            <input type="hidden" name="total_discount_amount_2" class="sales-discount-amount-2" value="<?= $editing_data['total_discount_amount_2']; ?>">
                        </div>
                        <div class="col-md-3 gross-total-cel">
                            <strong><?= print_lang($app_lang,'gross_total'); ?>: </strong>
                            <span><?= round_decimels_digits($editing_data['gross_total_amount']); ?></span>
                            <input type="hidden" name="gross_total_amount" value="<?= $editing_data['gross_total_amount']; ?>">
                        </div>
                        <div class="col-md-3 total-profit-cel">
                            <strong><?= print_lang($app_lang,'total_profit'); ?>: </strong>
                            <?php if(check_user_access('system_profit') === TRUE){ ?>
                                <span><?= round_decimels_digits($editing_data['total_profit_amount']); ?></span>
                            <?php }else{ ?>
                                <span><?= round_decimels_digits(0); ?></span>
                            <?php } ?>
                            <input type="hidden" name="total_profit_amount" value="<?= $editing_data['total_profit_amount']; ?>">
                        </div>
                    </div>

                </div>
            </div>

            <div class="row extra-mar-botm" style="margin-top: 10px;">
                <div class="col-md-12 text-right">
                    <a class="btn btn-w-m btn-outline btn-danger url-elem" href="<?= url('/pending-sales'); ?>"><?= print_lang($app_lang,'cancel'); ?></a>
                    <button class="btn btn-w-m btn-primary" type="submit"><?= print_lang($app_lang,'update'); ?></button>
                </div>
            </div>
        </form>

    </div>

</div>