<?php
require_once 'auth.php';

$conn = new mysqli("localhost","root","","tradelicense");

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

$zone_info_result = $conn->query(
"SELECT zone_info_id, municipality_name_english
 FROM ZoneInfoId"
);

$editMode = false;
$data = [];

if(isset($_GET['edit']))
{
    $editMode = true;

    $id = intval($_GET['edit']);

    $result = $conn->query(
        "SELECT * FROM details WHERE id='$id'"
    );

    if($result && $result->num_rows > 0){
        $data = $result->fetch_assoc();
    }
}

if($_SERVER["REQUEST_METHOD"]=="POST")
{
    $lic_id = $_POST['lic_id'];
    $zone_info_id = $_POST['zone_info_id'];
    $license_no = $_POST['license_no'];
    $license_id = $_POST['license_id'];
    $issue_date = $_POST['issue_date'];
    $renewal_year = $_POST['renewal_year'];
    $renewal_date = $_POST['renewal_date'];
    $expiry_date = $_POST['expiry_date'];

    $organization_name_bengali = $_POST['organization_name_bengali'];
    $organization_name_english = $_POST['organization_name_english'];

    $business_type_bengali = $_POST['business_type_bengali'];
    $business_type_english = $_POST['business_type_english'];

    $ward_bengali = $_POST['ward_bengali'];
    $ward_english = $_POST['ward_english'];

    $street_bengali = $_POST['street_bengali'];
    $street_english = $_POST['street_english'];

    $address_bengali = $_POST['address_bengali'];
    $address_english = $_POST['address_english'];
    $present_address = $_POST['present_address'];
    $parmanent_address = $_POST['parmanent_address'];



    $owner_name_bengali = $_POST['owner_name_bengali'];
    $owner_name_english = $_POST['owner_name_english'];
    $father_name = $_POST['father_name'];
    $mother_name = $_POST['mother_name'];
    $spouse_name = $_POST['spouse_name'];

    $nid = $_POST['nid'];
    $phone_number = $_POST['phone_number'];

    $license_fee = $_POST['license_fee'];
    $signboard_fee = $_POST['signboard_fee'];
    $miscellaneous_fee = $_POST['miscellaneous_fee'];
    $total_fee = $_POST['total_fee'];

    $surcharge = $_POST['surcharge'];
    $current_surcharge = $_POST['current_surcharge'];

    $vat = $_POST['vat'];
    $vat_arrear = $_POST['vat_arrear'];

    $income_tax = $_POST['income_tax'];
    $income_tax_arrear = $_POST['income_tax_arrear'];

    $arrear = $_POST['arrear'];
    $signboard_vat = $_POST['signboard_vat'];

    $valid = $_POST['valid'];

    if(isset($_GET['edit']))
    {
        $id = intval($_GET['edit']);

        $sql = "UPDATE details SET

        lic_id='$lic_id',
        zone_info_id='$zone_info_id',
        license_no='$license_no',
        license_id='$license_id',
        issue_date='$issue_date',
        renewal_year='$renewal_year',
        renewal_date='$renewal_date',
        expiry_date='$expiry_date',

        organization_name_bengali='$organization_name_bengali',
        organization_name_english='$organization_name_english',

        business_type_bengali='$business_type_bengali',
        business_type_english='$business_type_english',

        ward_bengali='$ward_bengali',
        ward_english='$ward_english',

        street_bengali='$street_bengali',
        street_english='$street_english',

        address_bengali='$address_bengali',
        address_english='$address_english',
        present_address='$present_address',
        parmanent_address='$parmanent_address',


        

        owner_name_bengali='$owner_name_bengali',
        owner_name_english='$owner_name_english',
        father_name='$father_name',
        mother_name='$mother_name',
        spouse_name='$spouse_name',

        nid='$nid',
        phone_number='$phone_number',

        license_fee='$license_fee',
        signboard_fee='$signboard_fee',
        miscellaneous_fee='$miscellaneous_fee',
        total_fee='$total_fee',

        surcharge='$surcharge',
        current_surcharge='$current_surcharge',

        vat='$vat',
        vat_arrear='$vat_arrear',

        income_tax='$income_tax',
        income_tax_arrear='$income_tax_arrear',

        arrear='$arrear',
        signboard_vat='$signboard_vat',
        valid='$valid'

        WHERE id='$id'";

        $conn->query($sql);

        header("Location: license_list.php");
        exit;
    }
    else
    {

        $insert_sql = "INSERT INTO details

        (
        lic_id,
        zone_info_id,
        license_no,
        license_id,
        issue_date,
        renewal_year,
        renewal_date,
        expiry_date,

        organization_name_bengali,
        organization_name_english,

        business_type_bengali,
        business_type_english,

        ward_bengali,
        ward_english,

        street_bengali,
        street_english,

        address_bengali,
        address_english,
       present_address,
       parmanent_address,
        owner_name_bengali,
        owner_name_english,
        father_name,
        mother_name,
        spouse_name,

        nid,
        phone_number,

        license_fee,
        signboard_fee,
        miscellaneous_fee,
        total_fee,

        surcharge,
        current_surcharge,

        vat,
        vat_arrear,

        income_tax,
        income_tax_arrear,

        arrear,
        signboard_vat,

        valid

        )

        VALUES

        (

        '$lic_id',
        '$zone_info_id',
        '$license_no',
        '$license_id',
        '$issue_date',
        '$renewal_year',
        '$renewal_date',
        '$expiry_date',

        '$organization_name_bengali',
        '$organization_name_english',

        '$business_type_bengali',
        '$business_type_english',

        '$ward_bengali',
        '$ward_english',

        '$street_bengali',
        '$street_english',

        '$address_bengali',
        '$address_english',
        '$present_address',
        '$parmanent_address',

        '$owner_name_bengali',
        '$owner_name_english',
        '$father_name',
        '$mother_name',
        '$spouse_name',

        '$nid',
        '$phone_number',

        '$license_fee',
        '$signboard_fee',
        '$miscellaneous_fee',
        '$total_fee',

        '$surcharge',
        '$current_surcharge',

        '$vat',
        '$vat_arrear',

        '$income_tax',
        '$income_tax_arrear',

        '$arrear',
        '$signboard_vat',

        '$valid'

        )";

        $conn->query($insert_sql);

        header("Location: license_list.php");
        exit;
    }
}
?>

