<?php
// ── DB for lookup tables ───────────────────────────────────────────────────
$conn = new MySQLi("localhost", "karan_live", "Sailor@123!", "karan_live");

$sql           = "SELECT * FROM caste";
$caste_result  = mysqli_query($conn, $sql);

$sql        = "SELECT * FROM sub_caste";
$sub_result = mysqli_query($conn, $sql);

$sql           = "SELECT * FROM status";
$status_result = mysqli_query($conn, $sql);

$sql           = "SELECT * FROM source";
$source_result = mysqli_query($conn, $sql);

$sql        = "SELECT * FROM res_status";
$res_result = mysqli_query($conn, $sql);

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$msg = '';

include("include/settings.php");
// include("checksession.php");   // uncomment in production

if (isset($_POST['btnSaveMember'])) {
    if (trim($_POST["txtDate"]) != null) {

        $leadtype = 'rishta_lead';
        $thedate  = $thisdate;  // comes from settings.php
        $lead_date = date('Y-m-d', strtotime($_POST['txtDate']));

        // ── Helper: safe nullable string ──────────────────────────────────
        function safeStr($key) {
            return (isset($_POST[$key]) && trim($_POST[$key]) != '') ? $_POST[$key] : null;
        }

        $paydate           = safeStr('txtdop');
        $txtpname          = safeStr('txtpname');
        $txtrelation       = safeStr('txtrelation');
        $txtpcontact       = safeStr('txtpcontact');
        $txtmstatus        = safeStr('txtmstatus');
        $txtemail          = safeStr('txtemail');
        $txtcurrency       = safeStr('txtcurrency');
        $txtamanual        = safeStr('txtamanual');
        $txtgender         = safeStr('txtgender');        // FIX: was bound to txtamanual
        $txtmarital        = safeStr('txtmarital');       // FIX: was bound to txtamanual
        $txtage            = safeStr('txtage');           // FIX: was checking txtrelation
        $txtheight         = safeStr('txtheight');
        $txtheightinch     = safeStr('txtheightinch');
        $txtedu            = safeStr('txtedu');
        $txtemp            = safeStr('txtemp');
        $txtreligion       = safeStr('txtreligion');      // FIX: was checking txtrelation
        $txtminfo          = safeStr('txtminfo');
        $txtcaste          = safeStr('txtcaste');
        $txtsubcaste       = safeStr('txtsubcaste');
        $txtrstatus        = safeStr('txtrstatus');
        $txtRemarks        = safeStr('txtRemarks');
        $txtname1          = safeStr('txtname1');
        $txturl1           = safeStr('txturl1');
        $txtname2          = safeStr('txtname2');
        $txturl2           = safeStr('txturl2');
        $txtname3          = safeStr('txtname3');
        $txturl3           = safeStr('txturl3');
        $txtname4          = safeStr('txtname4');
        $txturl4           = safeStr('txturl4');
        $txtname5          = safeStr('txtname5');
        $txturl5           = safeStr('txturl5');
        $txtvideocall      = safeStr('txtvideocall');
        $txtvideolink      = safeStr('txtvideolink');
        $txtcname2         = safeStr('txtcname2');
        $txtcontact2       = safeStr('txtcontact2');
        $txtemailid2       = safeStr('txtemailid2');
        $txtsmartmatch     = safeStr('txtsmartmatch');

        // ── Call time ─────────────────────────────────────────────────────
        if (isset($_POST["call_time_scheduling"]) && trim($_POST["call_time_scheduling"]) != '') {
            $call_time_scheduling = date('H:i:s', strtotime(trim($_POST['call_time_scheduling'])));
        } else {
            $call_time_scheduling = null;
        }

        // ── Allocation limit check ────────────────────────────────────────
        $pass = 1;
        if (isset($_POST['ddlLeadAllocated']) && !empty($_POST['ddlLeadAllocated'])) {
            $checkExists = $mysqli->prepare(
                "SELECT lead_allocated_to FROM rishta_leadmaster WHERE lead_allocated_to=?"
            );
            $alloc = (int)$_POST['ddlLeadAllocated'];
            $checkExists->bind_param('i', $alloc);
            $checkExists->execute();
            $checkExists = $checkExists->get_result();
            if (mysqli_num_rows($checkExists) >= 100) {
                $pass = 0;
                $msg  = "Please Select Other Sales Executive to allocate lead.";
            }
        }

        if ($pass == 1) {
            /*
             * Column order in INSERT:
             * lead_created, lead_payment, source, lead_type, profile_name,
             * contact_name, relation, profile_contact_no, contact_no, member_status,
             * profile_email, email_id, city, state, country, disposition,
             * currency, amount, gender, marital_status, age, height, height_inches,
             * education, employment, religion, more_info, caste, sub_caste,
             * residency_status, lead_remarks, name1, url1, name2, url2, name3, url3,
             * name4, url4, name5, url5, video_calling, video_link,
             * call_scheduling, call_time_scheduling, lead_allocated_to, lead_added_by,
             * contact_name_two, contact_email_two, contact_number_two,
             * smartmatch_profile_link, lead_created_on
             *
             * Types: 51 params
             * s = string/null, i = integer
             */
            $usermaster = $mysqli->prepare(
                "INSERT INTO rishta_leadmaster
                 (lead_created, lead_payment, source, lead_type,
                  profile_name, contact_name, relation, profile_contact_no,
                  contact_no, member_status, profile_email, email_id,
                  city, state, country, disposition, currency, amount,
                  gender, marital_status, age, height, height_inches,
                  education, employment, religion, more_info, caste,
                  sub_caste, residency_status, lead_remarks,
                  name1, url1, name2, url2, name3, url3, name4, url4, name5, url5,
                  video_calling, video_link, call_scheduling, call_time_scheduling,
                  lead_allocated_to, lead_added_by,
                  contact_name_two, contact_email_two, contact_number_two,
                  smartmatch_profile_link, lead_created_on)
                 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
            );

            // 52 chars: all s except lead_allocated_to(i) and lead_added_by(i)
            $usermaster->bind_param(
                "sssssssssssssssssssssssssssssssssssssssssssssiisssss",
                $lead_date,
                $paydate,
                $_POST['txtLeadSource'],
                $leadtype,
                $txtpname,
                $_POST['txtcname'],
                $txtrelation,
                $txtpcontact,
                $_POST['txtcontact'],
                $txtmstatus,
                $txtemail,
                $_POST['txtemailid'],
                $_POST['txtcity'],
                $_POST['txtstate'],
                $_POST['txtcountry'],
                $_POST['ddlLeadStatus'],
                $txtcurrency,
                $txtamanual,        // amount
                $txtgender,         // FIX: was $txtamanual
                $txtmarital,        // FIX: was $txtamanual
                $txtage,
                $txtheight,
                $txtheightinch,
                $txtedu,
                $txtemp,
                $txtreligion,
                $txtminfo,
                $txtcaste,
                $txtsubcaste,
                $txtrstatus,
                $txtRemarks,
                $txtname1,
                $txturl1,
                $txtname2,
                $txturl2,
                $txtname3,
                $txturl3,
                $txtname4,
                $txturl4,
                $txtname5,
                $txturl5,
                $txtvideocall,
                $txtvideolink,
                $_POST['txtcall'],
                $call_time_scheduling,
                $_POST['ddlLeadAllocated'],
                $_SESSION['userId'],
                $txtcname2,
                $txtemailid2,
                $txtcontact2,
                $txtsmartmatch,
                $thedate
            );

            $usermaster->execute();
            $lead_id = $usermaster->insert_id;
            $usermaster->close();

            // ── Call schedule insert ──────────────────────────────────────
            $call_scheduling = null;
            if (isset($_POST["txtcall"]) && trim($_POST["txtcall"]) != '') {
                $call_scheduling = date('Y-m-d', strtotime(trim($_POST['txtcall'])));
            }

            $disposition = $_POST['ddlLeadStatus'];
            $valid_dispositions = [
                'Follow up','New Call','No match shared yet','Match Shared',
                'Close to Conversion','Fully Paid','Lead Rejected','Refund',
                'Fresh Lead','Renewal Lead','Ptential CRM Lead','Interested',
                'Old Leads','Expired (Not to be converted)','Inactive','Ringing',
                'Expired','Active','Wrong Number','On Hold',
                'Confirmed Rishta done','Not Interested'
            ];

            if (in_array($disposition, $valid_dispositions)) {
                if (!empty($call_scheduling) && !empty($call_time_scheduling)) {
                    if (!empty($lead_id)) {
                        $ins = "INSERT INTO cbi_call_schedule SET
                                call_added_by=?, lead_id=?, disposition=?,
                                call_scheduling=?, call_time_scheduling=?, created_on=?";
                        $insertObj = $mysqli->prepare($ins);
                        $now       = date('Y-m-d H:i:s');
                        $insertObj->bind_param('iissss',
                            $_SESSION['userId'], $lead_id, $disposition,
                            $call_scheduling, $call_time_scheduling, $now);
                        $insertObj->execute();
                        $insertObj->close();
                    }
                }

                if (!empty($lead_id)) {
                    $ins2      = "INSERT INTO timeline SET
                                  call_added_by=?, lead_id=?, disposition=?, created_on=?";
                    $insertObj = $mysqli->prepare($ins2);
                    $now       = date('Y-m-d H:i:s');
                    $insertObj->bind_param('iiss',
                        $_SESSION['userId'], $lead_id, $disposition, $now);
                    $insertObj->execute();
                    $insertObj->close();
                }
            }

            $msg = "Lead has been added successfully.";

            // ── Send welcome email ────────────────────────────────────────
            require 'PHPMailer/src/Exception.php';
            require 'PHPMailer/src/PHPMailer.php';
            require 'PHPMailer/src/SMTP.php';
            require 'vendor/autoload.php';

            $mail = new PHPMailer();
            $mail->isSMTP();
            $mail->Host       = 'smtp.gmail.com';
            $mail->SMTPAuth   = true;
            $mail->Username   = 'aaadriversacademyca@gmail.com';
            $mail->Password   = 'vjqzkpbuigajmtcq';
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            $mail->Port       = 587;
            $mail->setFrom('rishtadepot@gmail.com', 'Rishta Depot');
            $mail->addAddress($_POST['txtemailid'], $_POST['txtcname']);
            $mail->isHTML(true);
            $mail->Subject = 'Welcome To Rishta Depot';
            $mail->Body    = '<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>Welcome</title></head>
<body style="margin:0;padding:0;background-color:#fff7df;font-family:Arial,sans-serif;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td align="center">
<table border="0" cellpadding="20" cellspacing="0" width="640" style="background:#fff;margin:20px auto;">
<tr><td align="center">
  <img src="https://rishtasite.com/assets/images/logo-black.png" style="width:200px;height:68px;" alt="Rishta Depot">
  <h1 style="margin-top:10px;">Welcome to Rishta Depot</h1>
  <p>Hi <strong>' . htmlspecialchars($_POST['txtcname']) . '</strong>,</p>
  <p>We are excited to have you join our community!</p>
  <a href="https://rishtasite.com/" style="background-color:#f36f21;border-radius:4px;color:#fff;padding:10px 20px;text-decoration:none;display:inline-block;margin:10px 0;">Visit our website</a>
  <hr style="border-color:#f36f21;">
  <h2>Why Choose Us?</h2>
  <p>RishtaDepot is a matrimonial service powered by world-class matchmakers that provide clients with personalized attention, hand-selected matches, and valuable coaching.</p>
  <h2>Our Services</h2>
  <h3>1. Premium Matching Algorithm</h3>
  <p>RishtaDepot combines proven matchmaking techniques with cutting-edge technology to ensure we introduce you to the best possible matches.</p>
  <h3>2. Secure Communication</h3>
  <p>All of our matchmakers come from impressive corporate backgrounds with a true passion for connecting people.</p>
  <a href="https://rishtasite.com/our-matchmakers" style="background-color:#f36f21;border-radius:4px;color:#fff;padding:10px 20px;text-decoration:none;display:inline-block;margin:10px 0;">Browse Our Matchmakers</a>
  <hr style="border-color:#f36f21;">
  <p>Address: 21 King Street West, 5th Floor, Hamilton, Ontario, L8P 4W7, Canada</p>
  <p>Phone: +91 9054811166</p>
  <p>
    <a href="https://www.facebook.com/rishtadepotcom"><img src="https://rishtasite.in/lead-crm/crm/logo/facebook.png" style="margin-right:5px;"></a>
    <a href="https://www.instagram.com/rishtadepot_official/"><img src="https://rishtasite.in/lead-crm/crm/logo/instagram.png" style="margin-right:5px;"></a>
    <a href="https://www.youtube.com/channel/UC7vBgqrX1JTECSLK1amee7Q"><img src="https://rishtasite.in/lead-crm/crm/logo/youtube.png"></a>
  </p>
  <p style="font-size:12px;">&copy; 2024 Rishta Depot. All Rights Reserved.</p>
</td></tr>
</table>
</td></tr>
</table>
</body>
</html>';

            if ($mail->send()) {
                $msg = 'Lead has been Added and Email has been sent successfully!';
            } else {
                $msg = 'Lead added but email failed: ' . $mail->ErrorInfo;
            }

            header("refresh:2;url=add-new.php");
        }

    } else {
        $msg = "Please enter all the fields correctly.";
    }
}

