<?php

require '../lib/dbclass.php';

$id = $_POST['id'];

if (!$id) {
    // updateQuery("Update tbl_challenge set status=1");
}

if (!$id) {
    $id = 0;
}

$challenge_startdate              = $_POST['challenge_startdate'];
$challenge_enddate                = $_POST['challenge_enddate'];
$_POST['sponsorproductid1']       = $_POST['sponsorproductid'][0] ? $_POST['sponsorproductid'][0] : 0;
$_POST['sponsorproductid2']       = $_POST['sponsorproductid'][1] ? $_POST['sponsorproductid'][1] : 0;
$_POST['sponsorproductid3']       = $_POST['sponsorproductid'][2] ? $_POST['sponsorproductid'][2] : 0;
$_POST['sponsorproductid4']       = $_POST['sponsorproductid'][3] ? $_POST['sponsorproductid'][3] : 0;
$_POST['sponsorproductid5']       = $_POST['sponsorproductid'][4] ? $_POST['sponsorproductid'][4] : 0;
$_POST['showdistrictandassembly'] = 0;

if ($_POST['showdistrictandassembly'] == 'on') {
    $_POST['showdistrictandassembly'] = 1;
}

if ($_POST['sponsorproductid1'] > 0) {
    $prodid = $_POST['sponsorproductid1'];

    $rate = findQuery("select * from  `tbl_product` where id=$prodid");

    if ($rate) {
        $rate           = $rate[0]['amount'];
        $_POST['rate1'] = $rate;
    }
}

if ($_POST['sponsorproductid2'] > 0) {
    $prodid = $_POST['sponsorproductid2'];

    $rate = findQuery("select * from  `tbl_product` where id=$prodid");

    if ($rate) {
        $rate           = $rate[0]['amount'];
        $_POST['rate2'] = $rate;
    }
}

if ($_POST['sponsorproductid3'] > 0) {
    $prodid = $_POST['sponsorproductid3'];

    $rate = findQuery("select * from  `tbl_product` where id=$prodid");

    if ($rate) {
        $rate           = $rate[0]['amount'];
        $_POST['rate3'] = $rate;
    }
}

if ($_POST['sponsorproductid4'] > 0) {
    $prodid = $_POST['sponsorproductid4'];

    $rate = findQuery("select * from  `tbl_product` where id=$prodid");

    if ($rate) {
        $rate           = $rate[0]['amount'];
        $_POST['rate4'] = $rate;
    }
}

if ($_POST['sponsorproductid5'] > 0) {
    $prodid = $_POST['sponsorproductid5'];

    $rate = findQuery("select * from  `tbl_product` where id=$prodid");

    if ($rate) {
        $rate           = $rate[0]['amount'];
        $_POST['rate5'] = $rate;
    }
}




$_POST['product_amount2'] = $_POST['product_amount2'] ? $_POST['product_amount2'] : 0;
$_POST['product_amount3'] = $_POST['product_amount3'] ? $_POST['product_amount3'] : 0;
$_POST['delivary_date2']  = $_POST['delivary_date2'] ? $_POST['delivary_date2'] : '2000-01-01';
$_POST['delivary_date3']  = $_POST['delivary_date3'] ? $_POST['delivary_date3'] : '2000-01-01';
unset($_POST['sponsorproductid']);

if ($challenge_enddate <= $challenge_startdate) {
    $message = 'Check Start & end dates';

    $encodedMessage = urlencode($message);

    $url = "add_challenge.php?message={$encodedMessage}";

    // Redirect to the new URL
    echo "<script>window.location.href='{$url}';</script>";

    return;
}

$allowed_image_extension = ['jpg', 'jpeg'];