<!DOCTYPE html>
<html lang="bn-BD">

<head>

<meta charset="utf-8">

<meta name="viewport"
      content="width=device-width, initial-scale=1">

<title>

<?= $editMode ? 'Edit License' : 'Add License'; ?>

</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
      rel="stylesheet">

<style>

body{
background:#f4f7fc;
}

.sidebar{
position:fixed;
left:0;
top:0;
width:250px;
height:100%;
background:#0f172a;
}

.sidebar a{
display:block;
padding:15px 20px;
color:#fff;
text-decoration:none;
}

.sidebar a:hover{
background:#2563eb;
}

.main-content{
margin-left:250px;
padding:20px;
}

.topbar{
background:#fff;
padding:15px;
border-radius:10px;
box-shadow:0 2px 8px rgba(0,0,0,.08);
margin-bottom:20px;
}

.card{
border:none;
border-radius:15px;
}

</style>

</head>

<body>

<div class="sidebar">

<h4 class="text-center text-white py-3">
LICENSE PANEL
</h4>

<a href="dashboard.php">🏠 Dashboard</a>
<a href="up.php">➕ Add License</a>
<a href="search.php">🔍 Search</a>
<a href="license_list.php">📋 All Licenses</a>
<a href="logout.php">🚪 Logout</a>

</div>

<div class="main-content">

<div class="topbar">

<h3>

<?= $editMode ? 'Edit License' : 'Add New License'; ?>

</h3>

</div>

<div class="card shadow">

<div class="card-body">

<form method="post">

<h4 class="mb-4">

<?= $editMode ? 'Update License Information'
              : 'Upload License Information'; ?>

</h4>

<!-- License Information -->

<div class="row">

<div class="col-md-4 mb-3">
<label>License ID</label>
<input type="text"
       name="lic_id"
       class="form-control"
       value="<?= $editMode ? htmlspecialchars($data['lic_id']) : '' ?>">
</div>

<div class="col-md-4 mb-3">
<label>Municipality</label>
<select name="zone_info_id" class="form-control">

<option value="">Select Municipality</option>

<?php
mysqli_data_seek($zone_info_result,0);