// ── Sales executives for dropdown ─────────────────────────────────────────
$usermaster = $mysqli->prepare(
    "SELECT * FROM cdi_usermaster WHERE user_type='sales_exec' AND user_status=1 ORDER BY user_creationdate DESC"
);
$usermaster->execute();
$userresult = $usermaster->get_result();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Add New Leads - Rishta Depot</title>
    <style>.required { color: red; }</style>
    <?php include('./include/meta.php'); ?>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="js/countrystatecity.js"></script>
</head>
<?php include('./include/header.php'); ?>

<div class="content-wrapper">
    <div class="container-full">
        <div class="content-header">
            <div class="d-flex align-items-center">
                <div class="me-auto">
                    <h4 class="page-title">Forms</h4>
                    <div class="d-inline-block align-items-center">
                        <nav>
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item"><a href="#"><i class="mdi mdi-home-outline"></i></a></li>
                                <li class="breadcrumb-item" aria-current="page">Add New Leads</li>
                                <li class="breadcrumb-item active" aria-current="page">Forms</li>
                            </ol>
                        </nav>
                    </div>
                </div>
            </div>
        </div>

        <section class="content">
            <div class="row">
                <div class="col-12">

                    <?php if (trim($msg) != ''): ?>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="alert alert-success" id="errormessage">
                                <strong><?php echo htmlspecialchars($msg); ?></strong>
                            </div>
                        </div>
                    </div>
                    <?php endif; ?>

                    <div id="div_error"></div>
                    <div class="box">
                        <div class="box-header with-border">
                            <h4 class="box-title">Add Leads</h4>
                        </div>

                        <form action="#" method="post" class="form" enctype="multipart/form-data">
                            <div class="box-body">
                                <center><strong><h3 class="box-title">Lead Details</h3></strong></center>

                                <!-- Lead Status + Lead Source -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Lead Status</label><span class="required">*</span>
                                            <select id="ddlLeadStatus" name="ddlLeadStatus" class="form-select">
                                                <option value="">Lead Status</option>
                                                <?php
                                                mysqli_data_seek($status_result, 0);
                                                while ($row = mysqli_fetch_assoc($status_result)):
                                                ?>
                                                    <option value="<?php echo htmlspecialchars($row['name']); ?>"
