<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_in_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-in'); ?>"><?= print_lang($app_lang,'mod_trans_in_view_breadcrumb'); ?></a>
            </li>
            <li class="active">
                <strong><?= print_lang($app_lang,'mod_trans_in_edit_breadcrumb'); ?></strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2">
    </div>
</div>

<span class="display-none" id="batch-items-json"><?= $batch_valid; ?></span>

<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="temp-transfer-in-form">

                    <div class="row " style="margin-bottom: 5px;">
                        <div class="col-md-8">
                            <div class="row">
                                <div class="col-md-3 fixed-fields-row pointer-none">
                                    <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_in_add_input_currency'); ?> --</option>
                                            <?php foreach($currencies as $row){ ?>
                                                <option value="<?= $row['uid']; ?>" <?php if($editing_data['currency'] == $row['uid']){echo "selected='selected'";}?>><?= $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_in_add_input_client'); ?> <strong class="text-danger">*</strong></label>
                                        <select class="form-control chosen-select temp_transfer_in_edit" name="temp_client">
                                            <option disabled="" selected="">-- <?= print_lang($app_lang,'mod_trans_in_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">
                                    <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_in_add_input_invoice'); ?>">
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'mod_trans_in_add_input_date'); ?></label>
                                        <?php $edate = display_true_date( explode(' ',$editing_data['created_at'])[0] ); $edate = date('Y-m-d',strtotime($edate)); ?>
                                        <?php if($branch_calander == 'persian'){ ?>
                                            <input type="text" class="form-control persian-date" name="temp_pdate" value="<?= true_date_for_inputs($edate); ?>" placeholder="<?= print_lang($app_lang,'mod_trans_in_add_input_date'); ?>" readonly="" style="background: #fff;">
                                        <?php }else{ ?>
                                            <input type="date" class="form-control" name="temp_pdate" value="<?php if(!empty($edate)){ echo date("Y-m-d",strtotime(true_date_for_inputs($edate))); } ?>">
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <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_in_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_in_add_input_item'); ?> <strong class="text-danger">*</strong></label>
                                <select class="form-control chosen-select" name="temp_item" id="transfer-in-temp-item">
                                    <option disabled="" selected="">-- <?= print_lang($app_lang,'mod_trans_in_add_input_item'); ?> --</option>
                                    <?php foreach($items as $row){
                                        $item_stats = json_decode($row['stats'],true);
                                        $item_tp = $item_stats['avg_cost'] + $item_stats['last_profit'];                                        
                                    ?>
                                        <option value="<?= $row['uid']; ?>" data-tp="<?= $item_tp; ?>" data-last-cost="<?= $item_stats['last_cost']; ?>" data-last-profit="<?= $item_stats['last_profit']; ?>" data-width="<?= $row['width']; ?>" data-height="<?= $row['height']; ?>" 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-opt-type="<?= $row['type']; ?>"><?= $row['name'].', '.$row['item_id']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'mod_trans_in_add_input_warehouse'); ?> <strong class="text-danger">*</strong></label>
                                <select class="form-control chosen-select" name="temp_warehouse">
                                    <option disabled="" selected="" data-opt-type="default">-- <?= print_lang($app_lang,'mod_trans_in_add_input_warehouse'); ?> --</option>
                                    <?php foreach($warehouse as $row){ ?>
                                        <option class="display-none" value="<?= $row['uid']; ?>" data-opt-type="<?= $row['type']; ?>"><?= $row['name']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'mod_trans_in_add_input_batch'); ?> </label>
                                <input type="text" class="form-control" name="temp_batch" placeholder="<?= print_lang($app_lang,'mod_trans_in_add_input_batch'); ?>">
                            </div>
                        </div>

                        <div class="col-md-2" style="padding: 2px;">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'mod_trans_in_add_input_mfg'); ?></label>
                                <?php if($branch_calander == 'persian'){ ?>
                                    <input type="text" class="form-control persian-date" name="temp_mfg" placeholder="<?= print_lang($app_lang,'mod_trans_in_add_input_mfg'); ?>" readonly="" style="background: #fff;">
                                <?php }else{ ?>
                                    <input type="date" class="form-control" name="temp_mfg">
                                <?php } ?>
                            </div>
                        </div>

                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'mod_trans_in_add_input_expiry'); ?></label>
                                <?php if($branch_calander == 'persian'){ ?>
                                    <input type="text" class="form-control persian-date" name="temp_expiry" placeholder="<?= print_lang($app_lang,'mod_trans_in_add_input_expiry'); ?>" readonly="" style="background: #fff;">
                                <?php }else{ ?>
                                    <input type="date" class="form-control" name="temp_expiry">
                                <?php } ?>
                            </div>
                        </div>
                    </div>

                    <div class="row" style="margin-bottom: 5px;">
                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'mod_trans_in_add_input_cost'); ?> <strong class="text-danger">*</strong></label>
                                <input type="number" class="form-control" name="temp_cost" placeholder="<?= print_lang($app_lang,'mod_trans_in_add_input_cost'); ?>">
                            </div>
                        </div>
                        <div class="col-md-2">
                            <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,'bonus'); ?></label>
                                <input type="number" class="form-control" name="temp_bonus" placeholder="<?= print_lang($app_lang,'bonus'); ?>">
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'mod_trans_in_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_in_add_input_unit'); ?> --</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'mod_trans_in_add_input_profit'); ?> <strong class="text-danger">*</strong></label>
                                <input type="number" class="form-control" name="temp_profit" placeholder="<?= print_lang($app_lang,'mod_trans_in_add_input_profit'); ?>">
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'tp_field'); ?><strong class="text-danger">*</strong></label>
                                <input type="number" class="form-control" name="temp_tp" placeholder="<?= print_lang($app_lang,'tp_field'); ?>">
                            </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_in_add_input_add_to_list'); ?></button>
                        </div>
                    </div>

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


        <form role="form" id="transfer-in-form" action="<?php echo url('/transfer-in/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_in_add_input_list_item'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_in_add_input_list_unit'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_in_add_input_list_qty'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_in_add_input_list_cost'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_in_add_input_list_profit'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_in_add_input_list_sale_price'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_in_add_input_list_dis_1'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_in_add_input_list_dis_2'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'tp_thead'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_in_add_input_list_total'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'mod_trans_in_add_input_list_action'); ?></th>
                                </tr>
                            </thead>
                            <tbody id="transfer-in-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-in-item-row">
                                        <td class="td-self-transfer-in">
                                            <?= array_key_exists($row['item'], $items) ? $items[$row['item']]['name'] : NULL; ?>
                                            <input type="hidden" name="currency[]" value="<?= $editing_data['currency']; ?>">
                                            <input type="hidden" class="tr_in_date" name="pdate[]" value="<?= true_date_for_inputs($edate); ?>">
                                            <input type="hidden" name="client[]" class="client_edit_input" value="<?= $editing_data['client']; ?>">
                                            <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="warehouse[]" value="<?= $row['warehouse']; ?>">
                                            <input type="hidden" name="batch[]" value="<?= $row['batch']; ?>">
                                            <input type="hidden" name="expiry[]" value="<?= $row['expiry']; ?>">
                                            <?php
                                                $transfer_in_mfg = isset($row['mfg']) ? $row['mfg'] : NULL;
                                            ?>
                                            <input type="hidden" name="mfg[]" value="<?= $transfer_in_mfg; ?>">
                                            <input type="hidden" name="item[]" value="<?= $row['item']; ?>">
                                            <?php
                                                $transfer_in_bonus = isset($row['bonus']) ? $row['bonus'] : 0;
                                            ?>
                                            <input type="hidden" name="bonus[]" value="<?= $transfer_in_bonus; ?>">
                                            <?php
                                                $first_cost = isset($row['first_cost']) ? $row['first_cost'] : $row['cost'];
                                            ?>
                                            <input type="hidden" name="first_cost[]" class="first-cost" value="<?= $first_cost; ?>">
                                        </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="qty-col" style="width: 60px;">
                                            <?= amount_number_format($row['qty']); ?>
                                            <input type="hidden" name="qty[]" value="<?= $row['qty']; ?>">
                                        </td>
                                        <td class="cost-col"><span><?= $row['cost']; ?></span><input type="hidden" style="width: 75px;" class="transfer-in-editable-iputs" name="cost[]" value="<?= $row['cost']; ?>"></td>
                                        <td class="profit-col"><input type="number" style="width: 75px;" class="transfer-in-editable-iputs" name="profit[]" value="<?= $row['profit']; ?>"></td>
                                        <td class="sale-price-col">
                                            <span><?= amount_number_format($row['sale_price']); ?></span> 
                                            <input type="hidden" name="sale_price[]" value="<?= $row['sale_price']; ?>">
                                        </td>
                                        <?php
                                            $discount = isset($row['discount']) ? $row['discount'] : NULL;
                                        ?>
                                        <td class="transfer-in-item-discount">
                                            <input type="number" style="width: 80px;" name="discount[]" value="<?= $discount; ?>" class="transfer-in-editable-iputs">
                                        </td>
                                        <?php
                                            $discount_2 = isset($row['discount_2']) ? $row['discount_2'] : NULL;
                                        ?>
                                        <td class="transfer-in-item-discount-2">
                                            <input type="number" style="width: 80px;" name="discount_2[]" value="<?= $discount_2; ?>" class="transfer-in-editable-iputs">
                                        </td>

                                        <td class="tp-col"><input type="text" style="width: 75px;" class="transfer-in-editable-iputs" name="tp[]" value="<?= $row['tp']; ?>"></td>
                                        <td class="total-amount-col">
                                            <span><?= amount_number_format($row['total']); ?></span> 
                                            <input type="hidden" name="total[]" value="<?= $row['total']; ?>">
                                        </td>
                                        <td style="width: 40px;"><a class="btn btn-danger btn-sm transfer-in-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-in-totals-col">
                    <div class="row">
                        <div class="col-md-3 transfer-in-total-cel transfer-in-total-amount-row">
                            <strong><?= print_lang($app_lang,'mod_trans_in_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-3 transfer-in-total-cel transfer-in-total-discount-row">
                            <strong><?= print_lang($app_lang,'total_discount'); ?>: </strong>
                            <span><?= amount_number_format($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="transfer-in-discount-amount-1" value="<?= $editing_data['total_discount_amount_1']; ?>">
                            <input type="hidden" name="total_discount_amount_2" class="transfer-in-discount-amount-2" value="<?= $editing_data['total_discount_amount_2']; ?>">
                        </div>
                        <div class="col-md-2 transfer-in-total-cel transfer-in-sub-total-amount-row">
                            <strong><?= print_lang($app_lang,'mod_trans_in_add_input_sub_total'); ?>: </strong>
                            <span class="cals-span"><?= amount_number_format($editing_data['sub_total']); ?></span>
                            <input class="cals-input" type="hidden" name="sub_total" value="<?= $editing_data['sub_total']; ?>">
                        </div>
                        <div class="col-md-2 transfer-in-total-cel transfer-in-paid-amount-row">
                            <strong><?= print_lang($app_lang,'paid'); ?>: </strong>
                            <input class="cals-input" type="number" name="paid_amount" value="<?= $editing_data['cash_paid_amount']; ?>" style="width: 80px;position: relative;bottom: 4px;border: none;border-bottom: 1px solid #555;">
                            <select class="form-group chosen-select" name="cash_paid_account">
                                <option disabled="" selected="">- <?= print_lang($app_lang,'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_paid_account'] == $row['uid']){echo "selected='selected'";}?>><?= $row['name']; ?></option>
                                <?php }} ?>
                            </select>
                        </div>
                        <div class="col-md-2 transfer-in-total-cel transfer-in-remaining-amount-row">
                            <strong><?= print_lang($app_lang,'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,'mod_trans_in_edit_update'); ?>">
                        </div>
                    </div>
                </div>

            </div>
        </form>

    </div>

</div>