<?php

function findIt($jarr, $fkey, $val, $rkey) {
    foreach ($jarr as $dic) {
        if (strcmp($dic[$fkey],$val)==0)
            return $dic[$rkey];
    }
    return "";
}

$userid = $_POST["userID"];

$string = file_get_contents("JSON/WMU.json");
$j_wmu = json_decode($string, true);


$PointsRight = $_POST['PointsRight'];
$PointsLeft = $_POST['PointsLeft'];
$SpurLengthInches = $_POST['SpurLengthInches'];
$spurLength = $_POST['spurLength'];
$TagNumber = $_POST['TagNumber'];
$NoHarvest = $_POST['NoHarvest'];
$Coupon = $_POST['Coupon'];
$YouthTaken = $_POST['YouthTaken'];
$BeardLengthinches = $_POST['BeardLengthinches'];
$BeardLength = $_POST['BeardLength'];
$DMAPUnit = $_POST['DMAPUnit'];
$GameID = $_POST['GameID'];
$KillDate = date_format(date_create($_POST['KillDate']),  "Y-m-d H:i:s");
$County = $_POST['County'];
$Sex = $_POST['Sex'];
$WMU = findIt($j_wmu, 'description', $_POST['WMU'], 'wmu_id');;
$User = $_POST['User'];
$Taken = $_POST['Taken'];
$Township = $_POST['Township'];

//NSString *post = [NSString stringWithFormat:@"PointsLeft=%@&PointsRight=%@&SpurLengthInches=%@&spurLength=%@TagNumber=%@&NoHarvest=%@&Coupon=%@&YouthTaken=%@&BeardLengthinches%@&BearLength=%@&DMAPUnit=%@&GameID=%@&KillDate=%@&County=%@&Sex=%@&WMU=%@&User=%@&Taken=%@&Township=%@", kill.PointsLeft, kill.PointsRight, kill.SpurLengthInches, kill.SpurLength, kill.NoHarvest, kill.TagNumber, kill.Coupon, kill.YouthTaken, kill.BeardLengthInches, kill.BeardLength, kill.DMAPUnit, kill.gameID, kill.killDate, kill.countyID, kill.sex, kill.WMU, kill.userID, kill.TakenWithID, kill.TownshipID];

function newIDnum($connection)
{
    $query = "select * from userkill order by kill_id desc";
    $result = $connection->query($query);
    if ($result->num_rows == 0) {
        //echo "No rows";
        return 1;
    }
    else {
        $row = $result->fetch_assoc();   //  get the first row
        $oid = $row['kill_id'];
        if ($oid<1000) {
            $oid=$oid+1000;
        }
        return $oid+1;
    }
}

$servername = "localhost";
$username = "biggame";
$password = "MaRtIn";
$dbname = "BigGame";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//NSString *post = [NSString stringWithFormat:@"PointsLeft=%@&PointsRight=%@&SpurLengthInches=%@&spurLength=%@TagNumber=%@&NoHarvest=%@&Coupon=%@&YouthTaken=%@&BeardLengthinches%@&BearLength=%@&DMAPUnit=%@&GameID=%@&KillDate=%@&County=%@&Sex=%@&WMU=%@&User=%@&Taken=%@&Township=%@", kill.PointsLeft, kill.PointsRight, kill.SpurLengthInches, kill.SpurLength, kill.TahNumber, kill.NoHarvest, kill.Coupon, kill.YouthTaken, kill.BeardLengthInches, kill.BeardLength, kill.DMAPUnit, kill.gameID, kill.killDate, kill.countyID, kill.sex, kill.WMU, kill.userID, kill.TakenWithID, kill.TownshipID];

$iid = newIDnum($conn);
$sql = "INSERT INTO userkill (kill_id, user_id, game_id, sex_id, kill_date_time, county_id, wmu_id, taken_id, township_id, tag_number, points_left, points_right, no_harvest, youth_taken, coupon, dmapunit, beard_length, beardlength_inches, spur_length, spurlength_inches, deleted_flg) VALUES ('$iid', '$User', '$GameID', '$Sex', '$KillDate', '$County', '$WMU', '$Taken', '$Township', '$TagNumber', '$PointsLeft', '$PointsRight', '$NoHarvest', '$YouthTaken', '$Coupon', '$DMAPUnit', '$BeardLengthinches', '$BeardLength', '$SpurLengthInches', '$spurLength', '0')";

if ($conn->query($sql) === TRUE) {
    echo '{"Success":true,"Message":"' . $sql . '","Response":null}';
} else {
    echo '{"Success":false,"Message":"insert failed ' . $sql. '.","Response":null}';
}

?>
