<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<link href="<?php echo base_url('assets/css/chosen.css') ?>" rel="stylesheet">
<link href="<?php echo base_url('assets/css/jquery.datetimepicker.css') ?>" rel="stylesheet">
<style>
.form-group label { font-size:11px; font-weight:700; color:#888; text-transform:uppercase; letter-spacing:.5px; margin-bottom:5px; display:block; }
.gender-opt { display:inline-flex; align-items:center; gap:5px; margin-right:18px; font-weight:400; font-size:13px; }
.modal-header-dark { background:linear-gradient(135deg,#1e3a5f,#2e75b6); border-radius:6px 6px 0 0; }
.modal-header-dark .modal-title { color:#fff; font-weight:700; }
.modal-header-dark .close { color:#fff; opacity:.8; }
</style>

<section class="content-header">
    <h1><i class="fa fa-calendar-plus-o"></i> <?php echo lang('appointments') ?> <small><?php echo lang('add') ?></small></h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin') ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li><a href="<?php echo site_url('admin/appointments') ?>"><?php echo lang('appointments') ?></a></li>
        <li class="active"><?php echo lang('add') ?></li>
    </ol>
</section>

<section class="content">

<?php if(validation_errors()): ?>
<div class="alert alert-danger alert-dismissible">
    <button class="close" data-dismiss="alert">&times;</button>
    <i class="fa fa-ban"></i> <?php echo validation_errors() ?>
</div>
<?php endif; ?>

<div class="box box-primary">
    <div class="box-header with-border" style="background:linear-gradient(135deg,#1e3a5f,#2e75b6);border-radius:10px 10px 0 0">
        <h3 class="box-title" style="color:#fff"><i class="fa fa-calendar-plus-o"></i> Novo Agendamento</h3>
    </div>
    <?php echo form_open_multipart('admin/appointments/add/') ?>
    <div class="box-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('title') ?></label>
                    <input type="text" name="title" value="<?php echo set_value('title') ?>" class="form-control" required>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('contact') ?></label>
                    <div style="display:flex;gap:8px">
                        <select name="contact_id" class="form-control chzn" style="flex:1">
                            <option value="">-- <?php echo lang('select_contact') ?> --</option>
                            <?php foreach($contacts as $new):
                                $sel = set_select('contact_id', $new->id) ? 'selected' : '';
                            ?>
                            <option value="<?php echo $new->id ?>" <?php echo $sel ?>><?php echo htmlspecialchars($new->name) ?></option>
                            <?php endforeach; ?>
                        </select>
                        <a href="#myModal" data-toggle="modal" class="btn btn-default" title="<?php echo lang('add_new') ?> <?php echo lang('contact') ?>"><i class="fa fa-plus"></i></a>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('date') ?></label>
                    <input type="text" name="date_time" value="<?php echo set_value('date_time') ?>" class="form-control datetimepicker" placeholder="AAAA-MM-DD HH:mm" required>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('motive') ?></label>
                    <textarea name="motive" class="form-control" rows="1"><?php echo set_value('motive') ?></textarea>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label><?php echo lang('notes') ?></label>
                    <textarea name="notes" class="form-control" rows="3"></textarea>
                </div>
            </div>
        </div>

        <?php if(!empty($fields)): ?>
        <hr>
        <h4 style="font-size:13px;font-weight:700;color:#1e3a5f;margin-bottom:14px"><i class="fa fa-list-alt"></i> Informações Adicionais</h4>
        <div class="row">
        <?php foreach($fields as $doc):
            $values = explode(',', $doc->values ?? '');
        ?>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo htmlspecialchars($doc->name) ?></label>
                    <?php if($doc->field_type==1): ?>
                    <input type="text" class="form-control" name="reply[<?php echo $doc->id ?>]">
                    <?php elseif($doc->field_type==2): ?>
                    <select name="reply[<?php echo $doc->id ?>]" class="form-control">
                        <?php foreach($values as $val): ?>
                        <option value="<?php echo trim($val) ?>"><?php echo trim($val) ?></option>
                        <?php endforeach; ?>
                    </select>
                    <?php elseif($doc->field_type==3): ?>
                    <div style="padding-top:6px">
                        <?php foreach($values as $val): ?>
                        <label class="gender-opt"><input type="radio" name="reply[<?php echo $doc->id ?>]" value="<?php echo trim($val) ?>"> <?php echo trim($val) ?></label>
                        <?php endforeach; ?>
                    </div>
                    <?php elseif($doc->field_type==4): ?>
                    <div style="padding-top:6px">
                        <?php foreach($values as $val): ?>
                        <label class="gender-opt"><input type="checkbox" name="reply[<?php echo $doc->id ?>][]" value="<?php echo trim($val) ?>"> <?php echo trim($val) ?></label>
                        <?php endforeach; ?>
                    </div>
                    <?php elseif($doc->field_type==5): ?>
                    <textarea class="form-control" name="reply[<?php echo $doc->id ?>]" rows="2"></textarea>
                    <?php elseif($doc->field_type==6): ?>
                    <input type="url" class="form-control" name="reply[<?php echo $doc->id ?>]">
                    <?php elseif($doc->field_type==7): ?>
                    <input type="email" class="form-control" name="reply[<?php echo $doc->id ?>]">
                    <?php elseif($doc->field_type==8): ?>
                    <input type="number" class="form-control" name="reply[<?php echo $doc->id ?>]">
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
    <div class="box-footer">
        <button type="submit" name="ok" value="ok" class="btn btn-primary"><i class="fa fa-save"></i> <?php echo lang('save') ?></button>
        <a href="<?php echo site_url('admin/appointments') ?>" class="btn btn-default"><i class="fa fa-arrow-left"></i> Cancelar</a>
    </div>
    <?php echo form_close() ?>
</div>

</section>

<!-- Modal Novo Contato -->
<div class="modal fade" id="myModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content" style="border-radius:10px;overflow:hidden">
            <div class="modal-header modal-header-dark">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><i class="fa fa-user-plus"></i> <?php echo lang('add_new') ?> <?php echo lang('contact') ?></h4>
            </div>
            <?php echo form_open_multipart('admin/contacts/add/') ?>
            <div class="modal-body">
                <div class="form-group">
                    <label><?php echo lang('name') ?></label>
                    <input type="text" name="name" value="" class="form-control">
                </div>
                <div class="form-group">
                    <label><?php echo lang('phone') ?></label>
                    <input type="text" name="phone" class="form-control">
                </div>
                <div class="form-group">
                    <label><?php echo lang('email') ?></label>
                    <input type="text" name="email" class="form-control">
                </div>
                <div class="form-group">
                    <label><?php echo lang('address') ?></label>
                    <textarea name="address" class="form-control" rows="2"></textarea>
                </div>

                <?php if(!empty($contact_fields)): foreach($contact_fields as $doc):
                    $values = explode(',', $doc->values ?? '');
                ?>
                <div class="form-group">
                    <label><?php echo htmlspecialchars($doc->name) ?></label>
                    <?php if($doc->field_type==1): ?>
                    <input type="text" class="form-control" name="reply[<?php echo $doc->id ?>]">
                    <?php elseif($doc->field_type==2): ?>
                    <select name="reply[<?php echo $doc->id ?>]" class="form-control">
                        <?php foreach($values as $val): ?>
                        <option value="<?php echo trim($val) ?>"><?php echo trim($val) ?></option>
                        <?php endforeach; ?>
                    </select>
                    <?php elseif($doc->field_type==3): ?>
                    <div style="padding-top:6px"><?php foreach($values as $val): ?><label class="gender-opt"><input type="radio" name="reply[<?php echo $doc->id ?>]" value="<?php echo trim($val) ?>"> <?php echo trim($val) ?></label><?php endforeach; ?></div>
                    <?php elseif($doc->field_type==4): ?>
                    <div style="padding-top:6px"><?php foreach($values as $val): ?><label class="gender-opt"><input type="checkbox" name="reply[<?php echo $doc->id ?>][]" value="<?php echo trim($val) ?>"> <?php echo trim($val) ?></label><?php endforeach; ?></div>
                    <?php elseif($doc->field_type==5): ?>
                    <textarea class="form-control" name="reply[<?php echo $doc->id ?>]" rows="2"></textarea>
                    <?php endif; ?>
                </div>
                <?php endforeach; endif; ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo lang('close') ?></button>
                <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> <?php echo lang('save') ?></button>
            </div>
            <?php echo form_close() ?>
        </div>
    </div>
</div>

<script src="<?php echo base_url('assets/js/chosen.jquery.min.js') ?>"></script>
<script src="<?php echo base_url('assets/js/jquery.datetimepicker.js') ?>"></script>
<script>
$(function() {
    $('.chzn').chosen({ width:'100%' });
    $('.datetimepicker').datetimepicker({ format:'Y-m-d H:i' });
});
</script>
