<?= $this->extend('layout'); ?>

<?= $this->section('css'); ?>
<link rel="stylesheet" href="/css/mawim.css">
<?= $this->endSection(); ?>
<?= $this->section('menu'); ?>
<?= $this->include('sidebar') ?>
<?= $this->endSection(); ?>
<?= $this->section('content'); ?>

<!-- Basic Vertical form layout section start -->
<section id="basic-vertical-layouts">
    <div class="row match-height">
        <div class="col-md-8 col-12">
            <div class="card">
                <div class="card-header pb-0">
                    <?php if ($id) : ?>
                        <h4 class="card-title"><?= ($transfer?'Transfer bearbeiten':($isScheduled?'Terminbuchung bearbeiten':'Rechnung bearbeiten'))?></h4>
                    <?php else: ?>
                        <h4 class="card-title"><?= ($transfer?'Neuer Transfer':($isScheduled?'Neue Terminbuchung':'Neue Rechnung'))?></h4>
                    
                    <?php endif; ?>
                    <?php if (! empty($validation)) : ?>
                        <div class='alert alert-danger mt-2'>
                            <div class="errors" role="alert">
                                <ul>
                                <?php foreach ($validation as $error) : ?>
                                    <li><?= esc($error) ?></li>
                                <?php endforeach ?>
                                </ul>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
                <div class="card-content">
                    <div class="card-body">
                        <?= form_open_multipart('newBill','class="form form-vertical" onsubmit="logData(event)" id="newbill"') ?>            
