<?php
require('top.inc.php');

$contact_no = '';
$buyer_name = '';
$address_1 = '';
$address_2 = '';
$city = '';
$state = '';
$pin = '';
$product = '';
$price = '';
$upi_id = '';
$payment_amount = '';
$msg = '';

if (isset($_GET['id']) && $_GET['id'] != '') {
    $id = get_safe_value($con, $_GET['id']);
    $res = mysqli_query($con, "select * from 500_ap where id='$id'");
    $check = mysqli_num_rows($res);
    if ($check > 0) {
        $row = mysqli_fetch_assoc($res);
        $contact_no = $row['Contact_No'];
        $buyer_name = $row['Buyer_name'];
        $address_1 = $row['Address_Line_1'];
        $address_2 = $row['Address_Line_2'];
        $city = $row['City'];
        $state = $row['State'];
        $pin = $row['PIN_Code'];
        $product = $row['Product'];
        $price = $row['price'];
        $upi_id = $row['upi_id'];
        $payment_amount = $row['payment_amount'];
    } else {
        header('location:show.php');
        die();
    }
}

if (isset($_POST['submit'])) {
    $contact_no = get_safe_value($con, $_POST['contact_no']);
    $buyer_name = get_safe_value($con, $_POST['buyer_name']);
    $address_1 = get_safe_value($con, $_POST['address_1']);
    $address_2 = get_safe_value($con, $_POST['address_2']);
    $city = get_safe_value($con, $_POST['city']);
    $state = get_safe_value($con, $_POST['state']);
    $pin = get_safe_value($con, $_POST['pin']);
    $product = get_safe_value($con, $_POST['product']);
    $price = get_safe_value($con, $_POST['price']);
    $upi_id = get_safe_value($con, $_POST['upi_id']);
    $payment_amount = get_safe_value($con, $_POST['payment_amount']);

    if (isset($_GET['id']) && $_GET['id'] != '') {
        $update_sql = "update 500_ap set Contact_No='$contact_no', Buyer_name='$buyer_name', Address_Line_1='$address_1', Address_Line_2='$address_2', City='$city', State='$state', PIN_Code='$pin', Product='$product', price='$price', upi_id='$upi_id', payment_amount='$payment_amount' where id='$id'";
        mysqli_query($con, $update_sql);
    } else {
        mysqli_query($con, "insert into 500_ap(Contact_No, Buyer_name, Address_Line_1, Address_Line_2, City, State, PIN_Code, Product, price, upi_id, payment_amount) values('$contact_no', '$buyer_name', '$address_1', '$address_2', '$city', '$state', '$pin', '$product', '$price', '$upi_id', '$payment_amount')");
    }
    header('location:show.php');
    die();
}
?>
<div class="content pb-0">
    <div class="animated fadeIn">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-header"><strong>User</strong><small> Form</small></div>
                    <form method="post">
                        <div class="card-body card-block">
                            <div class="row">
                                <div class="form-group col-md-6">
                                    <label for="contact_no" class="form-control-label">Contact No</label>
                                    <input type="text" name="contact_no" placeholder="Enter contact number" class="form-control" required value="<?php echo $contact_no ?>">
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="buyer_name" class="form-control-label">Buyer Name</label>
                                    <input type="text" name="buyer_name" placeholder="Enter buyer name" class="form-control" required value="<?php echo $buyer_name ?>">
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group col-md-6">
                                    <label for="address_1" class="form-control-label">Address Line 1</label>
                                    <input type="text" name="address_1" placeholder="Address 1" class="form-control" value="<?php echo $address_1 ?>">
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="address_2" class="form-control-label">Address Line 2</label>
                                    <input type="text" name="address_2" placeholder="Address 2" class="form-control" value="<?php echo $address_2 ?>">
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group col-md-4">
                                    <label for="city" class="form-control-label">City</label>
                                    <input type="text" name="city" placeholder="City" class="form-control" value="<?php echo $city ?>">
                                </div>
                                <div class="form-group col-md-4">
                                    <label for="state" class="form-control-label">State</label>
                                    <input type="text" name="state" placeholder="State" class="form-control" value="<?php echo $state ?>">
                                </div>
                                <div class="form-group col-md-4">
                                    <label for="pin" class="form-control-label">PIN Code</label>
                                    <input type="text" name="pin" placeholder="PIN" class="form-control" value="<?php echo $pin ?>">
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group col-md-6">
                                    <label for="product" class="form-control-label">Product</label>
                                    <input type="text" name="product" placeholder="Enter product name" class="form-control" value="<?php echo $product ?>">
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="price" class="form-control-label">Price</label>
                                    <input type="text" name="price" placeholder="Enter price" class="form-control" value="<?php echo $price ?>">
                                </div>
                            </div>
                            <div class="row" style="background: #f8f9fa; padding: 15px; border-radius: 5px; margin: 10px 0; border: 1px solid #ddd;">
                                <div class="col-md-12"><h5>Payment Settings (UPI Integration)</h5></div>
                                <div class="form-group col-md-6">
                                    <label for="upi_id" class="form-control-label"><strong>UPI ID (Receiver)</strong></label>
                                    <input type="text" name="upi_id" placeholder="example@upi" class="form-control" value="<?php echo $upi_id ?>">
                                    <small class="text-muted">User will pay to this UPI ID</small>
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="payment_amount" class="form-control-label"><strong>Payment Amount (INR)</strong></label>
                                    <input type="text" name="payment_amount" placeholder="e.g. 4500.00" class="form-control" value="<?php echo $payment_amount ?>">
                                    <small class="text-muted">Exact amount user will pay in the app</small>
                                </div>
                            </div>
                            <button id="payment-button" name="submit" type="submit" class="btn btn-lg btn-info btn-block">
                                <span id="payment-button-amount">Submit</span>
                            </button>
                            <div class="field_error"><?php echo $msg ?></div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<?php
require('footer.inc.php');
?>
