<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<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; }
</style>

<section class="content-header">
    <h1><i class="fa fa-user"></i> <?php echo lang('clients') ?> <small><?php echo lang('edit') ?></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/clients') ?>"><?php echo lang('clients') ?></a></li>
        <li class="active"><?php echo lang('edit') ?></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-pencil"></i> Editar Cliente</h3>
    </div>
    <form method="post" enctype="multipart/form-data">
    <div class="box-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('name') ?></label>
                    <input type="text" name="name" value="<?php echo htmlspecialchars($clients->name ?? '') ?>" class="form-control" required>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('profile') ?> <?php echo lang('picture') ?></label>
                    <div style="display:flex;align-items:center;gap:12px">
                        <input type="file" name="img" class="form-control" style="flex:1">
                        <?php if(!empty($clients->image)): ?>
                        <img src="<?php echo base_url('assets/uploads/images/'.$clients->image) ?>" style="width:44px;height:44px;border-radius:50%;object-fit:cover;border:2px solid #e2e6ea">
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('gender') ?></label>
                    <div style="padding-top:6px">
                        <label class="gender-opt"><input type="radio" name="gender" value="Male" <?php echo ($clients->gender ?? '') == 'Male' ? 'checked' : '' ?>> <?php echo lang('male') ?></label>
                        <label class="gender-opt"><input type="radio" name="gender" value="Female" <?php echo ($clients->gender ?? '') == 'Female' ? 'checked' : '' ?>> <?php echo lang('female') ?></label>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('date_of_birth') ?></label>
                    <input type="text" name="dob" value="<?php echo $clients->dob ?? '' ?>" class="form-control datepicker" placeholder="AAAA-MM-DD">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('phone') ?></label>
                    <input type="text" name="contact" value="<?php echo htmlspecialchars($clients->contact ?? '') ?>" class="form-control">
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('email') ?></label>
                    <input type="text" name="email" value="<?php echo htmlspecialchars($clients->email ?? '') ?>" class="form-control">
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label><?php echo lang('address') ?></label>
                    <textarea name="address" class="form-control" rows="2"><?php echo htmlspecialchars($clients->address ?? '') ?></textarea>
                </div>
            </div>
        </div>

        <hr>
        <h4 style="font-size:13px;font-weight:700;color:#1e3a5f;margin-bottom:14px"><i class="fa fa-key"></i> Acesso ao Portal do Cliente</h4>

        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('username') ?></label>
                    <input type="text" name="username" value="<?php echo htmlspecialchars($clients->username ?? '') ?>" class="form-control">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('password') ?> <small class="text-muted">(deixe em branco para manter)</small></label>
                    <input type="password" name="password" value="" class="form-control" autocomplete="new-password">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label><?php echo lang('confirm') ?> <?php echo lang('password') ?></label>
                    <input type="password" name="confirm" value="" class="form-control" autocomplete="new-password">
                </div>
            </div>
        </div>

        <?php
        $CI = get_instance();
        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):
            $result = $CI->db->query("SELECT * FROM rel_form_custom_fields WHERE custom_field_id='".$doc->id."' AND table_id='".$clients->id."' AND form='".$doc->form."'")->row();
            $reply  = $result->reply ?? '';
            $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 ?>]" value="<?php echo htmlspecialchars($reply) ?>">
                    <?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)==$reply?'selected':'' ?>><?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)==$reply?'checked':'' ?>> <?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)==$reply?'checked':'' ?>> <?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"><?php echo htmlspecialchars($reply) ?></textarea>
                    <?php elseif($doc->field_type==6): ?>
                    <input type="url" class="form-control" name="reply[<?php echo $doc->id ?>]" value="<?php echo htmlspecialchars($reply) ?>">
                    <?php elseif($doc->field_type==7): ?>
                    <input type="email" class="form-control" name="reply[<?php echo $doc->id ?>]" value="<?php echo htmlspecialchars($reply) ?>">
                    <?php elseif($doc->field_type==8): ?>
                    <input type="number" class="form-control" name="reply[<?php echo $doc->id ?>]" value="<?php echo htmlspecialchars($reply) ?>">
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
    <div class="box-footer">
        <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> <?php echo lang('update') ?></button>
        <a href="<?php echo site_url('admin/clients/view_client/'.$clients->id) ?>" class="btn btn-default"><i class="fa fa-arrow-left"></i> Cancelar</a>
    </div>
    <?php form_close() ?>
</div>

</section>

<script src="<?php echo base_url('assets/js/jquery.datetimepicker.js') ?>"></script>
<script>
$(function() {
    $('.datepicker').datetimepicker({ timepicker:false, format:'Y-m-d' });
});
</script>