<!--                        <form class="form form-vertical" id="newbill" method="post" enctype="multipart/form-data" onsubmit="logData(event)" action="<?= base_url() ?>newBill"> -->
                            <input type="hidden" value="<?= $id ?>" name="id">
                            <input type="hidden" value="<?= $transfer?'1':'0' ?>" name="transfer">
                            <div class="form-body">
                                <div class="row">
                                    <div class="col-12 <?= ($transfer?' d-none':'') ?>">
                                        <div class="form-check form-switch">
                                            <label class="form-check-label" for="multi">Multi Rechnung</label>
                                            <input class="form-check-input" type="checkbox" id="multi" value="on" name="multi" <?= set_checkbox('multi', 'on', $multi??false) ?>>                                           
                                        </div>
                                    </div>
                                    <div class="col-12 <?= ($transfer?' d-none':($isScheduled?' d-none':'')) ?>">
                                        <label for="billfile">Rechnung:</label>
                                        <input type="file" id="billfile" name="billfile" class="form-control <?= (!isset($billfile))?'':'d-none' ?>" capture="environment" accept="image/*, application/pdf">
                                        <?php if (isset($billfile)): ?>
                                            <a href="/viewBill/<?= $billfile ?>" id="billfileRef"><?= $billfile ?></a>
                                            <a class="" onclick="removeBillFile(this);"><i class="fa fa-trash"></i></a>
                                        <?php endif; ?>
                                    </div>
                                    <div class="col-12 <?= ($transfer?' d-none':'d-none') ?>">
                                        <div class='form-check'>
                                            <div class="checkbox">
                                                <label for="renummer">Re-Nr.</label>
                                                <input type="checkbox" id="renummer" name="renummer" value="on"  class='form-check-input' <?= set_checkbox('renummer', 'on', $renummer??false) ?>>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-12">
                                        <label for="datum">Datum</label>
                                            <input type="date" class="form-control" id="datum" name="datum" value="<?= $datum ?>" onclick="openBox(this);"/>
                                        </div>
                                    </div>
                                    <div class="col-12 <?= ($transfer?' d-none':'') ?>">
                                        <div class="form-group">
                                            <label for="receiver">Empfänger</label>
                                            <input type="text" class="form-control" id="receiver" name="receiver" value="<?= $receiver ?>">
                                        </div>
                                    </div>
                                    <div class="col-12">
                                        <div class="form-group">
                                            <label for="source">Konto:</label>
                                            <?= form_dropdown('source', $sourcelist, $source,'id="source" class="form-select"'); ?>
                                        </div>
                                    </div>
                                    <div class="col-12 multi">
                                        <div class="input-group">
                                            <input type="text" class="form-control dt-amount" id="total_in" name="total_in" placeholder="Einnahme" maxlength=120 value="<?= $total_in ?>">
                                            <input type="text" class="form-control dt-amount text-danger" id="total_out" name="total_out" placeholder="Ausgabe" maxlength=120 value="<?= $total_out ?>">
                                        </div>
                                    </div>
                                    <div class="col-12 scheduled <?=($isScheduled?'':'d-none')?>">
                                        <div class="form-group">
                                            <label for="scheduledNum">Scheduled:</label>
                                            <div class="input-group">
                                                <input type="hidden" value="<?= $isScheduled?'1':'0' ?>" name="scheduled">
                                                <?= form_dropdown("scheduledNum", $scheduled['NumList'], $scheduledNum,$scheduled['style']); ?>
                                                <?= form_dropdown("scheduledType",$scheduled['TypeList'], $scheduledType,$scheduled['style']); ?>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-12 d-none multi">
                                        <div class="form-group">
                                            <label for="openamount">Offener Betrag:</label>
                                            <input type="text" class="form-control dt-amount" readonly="readonly" id="openamount" name="openamount">
                                        </div>
                                    </div>
                                    <div class="col-12" id="linecont">
                                        <?php for ($i=0;$i<10;$i++): ?>    
                                            <div class="row <?= (($i<3 || $input[$i]>0 || $output[$i]>0)?'':' d-none')?>">
                                                <input type="hidden" value="<?= $sid[$i] ?>" name="sid[<?=$i?>]">
                                                
                                                <div class="col-lg-3 col-6 col-md-6 <?=(($i>0)?'multi':'')?> order-sm-2 px-0">
                                                    <?= form_dropdown("category_parent[$i]", $subsparent[$i], $category_parent[$i], 'class="form-select parent"'); ?>
                                                </div>
                                                <div class="col-lg-3 col-6 col-md-6 <?=(($i>0)?'multi':'')?> order-sm-2 px-0 sub">
                                                    <?= form_dropdown("category[$i]", $subscategory[$i], $category[$i], 'class="form-select"'); ?>
                                                </div>
                                                <div class="w-100 d-md-none"></div>
                                                <div class="col-lg-2 col-6 <?=(($i>0)?'multi':'')?>  <?= ($transfer?' d-none':'') ?> px-0 order-sm-2">
                                                    <input type="text" name="input[<?=$i?>]" class="form-control dt-subamount" placeholder="Einnahme" value="<?= $input[$i] ?>">
                                                </div>
                                                <div class="<?= ($transfer?'col-lg-4 col-12':'col-lg-2 col-6') ?> <?=(($i>0)?'multi':'')?> px-0 order-sm-2">
                                                    <input type="text" name="output[<?=$i?>]" class="form-control dt-subamount text-danger" placeholder="Ausgabe" value="<?= $output[$i] ?>">
                                                </div>
                                                <div class="col-lg-2 col-12 col-md-12 <?=(($i>0)?'multi':'')?> order-sm-1 px-0 mb-3 mb-md-0">
                                                    <input type="text" name="comment[<?=$i?>]" class="form-control" placeholder="Kommentar" value="<?= $comment[$i] ?>">
                                                </div>
                                            </div>
                                        <?php endfor; ?>
                                    </div>
                                    <div class="col-12 d-flex" role="group">
                                        <a class="btn btn-info multi" onclick="addLine();" role="button">+ Zeile</a>
                                        <input type="checkbox" id="validate" name="validate" value="1" class='btn-check' autocomplete="off"  <?= set_checkbox('validate', '1', (($validate??"f")=="t")?true:false) ?>>
                                        <label class="btn btn-outline-danger" for="validate">Entwurf</label>
                                        <?php if ($id) : ?>
                                        <a class="btn btn-danger" data-bs-toggle="modal" data-bs-id="<?= $id ?>" data-bs-target="#mymodal"><i class="fa fa-trash"></i></a>
                                        <?php endif; ?>
                                        <button type="submit" class="btn btn-primary">Submit</button>
                                        <a class="btn btn-secondary" onclick="history.back();" role="button"><i class="fa-solid fa-rotate-left"></i></a>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
</section>
<?= $this->endSection(); ?>
<?= $this->section('scripts'); ?>
<script>
    function openBox(e) {
         e.showPicker();
    }
    subsdata = <?= $subsdata; ?>;
</script>
<script src="/js/billing.js"></script>
<?= $this->endSection(); ?>