<?php

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

$id = $_POST['id'];

$custid = $_POST['custid'];

if ($_POST['clubid'] == '') {
    $clubid = 0;
} else {
    $clubid = $_POST['clubid'];
}

$name   = $_POST['name'];
$mobile = $_POST['mobile'];
$ward   = $_POST['ward_id'];
$type   = $_POST['type'];
$typeid = $_POST['typeid'];


if ($clubid > 0) {
    //$dataward = findQuery("select * from tbl_customer where id=$custid");
    //$panchayat=$dataward[0]['Panchayat'];

    //$dataward = findQuery("select * from tbl_ward where panchayat_id=$panchayat and ward_name='NA'");
    //$ward=$dataward[0]['id'];
}



if ($typeid == 1 || $typeid == 0) {// change from challenge or sponsorship
    if ($type == 2) {
        updateQuery(
            "update tbl_transaction set sponsorship=0,donation=1,challengeid=0 where id=$id"
        );
        updateQuery("Delete from  tbl_transactiondetails  where hdrid=$id");
    } else {
        if ($type == 0) {
            updateQuery(
                "update tbl_transaction set sponsorship=0,donation=0,challengeid=1 where id=$id"
            );
        } elseif ($type == 1) {
            updateQuery(
                "update tbl_transaction set sponsorship=1,donation=0,challengeid=1 where id=$id"
            );
        }
    }
} else { //change from donation
    $datachlng = findQuery(
        "select tbl_transaction.* from tbl_transaction  where tbl_transaction.id=$id"
    );

    $quantity = 0;
    $Amount   = 0;

    $price_per_item = 500;
    $Amount         = $datachlng[0]['Amount'];
    $quantity       = floor($Amount / $price_per_item);

    if ($type == 0) {
        updateQuery(
            "update tbl_transaction set sponsorship=0,donation=0,challengeid=1 where id=$id"
        );

        if ($quantity > 0) {
            $data_detail['hdrid']  = $id;
            $data_detail['item']   = 1;
            $data_detail['qty']    = $quantity;
            $data_detail['Totqty'] = $quantity;
            $data_detail['rate']   = 500;
            $data_detail['amount'] = $quantity * 500;

            $iddtl = saveArray('tbl_transactiondetails', $data_detail, 0);
        }
    } elseif ($type == 1) {
        updateQuery(
            "update tbl_transaction set sponsorship=1,donation=0,challengeid=1 where id=$id"
        );

        if ($quantity > 0) {
            $data_detail['hdrid']  = $id;
            $data_detail['item']   = 1;
            $data_detail['qty']    = $quantity;
            $data_detail['Totqty'] = $quantity;
            $data_detail['rate']   = 500;
            $data_detail['amount'] = $quantity * 500;

            $iddtl = saveArray('tbl_transactiondetails', $data_detail, 0);
        }
    } else {
    }
}

updateQuery(
    "update tbl_customer set name='$name',mobile='$mobile',Ward=$ward  where id=$custid"
);

updateQuery("update tbl_transaction set clubid=$clubid where id=$id");

$message = 'Edited successfully!';

$encodedMessage = urlencode($message);

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

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