<?php echo ($memberrow->disposition == $row['name']) ? 'selected' : ''; ?>>

    <?php echo htmlspecialchars($row['name']); ?>

</option>
                                                <?php endwhile; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Lead Source</label><span class="required">*</span>
                                            <select id="txtLeadSource" name="txtLeadSource" class="form-select">
                                                <option value="">Lead Source</option>
                                                <?php
                                                mysqli_data_seek($source_result, 0);
                                                while ($row = mysqli_fetch_assoc($source_result)):
                                                ?>
                                                    <option value="<?php echo htmlspecialchars($row['name']); ?>"
<?php echo ($memberrow->source == $row['name']) ? 'selected' : ''; ?>>

    <?php echo htmlspecialchars($row['name']); ?>

</option>
                                                <?php endwhile; ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <!-- Member Status + Country -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Member Status</label>
                                            <select id="txtmstatus" name="txtmstatus" class="form-select">
                                                <option value="">Member Status</option>
                                                <option value="#2">#2</option>
                                                <option value="#3">#3</option>
                                                <option value="#4">#4</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Country</label><span class="required">*</span>
                                            <select name="txtcountry" class="countries form-select" id="countryId">
                                                <option value="">Select Country</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <!-- State + City -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">State</label>
                                            <select name="txtstate" class="states form-select" id="stateId">
                                                <option value="">Select State</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">City</label>
                                            <select name="txtcity" class="cities form-select" id="cityId">
                                                <option value="">Select City</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <!-- Payment Date -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Payment Date</label>
                                            <input type="date" id="txtdop" name="txtdop" class="form-control"/>
                                        </div>
                                    </div>
                                </div>

                                <hr style="height:10px;">
                                <center><strong><h3 class="box-title">1st Contact Information</h3></strong></center>

                                <!-- Contact Name + Phone -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Contact 1 Name</label><span class="required">*</span>
                                            <input type="text" maxlength="50" id="txtcname" name="txtcname" class="form-control" placeholder="Name"/>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Contact 1 Phone Number</label><span class="required">*</span>
                                            <input type="text" maxlength="15" id="txtcontact" name="txtcontact" class="form-control" placeholder="Phone Number"/>
                                        </div>
                                    </div>
                                </div>

                                <!-- Email + Relation -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Contact 1 Email-ID</label><span class="required">*</span>
                                            <input type="text" id="txtemailid" autocomplete="off" name="txtemailid" class="form-control" placeholder="Contact Email-ID"/>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Relation</label>
                                            <select class="form-select" name="txtrelation" id="txtrelation">
                                                <option value="">Relation</option>
                                                <?php
                                                $relations = ['Match','Father','Mother','Brother','Sister','Brother-in-law',
                                                    'Bhabi Ji','Chacha Ji','Chachi Ji','Mama Ji','Mami Ji','Bhua Ji',
                                                    'Fufar Ji','Masi Ji','Masar Ji','Dada Ji','Dadi Ji','Dewar',
                                                    'Dewrani','Nana Ji','Nani Ji','Taya Ji','Tayi Ji','Friend','Self'];
                                                foreach ($relations as $rel):
                                                ?>
                                                    <option value="<?php echo $rel; ?>"><?php echo $rel; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <!-- Gender + Marital Status (ADDED - were commented out but needed for filter) -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Gender</label>
                                            <select id="txtgender" name="txtgender" class="form-select">
                                                <option value="">Gender</option>
                                                <option value="Male">Male</option>
                                                <option value="Female">Female</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Marital Status</label>
                                            <select id="txtmarital" name="txtmarital" class="form-select">
                                                <option value="">Marital Status</option>
                                                <option value="Never Married">Never Married</option>
                                                <option value="Annulled">Annulled</option>
                                                <option value="Not Living Together">Not Living Together</option>
                                                <option value="Divorce Awaited">Divorce Awaited</option>
                                                <option value="Divorced No Kids">Divorced No Kids</option>
                                                <option value="Divorced 1 Child">Divorced 1 Child</option>
                                                <option value="Divorced 2 Children">Divorced 2 Children</option>
                                                <option value="Divorced 3 Children">Divorced 3 Children</option>
                                                <option value="Widow">Widow</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <!-- Age + Residency Status (ADDED) -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Age</label>
                                            <select class="form-select" name="txtage" id="txtage">
                                                <option value="">Age</option>
                                                <?php for ($i = 18; $i <= 75; $i++): ?>
                                                    <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                                                <?php endfor; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Residency Status</label>
                                            <select id="txtrstatus" name="txtrstatus" class="form-select">
                                                <option value="">Residency Status</option>
                                                <?php
                                                mysqli_data_seek($res_result, 0);
                                                while ($row = mysqli_fetch_assoc($res_result)):
                                                ?>
                                                    <option value="<?php echo htmlspecialchars($row['name']); ?>">
                                                        <?php echo htmlspecialchars($row['name']); ?>
                                                    </option>
                                                <?php endwhile; ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">

    <!-- Amount -->
    <div class="col-md-6">
        <div class="form-group">
            <label class="form-label">Amount</label>

            <input type="number"
                   id="txtamanual"
                   name="txtamanual"
                   class="form-control"
                   placeholder="Enter Amount"
                   value="<?php echo htmlspecialchars($memberrow->amount ?? ''); ?>" />
        </div>
    </div>

    <!-- Currency -->
    <div class="col-md-6">
        <div class="form-group">
            <label class="form-label">Currency</label>

            <select id="txtcurrency"
                    name="txtcurrency"
                    class="form-select">

                <option value="">Currency</option>

                <option value="INR"
                <?php if(($memberrow->currency ?? '')=='INR'){ echo 'selected'; } ?>>
                INR
                </option>

                <option value="US Dollar"
                <?php if(($memberrow->currency ?? '')=='US Dollar'){ echo 'selected'; } ?>>
                US Dollar
                </option>

                <option value="Canadian Dollar"
                <?php if(($memberrow->currency ?? '')=='Canadian Dollar'){ echo 'selected'; } ?>>
                Canadian Dollar
                </option>

                <option value="Pound"
                <?php if(($memberrow->currency ?? '')=='Pound'){ echo 'selected'; } ?>>
                Pound
                </option>

                <option value="USD"
                <?php if(($memberrow->currency ?? '')=='USD'){ echo 'selected'; } ?>>
                USD
                </option>

                <option value="CAD"
                <?php if(($memberrow->currency ?? '')=='CAD'){ echo 'selected'; } ?>>
                CAD
                </option>

                <option value="GBP"
                <?php if(($memberrow->currency ?? '')=='GBP'){ echo 'selected'; } ?>>
                GBP
                </option>

                <option value="AUD"
                <?php if(($memberrow->currency ?? '')=='AUD'){ echo 'selected'; } ?>>
                AUD
                </option>

            </select>
        </div>
    </div>

