<?= message_box('success'); ?>
<?= message_box('error');
$eeror_message = $this->session->userdata('error');
if (!empty($eeror_message)) : foreach ($eeror_message as $key => $message) :
    ?>
    <div class="alert alert-danger">
        <?php echo $message; ?>
    </div>
<?php
endforeach;
endif;
$this->session->unset_userdata('error');

?>
<?php
$client_outstanding = $this->invoice_model->client_outstanding($client_details->client_id);
$client_payments = $this->invoice_model->get_sum('tbl_payments', 'amount', $array = array('paid_by' => $client_details->client_id));
$client_payable = $client_payments + $client_outstanding;
$client_currency = $this->invoice_model->client_currency_symbol($client_details->client_id);
if (!empty($client_currency)) {
    $cur = $client_currency->symbol;
} else {
    $currency = get_row('tbl_currencies', array('code' => config_item('default_currency')));
    $cur = $currency->symbol;
}
if ($client_payable > 0 and $client_payments > 0) {
    $perc_paid = round(($client_payments / $client_payable) * 100, 1);
    if ($perc_paid > 100) {
        $perc_paid = '100';
    }
} else {
    $perc_paid = 0;
}
$edited = can_action('4', 'edited');
$contacts = ($client_details->primary_contact != 0) ? $client_details->primary_contact : null;
$primary_contact = $this->client_model->check_by(array('user_id' => $contacts), 'tbl_account_details');
?>
<div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-800">
    <div class="flex flex-wrap items-center justify-between gap-3 border-b border-slate-200 px-5 py-4 dark:border-slate-700">
        <h3 class="text-base font-semibold text-slate-900 dark:text-slate-100">
            <?= html_escape($client_details->name) ?> - <?= lang('details') ?>
        </h3>
        <div class="flex items-center gap-3 text-sm">
            <?php if ($client_details->leads_id != 0) { ?>
                <span class="text-slate-500 dark:text-slate-300">
                    <?= lang('converted_from') ?>
                    <a href="<?= base_url() ?>admin/leads/leads_details/<?= $client_details->leads_id ?>" class="font-semibold text-indigo-600 hover:underline dark:text-indigo-300"><?= lang('leads') ?></a>
                </span>
            <?php } ?>
            <?php if (!empty($edited)) { ?>
                <a href="<?= base_url() ?>admin/client/create_client/<?= $client_details->client_id ?>"
                   class="inline-flex items-center rounded-lg border border-slate-200 px-3 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50 dark:border-slate-600 dark:text-slate-100 dark:hover:bg-slate-700">
                    <?= lang('edit') ?>
                </a>
            <?php } ?>
        </div>
    </div>

    <div class="p-5">
        <div class="grid grid-cols-1 gap-4 xl:grid-cols-2">
            <div class="rounded-xl border border-slate-200 bg-slate-50/70 p-4 dark:border-slate-700 dark:bg-slate-900/40">
                <p class="mb-3 text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400"><?= lang('contact_details') ?></p>
                <div class="space-y-2 text-sm">
                    <div class="grid grid-cols-12 gap-2">
                        <div class="col-span-5 text-slate-500 dark:text-slate-400"><?= lang('name') ?></div>
                        <div class="col-span-7 font-medium text-slate-800 dark:text-slate-100"><?= html_escape($client_details->name) ?></div>
                    </div>
                    <div class="grid grid-cols-12 gap-2">
                        <div class="col-span-5 text-slate-500 dark:text-slate-400"><?= lang('contact_person') ?></div>
                        <div class="col-span-7 font-medium text-slate-800 dark:text-slate-100"><?= !empty($primary_contact->fullname) ? html_escape($primary_contact->fullname) : '-' ?></div>
                    </div>
                    <div class="grid grid-cols-12 gap-2">
                        <div class="col-span-5 text-slate-500 dark:text-slate-400"><?= lang('email') ?></div>
                        <div class="col-span-7 font-medium text-slate-800 dark:text-slate-100"><?= !empty($client_details->email) ? html_escape($client_details->email) : '-' ?></div>
                    </div>
                    <div class="grid grid-cols-12 gap-2">
                        <div class="col-span-5 text-slate-500 dark:text-slate-400"><?= lang('city') ?></div>
                        <div class="col-span-7 text-slate-700 dark:text-slate-200"><?= !empty($client_details->city) ? html_escape($client_details->city) : '-' ?></div>
                    </div>
                    <div class="grid grid-cols-12 gap-2">
                        <div class="col-span-5 text-slate-500 dark:text-slate-400"><?= lang('zipcode') ?></div>
                        <div class="col-span-7 text-slate-700 dark:text-slate-200"><?= !empty($client_details->zipcode) ? html_escape($client_details->zipcode) : '-' ?></div>
                    </div>
                </div>
            </div>

            <div class="rounded-xl border border-slate-200 bg-slate-50/70 p-4 dark:border-slate-700 dark:bg-slate-900/40">
                <p class="mb-3 text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400"><?= lang('address') ?> &amp; <?= lang('phone') ?></p>
                <div class="space-y-2 text-sm">
                    <div class="grid grid-cols-12 gap-2">
                        <div class="col-span-5 text-slate-500 dark:text-slate-400"><?= lang('address') ?></div>
                        <div class="col-span-7 text-slate-700 dark:text-slate-200"><?= !empty($client_details->address) ? html_escape($client_details->address) : '-' ?></div>
                    </div>
                    <div class="grid grid-cols-12 gap-2">
                        <div class="col-span-5 text-slate-500 dark:text-slate-400"><?= lang('phone') ?></div>
                        <div class="col-span-7">
                            <?php if (!empty($client_details->phone)) { ?>
                                <a href="tel:<?= html_escape($client_details->phone) ?>" class="font-medium text-indigo-600 hover:underline dark:text-indigo-300"><?= html_escape($client_details->phone) ?></a>
                            <?php } else { ?>
                                <span class="text-slate-700 dark:text-slate-200">-</span>
                            <?php } ?>
                        </div>
                    </div>
                    <?php if (!empty($client_details->mobile)) { ?>
                        <div class="grid grid-cols-12 gap-2">
                            <div class="col-span-5 text-slate-500 dark:text-slate-400"><?= lang('mobile') ?></div>
                            <div class="col-span-7"><a href="tel:<?= html_escape($client_details->mobile) ?>" class="font-medium text-indigo-600 hover:underline dark:text-indigo-300"><?= html_escape($client_details->mobile) ?></a></div>
                        </div>
                    <?php } ?>
                    <div class="grid grid-cols-12 gap-2">
                        <div class="col-span-5 text-slate-500 dark:text-slate-400"><?= lang('fax') ?></div>
                        <div class="col-span-7 text-slate-700 dark:text-slate-200"><?= !empty($client_details->fax) ? html_escape($client_details->fax) : '-' ?></div>
                    </div>
                </div>
            </div>
        </div>

        <?php $show_custom_fields = custom_form_label(12, $client_details->client_id); ?>
        <?php if (!empty($show_custom_fields)) { ?>
            <div class="mt-4 rounded-xl border border-slate-200 bg-white p-4 dark:border-slate-700 dark:bg-slate-900/30">
                <p class="mb-3 text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400"><?= lang('custom_fields') ?></p>
                <div class="grid grid-cols-1 gap-2 md:grid-cols-2">
                    <?php foreach ($show_custom_fields as $c_label => $v_fields) {
                        if (empty($v_fields)) {
                            continue;
                        } ?>
                        <div class="rounded-lg border border-slate-200 bg-slate-50 px-3 py-2 text-sm dark:border-slate-700 dark:bg-slate-800/60">
                            <span class="text-slate-500 dark:text-slate-400"><?= html_escape($c_label) ?>:</span>
                            <span class="ml-1 text-slate-800 dark:text-slate-100"><?= $v_fields ?></span>
                        </div>
                    <?php } ?>
                </div>
            </div>
        <?php } ?>

        <div class="mt-4 grid grid-cols-1 gap-4 xl:grid-cols-2">
            <div class="rounded-xl border border-slate-200 bg-white p-4 dark:border-slate-700 dark:bg-slate-900/30">
                <p class="mb-3 text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400"><?= lang('social_links') ?></p>
                <div class="space-y-2 text-sm">
                    <?php if (!empty($client_details->website)) { ?>
                        <div><span class="text-slate-500 dark:text-slate-400"><?= lang('website') ?>:</span> <span class="text-slate-800 dark:text-slate-100"><?= html_escape($client_details->website) ?></span></div>
                    <?php } ?>
                    <?php if (!empty($client_details->skype_id)) { ?>
                        <div><span class="text-slate-500 dark:text-slate-400"><?= lang('skype_id') ?>:</span> <span class="text-slate-800 dark:text-slate-100"><?= html_escape($client_details->skype_id) ?></span></div>
                    <?php } ?>
                    <?php if (!empty($client_details->facebook)) { ?>
                        <div><span class="text-slate-500 dark:text-slate-400"><?= lang('facebook_profile_link') ?>:</span> <span class="text-slate-800 dark:text-slate-100"><?= html_escape($client_details->facebook) ?></span></div>
                    <?php } ?>
                    <?php if (!empty($client_details->twitter)) { ?>
                        <div><span class="text-slate-500 dark:text-slate-400"><?= lang('twitter_profile_link') ?>:</span> <span class="text-slate-800 dark:text-slate-100"><?= html_escape($client_details->twitter) ?></span></div>
                    <?php } ?>
                    <?php if (!empty($client_details->linkedin)) { ?>
                        <div><span class="text-slate-500 dark:text-slate-400"><?= lang('linkedin_profile_link') ?>:</span> <span class="text-slate-800 dark:text-slate-100"><?= html_escape($client_details->linkedin) ?></span></div>
                    <?php } ?>
                    <?php if (empty($client_details->website) && empty($client_details->skype_id) && empty($client_details->facebook) && empty($client_details->twitter) && empty($client_details->linkedin)) { ?>
                        <p class="text-slate-500 dark:text-slate-400">-</p>
                    <?php } ?>
                </div>
            </div>

            <div class="rounded-xl border border-slate-200 bg-white p-4 dark:border-slate-700 dark:bg-slate-900/30">
                <p class="mb-3 text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400"><?= lang('hosting_details') ?></p>
                <div class="space-y-2 text-sm">
                    <?php if (!empty($client_details->hosting_company)) { ?>
                        <div><span class="text-slate-500 dark:text-slate-400"><?= lang('hosting_company') ?>:</span> <span class="text-slate-800 dark:text-slate-100"><?= html_escape($client_details->hosting_company) ?></span></div>
                    <?php } ?>
                    <?php if (!empty($client_details->hostname)) { ?>
                        <div><span class="text-slate-500 dark:text-slate-400"><?= lang('hostname') ?>:</span> <span class="text-slate-800 dark:text-slate-100"><?= html_escape($client_details->hostname) ?></span></div>
                    <?php } ?>
                    <?php if (!empty($client_details->username)) { ?>
                        <div><span class="text-slate-500 dark:text-slate-400"><?= lang('username') ?>:</span> <span class="text-slate-800 dark:text-slate-100"><?= html_escape($client_details->username) ?></span></div>
                    <?php } ?>
                    <?php if (!empty($client_details->password)) {
                        $hosting_password = strlen((string) decrypt($client_details->password)); ?>
                        <div>
                            <span class="text-slate-500 dark:text-slate-400"><?= lang('password') ?>:</span>
                            <span id="show_password" class="text-slate-800 dark:text-slate-100">
                                <?php
                                if (!empty($hosting_password)) {
                                    for ($p = 1; $p <= $hosting_password; $p++) {
                                        echo '*';
                                    }
                                }
                                ?>
                            </span>
                            <?= btn_modal(
                                'admin/client/see_password/c_' . $client_details->client_id,
                                lang('see_password'),
                                lang('see_password'),
                                'md',
                                '',
                                array('id' => 'see_password', 'class' => 'ml-2 text-xs font-semibold text-indigo-600 hover:underline dark:text-indigo-300')
                            ) ?>
                            <strong id="hosting_password" class="required"></strong>
                        </div>
                    <?php } ?>
                    <?php if (!empty($client_details->port)) { ?>
                        <div><span class="text-slate-500 dark:text-slate-400"><?= lang('port') ?>:</span> <span class="text-slate-800 dark:text-slate-100"><?= html_escape($client_details->port) ?></span></div>
                    <?php } ?>
                    <?php if (empty($client_details->hosting_company) && empty($client_details->hostname) && empty($client_details->username) && empty($client_details->password) && empty($client_details->port)) { ?>
                        <p class="text-slate-500 dark:text-slate-400">-</p>
                    <?php } ?>
                </div>
            </div>
        </div>

        <div class="mt-4 rounded-xl border border-slate-200 bg-gradient-to-r from-slate-50 to-white p-4 text-center dark:border-slate-700 dark:from-slate-900/60 dark:to-slate-800">
            <h4 class="text-sm font-semibold text-slate-600 dark:text-slate-300"><?= lang('received_amount') ?></h4>
            <h3 class="mt-2 text-2xl font-bold text-rose-600 dark:text-rose-300">
                <?= display_money($this->client_model->client_paid($client_details->client_id), client_currency($client_details->client_id)); ?>
            </h3>
            <div class="mt-3 inline-flex items-center rounded-full border border-slate-200 bg-white px-3 py-1 text-sm font-semibold text-slate-700 dark:border-slate-600 dark:bg-slate-800 dark:text-slate-100">
                <?= $perc_paid ?>% <?= lang('paid') ?>
            </div>
        </div>
    </div>

    <div class="flex flex-wrap items-center justify-between gap-3 border-t border-slate-200 bg-slate-50 px-5 py-3 text-sm dark:border-slate-700 dark:bg-slate-900/40">
        <span class="text-slate-700 dark:text-slate-200">
            <?= lang('invoice_amount') ?>:
            <strong class="rounded-full bg-indigo-100 px-2 py-0.5 text-indigo-700 dark:bg-indigo-900/50 dark:text-indigo-200">
                <?= display_money($client_payable, client_currency($client_details->client_id)); ?>
            </strong>
        </span>
        <span class="text-slate-700 dark:text-slate-200">
            <?= lang('outstanding') ?>:
            <strong class="rounded-full bg-rose-100 px-2 py-0.5 text-rose-700 dark:bg-rose-900/50 dark:text-rose-200">
                <?= display_money($client_outstanding, client_currency($client_details->client_id)) ?>
            </strong>
        </span>
    </div>
</div>
