<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

include('./include/settings.php');

$message = "";

if(isset($_POST['import']))
{
    if(isset($_FILES['csv_file']) && $_FILES['csv_file']['error'] == 0)
    {
        $file = $_FILES['csv_file']['tmp_name'];

        $handle = fopen($file, "r");

        if($handle)
        {
            $row = 0;
            $inserted = 0;

            while(($data = fgetcsv($handle, 1000, ",")) !== FALSE)
            {
                $row++;

                // Skip Header Row
                if($row == 1)
                {
                    continue;
                }

                // CSV DATA
                $contact_name = mysqli_real_escape_string($mysqli, trim($data[0]));
                $contact_no   = mysqli_real_escape_string($mysqli, trim($data[1]));
                $email_id     = mysqli_real_escape_string($mysqli, trim($data[2]));

                // INSERT QUERY
                $sql = "INSERT INTO rishta_leadmaster
                (
                    contact_name,
                    contact_no,
                    email_id
                )
                VALUES
                (
                    '$contact_name',
                    '$contact_no',
                    '$email_id'
                )";

                $query = mysqli_query($mysqli, $sql);

                if($query)
                {
                    $inserted++;
                }
                else
                {
                    $message .= "
                    <div class='alert alert-danger'>
                        ".mysqli_error($mysqli)."
                    </div>";
                }
            }

            fclose($handle);

            $message .= "
            <div class='alert alert-success'>
                CSV Uploaded Successfully.<br>
                Total Rows Inserted : ".$inserted."
            </div>";
        }
        else
        {
            $message = "
            <div class='alert alert-danger'>
                Unable to read CSV file.
            </div>";
        }
    }
    else
    {
        $message = "
        <div class='alert alert-danger'>
            Please select CSV file.
        </div>";
    }
}
?>

<!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>Upload Data - Rishta Depot</title>

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

<style>

.upload-card{
    border-radius:12px;
    overflow:hidden;
    box-shadow:0 2px 10px rgba(0,0,0,0.08);
}

.upload-header{
    background:#fff;
    padding:20px;
    border-bottom:1px solid #e9ecef;
}

.upload-header h3{
    margin:0;
    font-size:24px;
    font-weight:600;
    color:#1d2b53;
}

.upload-body{
    background:#fff;
    padding:30px;
}

.form-label{
    font-weight:600;
    color:#1d2b53;
    margin-bottom:10px;
}

.csv-upload-box{
    border:2px dashed #6f42c1;
    border-radius:12px;
    padding:35px;
    background:#faf8ff;
    transition:0.3s;
}

.csv-upload-box:hover{
    background:#f3eeff;
}

.upload-btn{
    background:#6f42c1;
    border:none;
    padding:10px 30px;
    font-size:16px;
    font-weight:600;
    border-radius:8px;
}

.upload-btn:hover{
    background:#5a32a3;
}

.cancel-btn{
    padding:10px 30px;
    font-size:16px;
    font-weight:600;
    border-radius:8px;
}

.alert{
    border-radius:10px;
}

.info-text{
    color:#6c757d;
    font-size:14px;
    margin-top:10px;
}

</style>

</head>

<body>

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

<div class="content-wrapper">

<div class="container-full">

<!-- Content Header -->
<div class="content-header">

<div class="d-flex align-items-center">

<div class="me-auto">

<h4 class="page-title">
    Upload CSV File
</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">
Upload Data
</li>

<li class="breadcrumb-item active">
Forms
</li>

</ol>

</nav>

</div>

</div>

</div>

</div>

<!-- Main content -->
<section class="content">

<div class="row">

<div class="col-12">

<div class="upload-card">

<div class="upload-header">

<h3>
Import CSV Data
</h3>

</div>

<div class="upload-body">

<!-- MESSAGE -->
<?php echo $message; ?>

<!-- FORM -->
<form class="form"
      method="POST"
      enctype="multipart/form-data">

<div class="row">

<div class="col-md-12">

<div class="form-group">

<label class="form-label">
Select CSV File
</label>

<div class="csv-upload-box">

<input type="file"
       name="csv_file"
       class="form-control"
       accept=".csv"
       required>

<div class="info-text">
Supported File Format : CSV
</div>

</div>

</div>

</div>

</div>

<!-- BUTTONS -->
<div class="mt-4">

<button type="submit"
        name="import"
        class="btn btn-primary upload-btn">

<i class="ti-upload"></i>
Import CSV

</button>

<button type="reset"
        class="btn btn-warning cancel-btn">

Cancel

</button>

</div>

</form>

</div>

</div>

</div>

</div>

</section>

</div>

</div>

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

</body>

</html>