<?php
session_start();

include '../koneksi.php';
if (isset($_POST["edit"])) {
    $idcustomer = $_POST["idcustomer"];
    $bu = $_POST["bu"];
    $nama_customer = $_POST["nama_customer"];
    $alamat = $_POST["alamat"];
    $kota = $_POST["kota"];
    // $pic =$_POST['tt']." ".$_POST["pic"];
    $telp = $_POST["telp"];
    $telp = str_replace('-', '', $telp);
    $email = $_POST["email"];
    $tglinput = $_POST["tglinput"];

    $sql = mysqli_query($konek,"UPDATE customer SET bu='$bu',nama_customer='$nama_customer',alamat='$alamat',kota='$kota',telp='$telp',email='$email',tglinput='$tglinput' WHERE idcustomer='$idcustomer'");
    // $sql = mysqli_query($konek,"UPDATE customer SET tglinput='$tglinput' WHERE idcustomer='$idcustomer'");
    header("Location: ../customer.php");
}
if (isset($_POST["editb"])) {
    $idcustomer = $_POST["idcustomer"];
    $bu = $_POST["bu"];
    $nama_customer = $_POST["nama_customer"];
    $alamat = $_POST["alamat"];
    $kota = $_POST["kota"];
    // $pic =$_POST['tt']." ".$_POST["pic"];
    $telp = $_POST["telp"];
    $telp = str_replace('-', '', $telp);
    $email = $_POST["email"];
    $tglinput = $_POST["tglinput"];

    $sql = mysqli_query($konek,"UPDATE customeb SET bu='$bu',nama_customer='$nama_customer',alamat='$alamat',kota='$kota',telp='$telp',email='$email',tglinput='$tglinput' WHERE idcustomer='$idcustomer'");
    // $sql = mysqli_query($konek,"UPDATE customeb SET tglinput='$tglinput' WHERE idcustomer='$idcustomer'");
    header("Location: ../customeb.php");
}
if (isset($_POST["editDTELP"])) {
    $idtelp_custraxi = $_POST["idtelp_custraxi"];
    $idcustomer = $_POST["idcustomer"];
    $bu = $_POST["bu"];
    echo $nama_customer = $_POST["nama_customer"];
    $alamat = $_POST["alamat"];
    $kota = $_POST["kota"];
    // $pic =$_POST['tt']." ".$_POST["pic"];
    $telp = $_POST["telp"];
    $telp = str_replace('-', '', $telp);
    $email = $_POST["email"];
    $tglinput = $_POST["tglinput"];

    $sql = mysqli_query($konek,"UPDATE customer SET bu='$bu',nama_customer='$nama_customer',alamat='$alamat',kota='$kota',telp='$telp',email='$email',tglinput='$tglinput' WHERE idcustomer='$idcustomer'");
    header("Location: ../telp_custraxiedit.php?idtelp_custraxi=$idtelp_custraxi&nama_customer=$nama_customer");
}
if (isset($_POST["tambahcustomer"])) {
    $bu = $_POST["bu"];
    $nama_customer = $_POST["nama_customer"];
    $alamat = $_POST["alamat"];
    $kota = $_POST["kota"];
    $telp = $_POST["telp"];
    $email = $_POST["email"];

    $query = mysqli_query($konek, "
        SELECT MAX(idcustomer) AS last_id FROM customer");
    $data = mysqli_fetch_assoc($query);

    $lastId = $data['last_id'];
    $urutan = (int) substr($lastId, 3, 5);
    $urutan++;

    $idcustomer = 'CUS' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
    $tglinputcustomer = date('Y-m-d');

    $sqlmasuk = mysqli_query($konek, "INSERT INTO customer VALUES ('$idcustomer','$bu','$nama_customer','$alamat','$kota','$telp','$email','$tglinputcustomer')");
    header("Location: ../customer.php");
}
if (isset($_POST["tambahcustomeb"])) {
    $bu = $_POST["bu"];
    $nama_customer = $_POST["nama_customer"];
    $alamat = $_POST["alamat"];
    $kota = $_POST["kota"];
    $telp = $_POST["telp"];
    $email = $_POST["email"];

    $query = mysqli_query($konek, "
        SELECT MAX(idcustomer) AS last_id FROM customeb");
    $data = mysqli_fetch_assoc($query);

    $lastId = $data['last_id'];
    $urutan = (int) substr($lastId, 3, 5);
    $urutan++;

    $idcustomer = 'CUS' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
    $tglinputcustomer = date('Y-m-d');

    $sqlmasuk = mysqli_query($konek, "INSERT INTO customeb VALUES ('$idcustomer','$bu','$nama_customer','$alamat','$kota','$telp','$email','$tglinputcustomer')");
    header("Location: ../customeb.php");
}
?>