<style type="text/css">
    .chosen-container a.chosen-single{line-height: 24px !important;}
</style>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2><?= print_lang($app_lang,'mod_trans_out_edit_breadcrumb'); ?></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('/transfer-out'); ?>"><?= print_lang($app_lang,'mod_trans_out_view_breadcrumb'); ?></a>
            </li>
            <li class="active">
                <strong><?= print_lang($app_lang,'mod_trans_out_edit_breadcrumb'); ?></strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2">
    </div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox-title">
        <h5><?= print_lang($app_lang,'fields_with'); ?> <strong class="text-danger">*</strong> <?= print_lang($app_lang,'are_mandatory'); ?></h5>

        <div class="text-<?php if($app_direction == 'rtl'){echo 'left';}else{echo 'right';} ?>">
            <input style="width: 100px; border: none; background: #e0e0e0; padding: 9px 5px; text-align: center; border-radius: 2px; color: black;" type="text" class="pointer-none" value="<?= print_lang($app_lang,'add_entry_sr_num').' '.$editing_data['serial_id']; ?>" readonly="">
        </div>
    </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 -->
        <div class="row">
            <div class="col-sm-12 b-r">
                <form role="form" id="transfer-out-items-temp-form">

                    <div class="row " style="margin-bottom: 5px;">
                        <div class="col-md-10">
                            <div class="row">
                                <div class="col-md-2 transfer-out-fix-row pointer-none" style="padding: 2px;">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'currency'); ?> <strong class="text-danger">*</strong></label>
                                        <select class="form-control chosen-select" name="temp_currency">
                                            <option disabled="" selected="">-- <?= print_lang($app_lang,'mod_trans_out_add_input_currency'); ?> --</option>
                                            <?php foreach($currencies as $row){ ?>
                                                <option value="<?= $row['uid']; ?>" <?php if($editing_data['currency'] == $row['uid']){echo "selected='selected'";}?> data-usd-equalance="<?= $row['usd_equalance']; ?>"><?= $row['name']; ?> (<?= $row['symbol']; ?>)</option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'mod_trans_out_add_input_client'); ?> <strong class="text-danger">*</strong></label>
                                        <select class="form-control chosen-select temp_transfer_out_edit" name="temp_client">
                                            <option disabled="" selected="">-- <?= print_lang($app_lang,'mod_trans_out_add_input_client'); ?> --</option>
                                            <?php foreach($clients as $row){ ?>
                                                <option value="<?= $row['uid']; ?>" <?php if($editing_data['client'] == $row['uid']){echo "selected='selected'";}?>><?= $row['name']; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3" style="padding: 2px;">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'marketer'); ?> <strong class="text-danger">*</strong></label>
                                        <select class="form-control chosen-select temp_marketer_edit" name="temp_marketer" id="sales-add-marketer-dropdown">
                                            <option disabled="" selected="">-- <?= print_lang($app_lang,'marketer'); ?> --</option>
                                            <?php foreach($marketers as $row){ ?>
                                                <option <?php if($editing_data['marketer'] == $row['uid']){ echo 'selected="selected"'; } ?> value="<?= $row['uid']; ?>"><?= $row['name']; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-2" style="padding: 2px;">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'invoice'); ?> <strong class="text-danger">*</strong></label>
                                        <input type="text" class="form-control" name="temp_invoice" value="<?= $editing_data['invoice']; ?>" readonly placeholder="<?= print_lang($app_lang,'mod_trans_out_add_input_invoice'); ?>">
                                    </div>
                                </div>
                                <div class="col-md-2" style="padding: 2px;">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'mod_trans_out_add_input_date'); ?></label>
                                        <?php $sdate = display_true_date( explode(' ',$editing_data['created_at'])[0] ); $sdate = date('Y-m-d',strtotime($sdate)); ?>
                                        <?php if($branch_calander == 'persian'){ ?>
                                            <input type="text" class="form-control persian-date" name="temp_sdate" value="<?= true_date_for_inputs($sdate); ?>" placeholder="<?= print_lang($app_lang,'mod_trans_out_add_input_date'); ?>" readonly="" style="background: #fff;">
                                        <?php }else{ ?>
                                            <input type="date" class="form-control" name="temp_sdate" value="<?php if(!empty($sdate)){echo date("Y-m-d",strtotime(true_date_for_inputs($sdate))); } ?>">
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-2" style="padding: 2px;">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'memo'); ?></label>
                                <textarea style="height: 35px;" class="form-control temp_p_memo_edit" name="temp_memo" rows="1" placeholder="<?= print_lang($app_lang,'mod_trans_out_add_input_memo'); ?>"><?= $editing_data['memo']; ?></textarea>
                            </div>
                        </div>
                    </div>                    

                    <div class="row" style="margin-bottom: 5px;">
                        <div class="col-md-3">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'mod_trans_out_add_input_item'); ?> <strong class="text-danger">*</strong></label>
                                <select class="form-control chosen-select" name="temp_item">
                                    <option disabled="" selected="">-- <?= print_lang($app_lang,'mod_trans_out_add_input_item'); ?> --</option>
                                    <?php $icount = 1; foreach($items as $row){ ?>
                                        <?php
                                        $row['stats'] = empty($row['stats']) ? array() : json_decode($row['stats'],true);
                                        $ri_warehouse_wise_avail_units = $row['stats']['avail_units'];
                                        foreach($ri_warehouse_wise_avail_units as $warehouse_uid => $wau){
                                            $ri_warehouse_name = array_key_exists($warehouse_uid, $warehouse) ? $warehouse[$warehouse_uid]['name'] : NULL;
                                        ?>
                                            <option value="<?= $row['uid']; ?>" data-width="<?= $row['width']; ?>" data-height="<?= $row['height']; ?>" data-purchase-unit-equalance="<?= $row['purchase_unit_equalance']; ?>" data-sale-unit-equalance="<?= $row['sale_unit_equalance']; ?>" data-production-unit-equalance="<?= $row['production_unit_equalance']; ?>" data-purchase-unit="<?= array_key_exists($row['purchase_unit'],$units) ? $units[$row['purchase_unit']]['name'] : NULL; ?>" data-sale-unit="<?= array_key_exists($row['sale_unit'],$units) ? $units[$row['sale_unit']]['name'] : NULL; ?>" data-production-unit="<?= array_key_exists($row['production_unit'],$units) ? $units[$row['production_unit']]['name'] : NULL; ?>" data-usd-equalance="<?= $row['currency_usd_equalance']; ?>" data-item-currency="<?= $row['currency']; ?>" data-opt-ref="item-opt-<?= $icount++; ?>" data-item-name="<?= $row['name']; ?>" data-warehouse="<?= $warehouse_uid; ?>" data-warehouse-name="<?= $ri_warehouse_name; ?>" data-profit="<?= $row['stats']['last_profit']; ?>" data-cost="<?= $row['stats']['avg_cost']; ?>" data-avail-units="<?= $wau; ?>"><?= $row['name'].', '.$row['item_id']; ?> | <?= $ri_warehouse_name; ?> (Avail Units: <?= $wau; ?>)</option>
                                        <?php } ?>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'mod_trans_out_add_input_batch'); ?></label>
                                <select class="form-control chosen-select" name="temp_batch">
                                    <option disabled="" selected="">-- <?= print_lang($app_lang,'select_batch'); ?> --</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-1" style="padding: 2px;">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'qty'); ?> <strong class="text-danger">*</strong></label>
                                <input type="number" class="form-control" name="temp_qty" placeholder="<?= print_lang($app_lang,'qty'); ?>">
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'mod_trans_out_add_input_unit'); ?> <strong class="text-danger">*</strong></label>
                                <select class="form-control chosen-select" name="temp_unit">
                                    <option disabled="" selected="">-- <?= print_lang($app_lang,'mod_trans_out_add_input_unit'); ?> --</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-1" style="padding: 2px;">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'bonus'); ?></label>
                                <input type="number" class="form-control" name="temp_bonus" placeholder="<?= print_lang($app_lang,'bonus'); ?>">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'extra_narration'); ?></label>
                                <textarea style="height: 35px;" class="form-control" name="temp_narration" rows="1" placeholder="<?= print_lang($app_lang,'extra_narration'); ?>"></textarea>
                            </div>
                        </div>
                    </div>

                    <div class="row extra-mar-botm">
                        <div class="col-md-12 text-right">
                            <button style="width: 100%;" class="btn btn-w-m btn-primary" type="submit"><?= print_lang($app_lang,'mod_trans_out_add_input_add_to_list'); ?></button>
                        </div>
                    </div>

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

        <form role="form" id="transfer-out-form" action="<?php echo url('/transfer-out/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,'mod_trans_out_add_input_list_item'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_out_add_input_list_unit'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_out_add_input_list_cost'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_out_add_input_list_sale_price'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_out_add_input_list_qty'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_out_add_input_list_dis_1'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_out_add_input_list_dis_2'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_out_add_input_list_profit'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_out_add_input_list_total'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_out_add_input_list_action'); ?></th>
                                </tr>
                            </thead>
                            <tbody id="transfer-out-items-table-body">
                                <?php
                                    $transfer_in_items_db = json_decode($editing_data['items'],true);
                                    if(count($transfer_in_items_db) > 0){
                                ?>
                                <?php foreach($transfer_in_items_db as $row){ ?>
                                    <tr class="text-center transfer-out-item-row">
                                        <td class="transfer-out-item-self">
                                            <?= array_key_exists($row['item'], $items) ? $items[$row['item']]['name'] : NULL; ?>
                                            <input type="hidden" name="item[]" class="sale-form-item-name" value="<?= $row['item']; ?>">
                                            <input type="hidden" name="currency[]" value="<?= $editing_data['currency']; ?>">
                                            <input type="hidden" name="sdate[]" value="<?= $sdate; ?>">
                                            <input type="hidden" name="client[]" class="client_edit_input" 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[]" class="memo_p_edit_input" value="<?= $editing_data['memo']; ?>">
                                            <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="bonus[]" value="<?= $row['bonus']; ?>">
                                            <input type="hidden" name="warehouse[]" value="<?= $row['warehouse']; ?>">
                                            <input type="hidden" name="narration[]" value="<?= $row['narration']; ?>">
                                        </td>
                                        <td>
                                            <?php
                                                $transfer_in_item_data = array_key_exists($row['item'], $items) ? $items[$row['item']] : array('purchase_unit' => NULL, 'sale_unit' => NULL, 'production_unit' => NULL);
                                                $transfer_in_item_unit_name = NULL;
                                                if($row['unit'] == 'purchase')
                                                {
                                                    $transfer_in_item_unit_name = array_key_exists($transfer_in_item_data['purchase_unit'],$units) ? $units[$transfer_in_item_data['purchase_unit']]['name'] : NULL;
                                                }
                                                if($row['unit'] == 'sale')
                                                {
                                                    $transfer_in_item_unit_name = array_key_exists($transfer_in_item_data['sale_unit'],$units) ? $units[$transfer_in_item_data['sale_unit']]['name'] : NULL;
                                                }
                                                if($row['unit'] == 'production')
                                                {
                                                    $transfer_in_item_unit_name = array_key_exists($transfer_in_item_data['production_unit'],$units) ? $units[$transfer_in_item_data['production_unit']]['name'] : NULL;
                                                }
                                                echo $transfer_in_item_unit_name;
                                            ?>
                                            <input type="hidden" name="unit[]" value="<?= $row['unit']; ?>">
                                        </td>
                                        <td class="transfer-out-item-cost">
                                            <?= amount_number_format($row['cost']); ?>
                                            <input type="hidden" name="cost[]" value="<?= $row['cost']; ?>">
                                        </td>
                                        <td class="transfer-out-item-sale-price">
                                            <input type="text" name="sale_price[]" style="width:75px;" value="<?= $row['sale_price']; ?>" class="transfer-out-editable-iputs">
                                        </td>
                                        <td class="transfer-out-item-qty">
                                            <input type="number" name="qty[]" style="width:75px;" value="<?= $row['qty']; ?>" class="transfer-out-editable-iputs">
                                        </td>
                                        <td class="transfer-out-item-discount">
                                            <input type="number" name="discount[]" style="width:75px;" value="<?= $row['discount']; ?>" class="transfer-out-editable-iputs">
                                        </td>
                                        <td class="transfer-out-item-discount-2">
                                            <input type="number" name="discount_2[]" style="width:75px;" value="<?= $row['discount_2']; ?>" class="transfer-out-editable-iputs">
                                        </td>
                                        <td class="transfer-out-item-profit">
                                            <?= amount_number_format($row['profit']); ?>
                                            <input type="hidden" name="profit[]" value="<?= $row['profit']; ?>">
                                        </td>
                                        <td class="transfer-out-item-total">
                                            <span><?= amount_number_format($row['item_total']); ?></span>
                                            <input type="hidden" name="item_total[]" value="<?= $row['item_total']; ?>">
                                        </td>
                                        <td><a class="btn btn-danger btn-sm transfer-out-item-del-trigger" href="javascript:void(0);"><i class="fa fa-trash"></i></a></td>
                                    </tr>
                                <?php } } ?>
                            </tbody>
                        </table>    
                    </div>
                </div>

                <div class="col-md-12 transfer-out-under-table-totals">
                    <div class="row">
                        <div class="col-md-2 total-transfer-out-cel">
                            <strong><?= print_lang($app_lang,'mod_trans_out_add_input_total_trans_in'); ?>: </strong>
                            <span class="cals-span"><?= amount_number_format($editing_data['grand_total']); ?></span>
                            <input class="cals-input" type="hidden" name="grand_total" value="<?= $editing_data['grand_total']; ?>">
                        </div>
                        <div class="col-md-2 total-discount-transfer-out-cel">
                            <strong><?= print_lang($app_lang,'mod_trans_out_add_input_discount_trans_in'); ?>: </strong>
                            <span class="cals-span"><?= amount_number_format($editing_data['total_discount_amount']); ?></span>
                            <input class="cals-input" type="hidden" name="total_discount_amount" value="<?= $editing_data['total_discount_amount']; ?>">
                            <input type="hidden" name="total_discount_amount_1" class="transfer-out-discount-amount-1" value="<?= $editing_data['total_discount_amount_1']; ?>">
                            <input type="hidden" name="total_discount_amount_2" class="transfer-out-discount-amount-2" value="<?= $editing_data['total_discount_amount_2']; ?>">
                        </div>
                        <div class="col-md-2 total-profit-cel">
                            <strong><?= print_lang($app_lang,'mod_trans_out_add_input_total_profit'); ?>: </strong>
                            <span class="cals-span"><?= amount_number_format($editing_data['profit']); ?></span>
                            <input class="cals-input" type="hidden" name="total_profit" value="<?= $editing_data['profit']; ?>">
                        </div>
                        <div class="col-md-2 total-gross-transfer-out-cel">
                            <strong><?= print_lang($app_lang,'mod_trans_out_add_input_gross_trans_in'); ?>: </strong>
                            <span class="cals-span"><?= amount_number_format($editing_data['gross_total_amount']); ?></span>
                            <input class="cals-input" type="hidden" name="gross_total_amount" value="<?= $editing_data['gross_total_amount']; ?>">
                        </div>
                        <div class="col-md-2 transfer-out-cash-received-amount-row">
                            <strong><?= print_lang($app_lang,'mod_trans_out_add_input_cash_received'); ?>: </strong>
                            <input class="cals-input" type="number" name="cash_received" value="<?= $editing_data['cash_received']; ?>" style="width: 80px;position: relative;bottom: 4px;border: none;border-bottom: 1px solid #555;">
                            <select class="form-group chosen-select" name="cash_received_account">
                                <option disabled="" selected="">- <?= print_lang($app_lang,'mod_trans_out_add_input_account'); ?> -</option>
                                <?php foreach($accounts as $row){ ?>
                                <?php if($row['type'] == 'bank' OR $row['type'] == 'cashbook'){?>
                                    <option value="<?= $row['uid']; ?>" <?php if($editing_data['cash_received_account'] == $row['uid']){echo "selected='selected'";}?>><?= $row['name']; ?></option>
                                <?php }} ?>
                            </select>
                        </div>
                        <div class="col-md-2 transfer-out-remaining-amount-row">
                            <strong><?= print_lang($app_lang,'mod_trans_out_add_input_pending'); ?>: </strong>
                            <span class="cals-span"><?= amount_number_format($editing_data['pending_amount']); ?></span>
                            <input class="cals-input" type="hidden" name="pending_amount" value="<?= $editing_data['pending_amount']; ?>">
                        </div>
                        <div class="col-md-12" style="margin-top: 20px;">
                            <input type="submit" style="width: 100%;" class="btn btn-primary" value="<?= print_lang($app_lang,'save_print'); ?>">
                        </div>
                    </div>
                </div>

            </div>
        </form>

    </div>

</div>