<?php
ob_start();

//############################################################################//
// Script Name: crm_validate.php                                              //
// Last Modified: Modernized 2025                                             //
// Description: Handles insert/update to partners_crm                         //
//############################################################################//

include '../includes/session.php';
include '../includes/constants.php';
include '../includes/functions.php';
include '../includes/allstripslashes.php';

// Create mysqli connection
$connection = new mysqli($host, $user, $pass, $db);
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

// Load class and initialize
$partners = new partners;
$partners->connection($host, $user, $pass, $db);

// Sanitize and fetch inputs
$crm_affiliate_id = intval(trim($_POST['crm_affiliate_list'] ?? ''));
$crm_cat_id       = trim($_POST['crm_cat_id'] ?? '');
$crm_date         = trim($_POST['crm_date'] ?? '');
$crm_crdate       = trim($_POST['crm_crdate'] ?? '');
$crm_type         = trim($_POST['crm_type'] ?? '');
$crm_subject      = trim($_POST['crm_subject'] ?? '');
$crm_note         = trim($_POST['crm_note'] ?? '');
$crm_flag         = trim($_POST['crm_flag'] ?? '');
$crm_action       = trim($_POST['crm_action'] ?? '');
$crm_merchant_id  = intval(trim($_POST['crm_merchant_id'] ?? ''));
$crm_id           = trim($_POST['crm_id'] ?? '');

// Return URL (with preserved form values)
$url = "index.php?Act=list_crm&mode=$crm_action&crm_merchant_id=$crm_merchant_id&id=$crm_id&crm_affiliate_id=$crm_affiliate_id&crm_cat_id=$crm_cat_id&crm_date=$crm_date&crm_subject=$crm_subject&crm_note=$crm_note&crm_flag=$crm_flag";

// ------------------------------
// Step 1: Validate Required Fields
// ------------------------------
$err = '';
$err .= empty($crm_affiliate_id) ? "1" : "0";
$err .= empty($crm_cat_id)       ? ".1" : ".0";
$err .= empty($crm_date)         ? ".1" : ".0";
$err .= empty($crm_subject)      ? ".1" : ".0";
$err .= empty($crm_crdate)       ? ".1" : ".0";
$err .= empty($crm_type)         ? ".1" : ".0";
$err .= empty($crm_note)         ? ".1" : ".0";
$err .= empty($crm_flag)         ? ".1" : ".0";

if ($err != "0.0.0.0.0.0.0.0") {
    $errMsg = 1;
    header("Location: $url&errMsg=$errMsg");
    exit;
}

// ------------------------------
// Step 2: Date Format Validation
// ------------------------------
$crm_date   = $partners->date2mysql($crm_date);
$crm_crdate = $partners->date2mysql($crm_crdate);

if ($partners->is_date($crm_date) || $partners->is_date($crm_crdate)) {
    $errMsg = 2;
    header("Location: $url&errMsg=$errMsg");
    exit;
}

// ------------------------------
// Step 3: Execute Query (Insert or Update)
// ------------------------------
switch ($crm_action) {
    case "Add New":
        $stmt = $connection->prepare("
            INSERT INTO partners_crm 
            (crm_merchantid, crm_affiliateid, crm_catid, crm_date, crm_flag, crm_subject, crm_note, crm_crdate, crm_type)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
        ");
        $stmt->bind_param(
            "iisssssss",
            $crm_merchant_id,
            $crm_affiliate_id,
            $crm_cat_id,
            $crm_date,
            $crm_flag,
            $crm_subject,
            $crm_note,
            $crm_crdate,
            $crm_type
        );
        $stmt->execute();
        $stmt->close();
        $errMsg = 4;
        break;

    case "Edit":
        $stmt = $connection->prepare("
            UPDATE partners_crm SET
                crm_merchantid = ?,
                crm_affiliateid = ?,
                crm_catid = ?,
                crm_date = ?,
                crm_flag = ?,
                crm_subject = ?,
                crm_note = ?,
                crm_crdate = ?,
                crm_type = ?
            WHERE crm_id = ?
        ");
        $stmt->bind_param(
            "iisssssssi",
            $crm_merchant_id,
            $crm_affiliate_id,
            $crm_cat_id,
            $crm_date,
            $crm_flag,
            $crm_subject,
            $crm_note,
            $crm_crdate,
            $crm_type,
            $crm_id
        );
        $stmt->execute();
        $stmt->close();
        $errMsg = 5;
        break;
}

header("Location: index.php?Act=list_crm&errMsg=$errMsg");
exit;