</div>

                               

                                <hr style="height:10px;">
                                <center><strong><h3 class="box-title">Profile Information</h3></strong></center>

                                <!-- Profile Name + SmartMatch -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Profile Name</label>
                                            <input type="text" maxlength="50" id="txtcname2" name="txtcname2" class="form-control" placeholder="Profile Name"/>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">SmartMatch Profile Link</label>
                                            <input type="text" maxlength="255" id="txtsmartmatch" name="txtsmartmatch" class="form-control" placeholder="SmartMatch Profile Link"/>
                                        </div>
                                    </div>
                                </div>

                                <!-- Profile Phone + Profile Email -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Profile Phone No.</label>
                                            <input type="text" maxlength="15" id="txtcontact2" name="txtcontact2" class="form-control" placeholder="Profile Phone No."/>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Profile Email-ID</label>
                                            <input type="text" id="txtemailid2" autocomplete="off" name="txtemailid2" class="form-control" placeholder="Profile Email-ID"/>
                                        </div>
                                    </div>
                                </div>

                                <hr style="height:10px;">
                                <center><strong><h3 class="box-title">Matches</h3></strong></center>

                                <?php for ($m = 1; $m <= 5; $m++): ?>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Name <?php echo $m; ?></label>
                                            <input type="text" maxlength="50" id="txtname<?php echo $m; ?>" name="txtname<?php echo $m; ?>" class="form-control" placeholder="Name <?php echo $m; ?>"/>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">URL <?php echo $m; ?></label>
                                            <input type="text" maxlength="250" id="txturl<?php echo $m; ?>" name="txturl<?php echo $m; ?>" class="form-control" placeholder="URL <?php echo $m; ?>"/>
                                        </div>
                                    </div>
                                </div>
                                <?php endfor; ?>

                                <!-- Video Calling + Video Link -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Video Calling</label>
                                            <select id="txtvideocall" name="txtvideocall" class="form-select">
                                                <option value="">Video Calling</option>
                                                <option value="Yes">Yes</option>
                                                <option value="No">No</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Video Link</label>
                                            <input type="text" maxlength="250" id="txtvideolink" name="txtvideolink" class="form-control" placeholder="Video Link"/>
                                        </div>
                                    </div>
                                </div>

                                <!-- Created On + Allocated To -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Created On</label>
                                            <input type="text" id="txtDate" name="txtDate" class="form-control"
                                                   value="<?php echo date('Y-m-d'); ?>" autocomplete="off" readonly="readonly"/>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Lead Allocated To</label>
                                            <select id="ddlLeadAllocated" name="ddlLeadAllocated" class="form-select">
                                                <option value="">Lead Allocated To</option>
                                                <?php
                                                mysqli_data_seek($userresult, 0);
                                                while ($userrow = mysqli_fetch_object($userresult)):
                                                ?>
                                                    <option value="<?php echo $userrow->user_id; ?>">
                                                        <?php echo htmlspecialchars($userrow->user_full_name); ?>
                                                    </option>
                                                <?php endwhile; ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <hr style="height:10px;">

                                <!-- Notes -->
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label class="form-label">Add Notes</label>
                                            <textarea maxlength="500" style="height:100px" id="txtRemarks" name="txtRemarks" class="form-control" placeholder="Add Notes"></textarea>
                                        </div>
                                    </div>
                                </div>

                                <!-- Next Call Date + Time -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Next Date of Call</label>
                                            <input type="date" id="txtcall" name="txtcall" class="form-control"/>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="form-label">Next Time of Call</label>
                                            <input type="time" id="call_time_scheduling" name="call_time_scheduling" class="form-control"/>
                                        </div>
                                    </div>
                                </div>

                                <div class="box-footer">
                                    <input type="submit" id="btnSaveMember" name="btnSaveMember"
                                           value="Save Rishta Lead" class="btn btn-primary"
                                           onclick="return saveuser()"/>
                                    <input type="button" name="btnBack" value="Back"
                                           class="btn btn-warning" onclick="return back(1)"/>
                                </div>

                            </div><!-- /.box-body -->
                        </form>
                    </div><!-- /.box -->
                </div>
            </div>
        </section>
    </div>
