<?php
require_once __DIR__.'/../includes/db.php';
require_once __DIR__.'/../includes/auth.php';
include __DIR__.'/../includes/header.php';

$id = (int)$_GET['id'];

$q = $mysqli->query("
  SELECT * FROM pengajuan_surat
  WHERE id=$id AND jenis_surat='usaha'
");
$p = $q->fetch_assoc();
$data = json_decode($p['data_tambahan'],true);

$nomor = "510.1/".rand(100,999)."/DS-MAMBU/".date('Y');

$stmt = $mysqli->prepare("
  INSERT INTO surat_usaha
  (nomor_surat,warga_id,bidang_usaha,alamat_usaha,tahun_berdiri,tanggal_surat)
  VALUES (?,?,?,?,?,CURDATE())
");

$stmt->bind_param(
  'sisss',
  $nomor,
  $p['warga_id'],
  $data['bidang_usaha'],
  $data['alamat_usaha'],
  $data['tahun_berdiri']
);

$stmt->execute();

$mysqli->query("
  UPDATE pengajuan_surat
  SET status='approved', admin_id=".$_SESSION['admin_id']."
  WHERE id=$id
");

header("Location: pengajuan_surat.php");