while($zone_info_row = $zone_info_result->fetch_assoc()){

$selected =
($editMode &&
$data['zone_info_id'] ==
$zone_info_row['zone_info_id'])
?
'selected'
:
'';

echo "<option value='".$zone_info_row['zone_info_id']."' $selected>".
$zone_info_row['municipality_name_english'].
"</option>";
}
?>

</select>
</div>

<div class="col-md-4 mb-3">
<label>License No</label>
<input type="text"
       name="license_no"
       class="form-control"
       value="<?= $editMode ? htmlspecialchars($data['license_no']) : '' ?>">
</div>

<div class="col-md-4 mb-3">
<label>License ID Number (-s-)</label>
<input type="text"
       name="license_id"
       class="form-control"
       value="<?= $editMode ? htmlspecialchars($data['license_id']) : '' ?>">
</div>

<div class="col-md-4 mb-3">
<label>Issue Date</label>
<input type="date"
       name="issue_date"
       class="form-control"
       value="<?= $editMode ? $data['issue_date'] : '' ?>">
</div>

<div class="col-md-4 mb-3">
<label>Renewal Year</label>
<input type="text"
       name="renewal_year"
       class="form-control"
       value="<?= $editMode ? htmlspecialchars($data['renewal_year']) : '' ?>">
</div>

<div class="col-md-4 mb-3">
<label>Renewal Date</label>
<input type="date"
       name="renewal_date"
       class="form-control"
       value="<?= $editMode ? $data['renewal_date'] : '' ?>">
</div>

<div class="col-md-4 mb-3">
<label>Expiry Date</label>
<input type="date"
       name="expiry_date"
       class="form-control"
       value="<?= $editMode ? $data['expiry_date'] : '' ?>">
</div>

</div>

<hr>

<h5>Client Information</h5>

<div class="row">

<?php

$fields = [

'organization_name_bengali'=>'Organization Name Bengali',
'organization_name_english'=>'Organization Name English',
'business_type_bengali'=>'Business Type Bengali',
'business_type_english'=>'Business Type English',
'ward_bengali'=>'Ward Bengali',
'ward_english'=>'Ward English',
'street_bengali'=>'Street Bengali',
'street_english'=>'Street English',
'address_bengali'=>'Address Bengali',
'present_address'=>'Present Address Bengali',
'parmanent_address'=>'Parmanent Address Bengali',
'address_english'=>'Address English',
'owner_name_bengali'=>'Owner Bengali',
'owner_name_english'=>'Owner English',
'father_name'=>'Father Name',
'mother_name'=>'Mother Name',
'spouse_name'=>'Spouse Name',
'nid'=>'NID',
'phone_number'=>'Phone Number'

];

foreach($fields as $name=>$label){
?>

<div class="col-md-4 mb-3">

<label><?= $label ?></label>

<input type="text"
       name="<?= $name ?>"
       class="form-control"
       value="<?= $editMode ? htmlspecialchars($data[$name]) : '' ?>">

</div>

<?php } ?>

</div>

<hr>

<h5>Fee Information</h5>

<div class="row">

<?php

$feeFields = [

'license_fee'=>'License Fee',
'signboard_fee'=>'Signboard Fee',
'miscellaneous_fee'=>'Miscellaneous Fee',
'total_fee'=>'Total Fee',
'surcharge'=>'Surcharge',
'current_surcharge'=>'Current Surcharge',
'vat'=>'VAT',
'vat_arrear'=>'VAT Arrear',
'income_tax'=>'Income Tax',
'income_tax_arrear'=>'Income Tax Arrear',
'arrear'=>'Arrear',
'signboard_vat'=>'Signboard VAT',
'valid'=>'Valid Till'

];

foreach($feeFields as $name=>$label){
?>

<div class="col-md-3 mb-3">

<label><?= $label ?></label>

<input type="number"
       step="0.01"
       name="<?= $name ?>"
       class="form-control"
       value="<?= $editMode ? $data[$name] : '' ?>">

</div>

<?php } ?>

</div>

<div class="mt-4">

<button type="submit"
        class="btn btn-primary">

<?= $editMode
? 'Update License'
: 'Save License'; ?>

</button>

<a href="license_list.php"
   class="btn btn-secondary">

Back

</a>

</div>

</form>

</div>
</div>

</div>

</body>
</html>