if ($_FILES['challenge_image']['name']) {
    $file_extension = pathinfo($_FILES['challenge_image']['name'], PATHINFO_EXTENSION);
    $filename       = $_FILES['challenge_image']['name'];
    $filetmp        = $_FILES['challenge_image']['tmp_name'];

    $image_info   = getimagesize($filetmp);
    $image_width  = $image_info[0];
    $image_height = $image_info[1];



    if ($image_width != 326 || $image_height != 203) {
        $message = 'Challenge image size must be 326 x 203 pixels';

        $encodedMessage = urlencode($message);

        $url = "add_challenge.php?message={$encodedMessage}";

        // Redirect to the new URL
        echo "<script>window.location.href='{$url}';</script>";

        return;
    }


    if ($filename) {
        if (!in_array($file_extension, $allowed_image_extension)) {
            $message = 'Please upload jpg,jpeg images';

            $encodedMessage = urlencode($message);

            $url = "add_challenge.php?message={$encodedMessage}";

            // Redirect to the new URL
            echo "<script>window.location.href='{$url}';</script>";

            return;
        }

        if ($_FILES['challenge_image']['size'] > 1000000) {
            $message = 'Challenge Image size exceeds 1 MB';

            $encodedMessage = urlencode($message);

            $url = "add_challenge.php?message={$encodedMessage}";

            // Redirect to the new URL
            echo "<script>window.location.href='{$url}';</script>";

            return;
        }
    }
}



if ($_FILES['product_image1']['name']) {
    $filename1      = $_FILES['product_image1']['name'];
    $filetmp1       = $_FILES['product_image1']['tmp_name'];
    $file_extension = pathinfo($_FILES['product_image1']['name'], PATHINFO_EXTENSION);

    $image_info   = getimagesize($filetmp1);
    $image_width  = $image_info[0];
    $image_height = $image_info[1];



    if ($image_width != 326 || $image_height != 164) {
        $message = 'Product-1 image size must be 326 x 164 pixels';

        $encodedMessage = urlencode($message);

        $url = "add_challenge.php?message={$encodedMessage}";

        // Redirect to the new URL
        echo "<script>window.location.href='{$url}';</script>";

        return;
    }

    if ($filename1) {
        if (!in_array($file_extension, $allowed_image_extension)) {
            $message = 'Please upload jpg,jpeg images';

            $encodedMessage = urlencode($message);

            $url = "add_challenge.php?message={$encodedMessage}";

            // Redirect to the new URL
            echo "<script>window.location.href='{$url}';</script>";

            return;
        }

        if ($_FILES['product_image1']['size'] > 1000000) {
            $message = 'Product-1 Image size exceeds 1 MB';

            $encodedMessage = urlencode($message);

            $url = "add_challenge.php?message={$encodedMessage}";

            // Redirect to the new URL
            echo "<script>window.location.href='{$url}';</script>";

            return;
        }
    }
}

if ($_FILES['product_image2']['name']) {
    $filename2      = $_FILES['product_image2']['name'];
    $filetmp2       = $_FILES['product_image2']['tmp_name'];
    $file_extension = pathinfo($_FILES['product_image2']['name'], PATHINFO_EXTENSION);
    $image_info     = getimagesize($filetmp2);
    $image_width    = $image_info[0];
    $image_height   = $image_info[1];



    if ($image_width != 326 || $image_height != 164) {
        $message = 'Product-2 image size must be 326 x 164 pixels';

        $encodedMessage = urlencode($message);

        $url = "add_challenge.php?message={$encodedMessage}";

        // Redirect to the new URL
        echo "<script>window.location.href='{$url}';</script>";

        return;
    }

    if ($filename2) {
        if (!in_array($file_extension, $allowed_image_extension)) {
            $message = 'Please upload jpg,jpeg images';

            $encodedMessage = urlencode($message);

            $url = "add_challenge.php?message={$encodedMessage}";

            // Redirect to the new URL
            echo "<script>window.location.href='{$url}';</script>";

            return;
        }

        if ($_FILES['product_image2']['size'] > 1000000) {
            $message = 'Product-2 Image size exceeds 1 MB';

            $encodedMessage = urlencode($message);

            $url = "add_challenge.php?message={$encodedMessage}";

            // Redirect to the new URL
            echo "<script>window.location.href='{$url}';</script>";

            return;
        }
    }
}

