<?php
$item_settings = get_item_settings();
$item_details = json_decode($set_data['items_details'],true);
$edate = display_true_date( explode(' ',$editing_data['sdate'])[0]); $edate = date('Y-m-d',strtotime($edate));
foreach($item_details as $row){
    $sale_item_data = fetch_table_where('items','uid',$row['item'],true);
    $item_stats = json_decode($sale_item_data['stats'],true);
    $item_cost = isset($item_stats['avg_cost']) ? (float)$item_stats['avg_cost'] : 0;
    $item_profit = isset($item_stats['last_profit']) ? (float)$item_stats['last_profit'] : 0;
    $item_sale_price = $item_cost + $item_profit;
    // currency conversion
    $item_cost = amount_conversion($item_cost, $sale_item_data['currency_usd_equalance'], $editing_data['currency_usd_equalance']);
    $item_profit = amount_conversion($item_profit, $sale_item_data['currency_usd_equalance'], $editing_data['currency_usd_equalance']);
    $item_sale_price = amount_conversion($item_sale_price, $sale_item_data['currency_usd_equalance'], $editing_data['currency_usd_equalance']);
    // currency conversion

    // unit conversion
    $item_cost = amount_unit_based_conversion($sale_item_data,$row['unit'],$item_cost);
    $item_profit = amount_unit_based_conversion($sale_item_data,$row['unit'],$item_profit);
    $item_sale_price = amount_unit_based_conversion($sale_item_data,$row['unit'],$item_sale_price);
    // unit conversion
    $item_total = $item_sale_price * $row['qty'];
?>
    <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="set_name[]" value="<?= $set_data['name']; ?>">
            <input type="hidden" name="set_uid[]" value="<?= $set_data['uid']; ?>">
            <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="<?= isset($editing_data['marketer']) ? $editing_data['marketer'] : null; ?>">
            <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; ?>">
            <input type="hidden" name="bonus[]" value="0">
            <input type="hidden" name="batch[]" value="<?= $row['batch']; ?>">
            <input type="hidden" name="out_qty[]" value="0">
            <input type="hidden" name="out[]" value="0">
            <?php
                if(isset($sale_item_data['mfg_date']))
                {
                    if(!empty($sale_item_data['mfg_date']))
                    {
                        $mfg = $sale_item_data['mfg_date'];
                    }
                    else
                    {
                        $mfg = '';
                    }
                }
                else
                {
                    $mfg = '';
                }
            ?>
            <input type="hidden" name="mfg[]" value="<?= $mfg; ?>">

            <?php
                if(isset($sale_item_data['expiry_date']))
                {
                    if(!empty($sale_item_data['expiry_date']))
                    {
                        $expiry = $sale_item_data['expiry_date'];
                    }
                    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="<?= isset($row['narration']) ? $row['narration'] : NULL; ?>">

            <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_mob']; ?>">
        </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($item_cost); ?></span>
            <?php }else{?>
                <span><?= amount_number_format(0); ?></span>
            <?php } ?>
            <input type="hidden" name="cost[]" value="<?= $item_cost; ?>">
        </td>
        <td class="sales-item-sale-price">
            <input type="text" name="sale_price[]" value="<?= $item_sale_price; ?>" class="sales-editable-iputs">
        </td>
        <td class="sales-item-qty">
            <input type="text" name="qty[]" value="<?= $row['qty']; ?>" class="sales-editable-iputs">
        </td>
        <td class="sales-item-discount">
            <input type="text" name="discount[]" value="<?= 0; ?>" class="sales-editable-iputs">
        </td>
        <?php if($item_settings['distribution'] == 'enable'){ ?>
            <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[]" value="<?= $discount_2; ?>" class="sales-editable-iputs">
            </td>
        <?php } ?>
        <td class="sales-item-profit">
            <?php if(check_user_access('system_profit') === TRUE){ ?>
                <span><?= amount_number_format($item_profit); ?></span>
            <?php }else{?>
                <span><?= amount_number_format(0); ?></span>
            <?php } ?>
            <input type="hidden" name="profit[]" value="<?= $item_profit; ?>">
        </td>
        <?php
            $sale_bundle = isset($row['bundle']) ? $row['bundle'] : NULL;
            $sale_toop = isset($row['toop']) ? $row['toop'] : NULL;
        ?>
        <?php if($item_settings['bundle_toop'] == 'enable'){ ?>
            <td><input type="text" name="bundle[]" value="<?= $sale_bundle; ?>" style="width: 60px;"></td>
            <td><input type="text" name="toop[]" value="<?= $sale_toop; ?>" style="width: 60px;"></td>
        <?php } ?>
        <td class="sales-item-total">
            <span><?= amount_number_format($item_total); ?></span>
            <input type="hidden" name="item_total[]" value="<?= $item_total; ?>">
        </td>
        <td>
            <a class="btn btn-danger btn-sm sales-item-del-trigger" href="javascript:void(0);"><i class="fa fa-trash"></i></a>
        </td>
    </tr>
<?php } ?>