<?php
require_once('../config.php');

$sql_details = $search_db;

$custom_sorting = '';

$table = <<<EOT
(
    SELECT * FROM `contacts` 
) join_table
EOT;

$whereResult = null;

if (isset($_GET['all_leads_where_clause']) && !empty($_GET['all_leads_where_clause'])) {
    $whereAll = $_GET['all_leads_where_clause'];
} else {
    $whereAll = null;
}

$primaryKey = 'id';

$s_no = 1;
$columns = array(
    array(
        'db' => 'id',
        'dt' => 'id',
        'formatter' => function ($cell, $row) {
            $checkboxIcon = "<span><input type='checkbox' data-lead-id='" . $cell . "'  name='leadsdata' id='' class='leadsdata' value='" . $cell . "'></span> ";
            // $id = "<span>" . $cell . "</span>";
            
            return $checkboxIcon;
        }
    ),
    array(
        'db' => 'name',
        'dt' => 'name'
    ),
    array(
        'db' => 'phone',
        'dt' => 'phone',
        'formatter' => function($cell, $row){
            $countryCode = $row['country_code'];
            $number = $cell;

            return "(+$countryCode) $number";
        }
    ),
    array(
        'db' => 'country_code',
        'dt' => 'country_code',
        'formatter' => function($cell, $row){
            $getAttri = mysqli_query($GLOBALS['db'], "SELECT * FROM contact_attributes WHERE contact_id='$row[id]'");
            $attriCount = mysqli_num_rows($getAttri);
            $attriShows = "<div class='d-flex gap-2'>";
            while($attriDetails = mysqli_fetch_assoc($getAttri)){
                $attriId = $attriDetails['id'];
                $attriDiv = "<div style='background: #efefef;border-radius: 5px;padding: 5px 10px;' class='d-flex align-items-center gap-2'>$attriDetails[attri_name]: $attriDetails[attri_value] <div data-attri-id='$attriId' data-contact-attri-name='$attriDetails[attri_name]' class='deleteContactAttri'><svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' style='fill: rgba(0, 0, 0, 1);transform: ;msFilter:;' class='remove-attribute'><path d='M12 2C6.486 2 2 6.486 2 12s4.486 10 10 10 10-4.486 10-10S17.514 2 12 2zm4.207 12.793-1.414 1.414L12 13.414l-2.793 2.793-1.414-1.414L10.586 12 7.793 9.207l1.414-1.414L12 10.586l2.793-2.793 1.414 1.414L13.414 12l2.793 2.793z'></path></svg></div></div>";

                $attriShows .= $attriDiv;
            }
            $attriShows .= "</div>";

            return  $attriShows;
        }
    ),
    array(
        'db' => 'source',
        'dt' => 'source',
        'formatter' => function($cell, $row){
            $editBtn = "<button class='btn btn-outline-success editContact' data-contact-id='$row[id]'><svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' style='fill: #009688;' class='actionBtn'><path d='m7 17.013 4.413-.015 9.632-9.54c.378-.378.586-.88.586-1.414s-.208-1.036-.586-1.414l-1.586-1.586c-.756-.756-2.075-.752-2.825-.003L7 12.583v4.43zM18.045 4.458l1.589 1.583-1.597 1.582-1.586-1.585 1.594-1.58zM9 13.417l6.03-5.973 1.586 1.586-6.029 5.971L9 15.006v-1.589z'></path><path d='M5 21h14c1.103 0 2-.897 2-2v-8.668l-2 2V19H8.158c-.026 0-.053.01-.079.01-.033 0-.066-.009-.1-.01H5V5h6.847l2-2H5c-1.103 0-2 .897-2 2v14c0 1.103.897 2 2 2z'></path></svg></button>";

            $dltBtn = "<button class='btn btn-outline-danger deletContact' data-contact-id='$row[id]'><svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' style='fill: #F44336;' class='actionBtn'><path d='M5 20a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8h2V6h-4V4a2 2 0 0 0-2-2H9a2 2 0 0 0-2 2v2H3v2h2zM9 4h6v2H9zM8 8h9v12H7V8z'></path><path d='M9 10h2v8H9zm4 0h2v8h-2z'></path></svg></button>";

            return "<div class='d-flex justify-content-center align-items-center gap-2'>$editBtn $dltBtn</div>";
        }
    ),
    array(
        'db' => 'created_at',
        'dt' => 'created_at'
    ),
);


require('ssp.class.php');

echo json_encode(
    SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, $where, $whereAll)
);
?>