if ($_FILES['product_image3']['name']) {
    $filename3      = $_FILES['product_image3']['name'];
    $filetmp3       = $_FILES['product_image3']['tmp_name'];
    $file_extension = pathinfo($_FILES['product_image3']['name'], PATHINFO_EXTENSION);
    $image_info     = getimagesize($filetmp3);
    $image_width    = $image_info[0];
    $image_height   = $image_info[1];



    if ($image_width != 326 || $image_height != 164) {
        $message = 'Product-3 image size must be 326 x 164 pixels';

        $encodedMessage = urlencode($message);

        $url = "add_challenge.php?message={$encodedMessage}";

        // Redirect to the new URL
        echo "<script>window.location.href='{$url}';</script>";

        return;
    }

    if ($filename3) {
        if (!in_array($file_extension, $allowed_image_extension)) {
            $message = 'Please upload jpg,jpeg images';

            $encodedMessage = urlencode($message);

            $url = "add_challenge.php?message={$encodedMessage}";

            // Redirect to the new URL
            echo "<script>window.location.href='{$url}';</script>";

            return;
        }

        if ($_FILES['product_image3']['size'] > 1000000) {
            $message = 'Product-3 Image size exceeds 1 MB';

            $encodedMessage = urlencode($message);

            $url = "add_challenge.php?message={$encodedMessage}";

            // Redirect to the new URL
            echo "<script>window.location.href='{$url}';</script>";

            return;
        }
    }
}


$count = 0;

$existstart = findQuery("select * from  `tbl_challenge` where '$challenge_startdate'  between `challenge_startdate` and `challenge_enddate` and status=0");

if ($existstart) {
    $count = 1;
}

if ($count > 0) {
    $message = 'A challenge already exist in same period';

    $encodedMessage = urlencode($message);

    $url = "add_challenge.php?message={$encodedMessage}";

    // Redirect to the new URL
    echo "<script>window.location.href='{$url}';</script>";

    return;
} else {
    unset($_POST['id']);



    $id = saveArray(' tbl_challenge', $_POST, $id);
}

if ($id) {
    $message = 'Registered successfully!';

    if ($filename) {
        $picname = $_POST['challenge_name'] . '-' . $id;
        updateQuery("Update tbl_challenge set challenge_image='$picname' where id=$id");
        $filepath = 'upload/' . $picname;
        move_uploaded_file($filetmp, $filepath);
    }

    if ($filename1) {
        $picname = $_POST['product_name1'] . '-' . $id;
        updateQuery("Update tbl_challenge set product_image1='$picname' where id=$id");
        $filepath = 'upload/' . $picname;
        move_uploaded_file($filetmp1, $filepath);
    }

    if ($filename2) {
        $picname = $_POST['product_name2'] . '-' . $id;
        updateQuery("Update tbl_challenge set product_image2='$picname' where id=$id");
        $filepath = 'upload/' . $picname;
        move_uploaded_file($filetmp2, $filepath);
    }

    if ($filename3) {
        $picname = $_POST['product_name3'] . '-' . $id;
        updateQuery("Update tbl_challenge set product_image3='$picname' where id=$id");
        $filepath = 'upload/' . $picname;
        move_uploaded_file($filetmp3, $filepath);
    }

    $encodedMessage = urlencode($message);

    $url = "add_challenge.php?message={$encodedMessage}";

    // Redirect to the new URL
    echo "<script>window.location.href='{$url}';</script>";

    return;
} else {
    $message = 'Something went wrong, please try again.';

    $encodedMessage = urlencode($message);

    $url = "add_challenge.php?message={$encodedMessage}";

    // Redirect to the new URL
    echo "<script>window.location.href='{$url}';</script>";

    return;
}