</div>

<script>
    $('#div_error').hide();

    function checkContactNumber() {
        var txtcontact = $('#txtcontact').val();
        if ($.trim(txtcontact).length > 0) {
            $.ajax({
                url:  'common_ajax.php',
                type: 'POST',
                data: { ajax_for: 'unique_contact', contact_no: txtcontact },
                success: function (result) {
                    if (result !== "pass") {
                        $('#txtcontact').val('');
                        $('#div_error').html(result).show();
                    }
                }
            });
        }
    }

    $('#txtcontact').on('blur', checkContactNumber);

    function saveuser() {
        var txtLeadSource  = $('#txtLeadSource').val();
        var ddlLeadStatus  = $('#ddlLeadStatus').val();
        var txtcountry     = $('#countryId').val();
        var txtcname       = $('#txtcname').val();
        var txtcontact     = $('#txtcontact').val();
        var txtemailid     = $('#txtemailid').val();
        var emailReg       = /^([\w\-\.]+@([\w\-]+\.)+[\w\-]{2,4})?$/;

        if (!txtLeadSource || !ddlLeadStatus || !txtcountry || !txtcname || !txtcontact || !txtemailid) {
            alert("Please fill all the required fields.");
            return false;
        }
        if (!emailReg.test(txtemailid)) {
            alert("Enter a valid email address.");
            return false;
        }
        return confirm('Are you sure you want to save this lead?');
    }

    function back(param) {
        if (confirm('Are you sure?')) {
            window.location = param === 0 ? "manage-leads.php" : "filter-manage-rishta-leads.php";
        }
        return false;
    }
</script>

<?php include('./include/footer.php'); ?>