<?php
session_start();
$nama_pegawai = $_SESSION['nama_pegawai'];
include '../koneksi.php';

if (isset($_POST["tambah"])) {
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        include '../koneksi.php';

        $nama_customer = $_POST['nama_customer'];
        $bu = $_POST['bu'];
        $tt = $_POST['tt'];
        $pic = $tt . " " . $_POST['pic'];
        $telp = $_POST['telp'];
        $alamat = $_POST['alamat'];
        $email = $_POST['email'];
        $ketstatus = $_POST['ketstatus'];
        $nofile = $_POST['nofile'];

        date_default_timezone_set('Asia/Jakarta');
        $tgl = date("Y-m-d");
        $status_check = $_POST["check"];

        $nofile = $_GET['nofile'];
        $sql = mysqli_query($konek, "SELECT * FROM bisman_ps WHERE nofile_bismanps='$nofile'");
        $datar = mysqli_num_rows($sql);
        if ($datar == 0) {

            $sql = mysqli_query($konek, "SELECT * FROM customer WHERE nama_customer='$nama_customer' AND bu='$bu' AND alamat='$alamat'");
            $data = mysqli_num_rows($sql);

            if ($data == 0) {
                // kode id
                $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);

                $sqlmasuk = mysqli_query($konek, "INSERT INTO customer VALUES ('$idcustomer','$bu','$nama_customer','$alamat','','$telp','$email')");
                // echo "tada";
            } else {
                $crcust = mysqli_query($konek, "SELECT idcustomer FROM customer where nama_customer='$nama_customer'");
                $datac = mysqli_fetch_assoc($crcust);
                $idcustomer = $datac['idcustomer'];
                $sqledit = mysqli_query($konek, "UPDATE customer SET alamat='$alamat',email='$email' WHERE idcustomer='$idcustomer'");
                // echo "ada";
            }

            $sqlpic = mysqli_query($konek, "SELECT * FROM pic WHERE nama_pic='$pic' AND idcustomer='$idcustomer'");
            $datapic = mysqli_num_rows($sqlpic);

            if ($datapic == 0) {
                // kode id
                $query = mysqli_query($konek, "
                SELECT MAX(idpic) AS last_id FROM pic");
                $datapic = mysqli_fetch_assoc($query);

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

                $idpic = 'PIC' . str_pad($urutan, 5, '0', STR_PAD_LEFT);

                $sqlmasuk = mysqli_query($konek, "INSERT INTO pic VALUES ('$idpic','$idcustomer','$pic','','')");
                // echo "tada";
            } else {
                $crpic = mysqli_query($konek, "SELECT idpic FROM pic where nama_pic='$pic'");
                $datapic = mysqli_fetch_assoc($crpic);
                $idpic = $datapic['idpic'];
                // echo "ada";
            }

            // cari idPegawai
            $crpegawai = mysqli_query($konek, "SELECT idpegawai FROM pegawai where nama_pegawai='$nama_pegawai'");
            $datap = mysqli_fetch_assoc($crpegawai);
            $idpegawai = $datap["idpegawai"];

            // kode id
            $query = mysqli_query($konek, "
            SELECT MAX(idbismanps) AS last_id FROM bisman_ps");
            $data = mysqli_fetch_assoc($query);

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

            $idbismanps = 'PSR' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
            if ($status_check == 'Sudah') {
                $checker = $_POST['checker'];
                $tglcheck = $_POST['tgl_check'];
            } else {
                $tglcheck = $tgl;
                $checker = "-";
                // $ketstatus = "";
            }
            $sql = mysqli_query($konek, "INSERT INTO bisman_ps VALUES ('$idbismanps','$nofile','$idpegawai','$idcustomer','$idpic','$tgl','$status_check','$tglcheck','$checker','$ketstatus')");
        }
    }

    header("Location: ../penawaranbisman-tambah.php?nofile=$nofile&modal=ModalTmb");
    // data-bs-toggle="modal" data-bs-target="#ModalTmb"
}
if (isset($_POST["tmbunit"])) {
    include "../koneksi.php";
    $nofile = $_GET['nofile'];
    $unit = $_POST['unit'];
    $wo = $_POST['wo'];
    $nounit = $_POST['nounit'];

    // cek idbisman ps
    $sqlr = mysqli_query($konek, "SELECT idbismanps FROM bisman_ps WHERE nofile_bismanps='$nofile'");
    $datar = mysqli_fetch_assoc($sqlr);
    $idbismanps = $datar['idbismanps'];
    // cek unit
    $sql = mysqli_query($konek, "SELECT * FROM unit WHERE nama_unit='$unit'");
    $data = mysqli_num_rows($sql);

    if ($data == 0) {
        $query = mysqli_query($konek, "
            SELECT MAX(idunit) AS last_id FROM unit");
        $data = mysqli_fetch_assoc($query);

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

        $idunit = 'UNT' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
        $sqlmasuk = mysqli_query($konek, "INSERT INTO unit VALUES ('$idunit','$unit')");
        // echo "tada";
    } else {
        $crunit = mysqli_query($konek, "SELECT idunit FROM unit where nama_unit='$unit'");
        $datau = mysqli_fetch_assoc($crunit);
        $idunit = $datau['idunit'];
        // echo "ada";
    }

    // kode id
    $query = mysqli_query($konek, "
            SELECT MAX(iddetail_bismanps) AS last_id FROM detail_bismanps");
    $data = mysqli_fetch_assoc($query);

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

    $iddetail_bismanps = 'DPR' . str_pad($urutan, 5, '0', STR_PAD_LEFT);

    $sql = mysqli_query($konek, "INSERT INTO detail_bismanps VALUES ('$iddetail_bismanps','$idbismanps','$idunit','$nounit','$wo')");
    header("Location: ../penawaranbisman-tambah.php?nofile=$nofile");
}
if (isset($_POST["tmbser"])) {
    include "../koneksi.php";
    $nofile = $_GET['nofile'];
    $jservice = $_POST['jservice'];
    $hservice = str_replace('.', '', $_POST['hservice']);
    $qty = $_POST['qty'];
    $kete = $_POST['kete'];
    $iddetail_bismanps = $_GET['iddetail'];

    // cek idjenis
    $sqlr = mysqli_query($konek, "SELECT idjenis_service FROM jenis_service WHERE jenis_service='$jservice'");
    $datar = mysqli_num_rows($sqlr);
    if ($datar == 0) {
        $query = mysqli_query($konek, "
            SELECT MAX(idjenis_service) AS last_id FROM jenis_service");
        $data = mysqli_fetch_assoc($query);

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

        $idjenis_service = 'JSC' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
        $sqlmasuk = mysqli_query($konek, "INSERT INTO jenis_service VALUES ('$idjenis_service','$jservice','$hservice')");
    } else {
        $sqlr = mysqli_query($konek, "SELECT idjenis_service FROM jenis_service WHERE jenis_service='$jservice'");
        $datar = mysqli_fetch_assoc($sqlr);
        $idjenis_service = $datar['idjenis_service'];
    }

    // kode id
    $query = mysqli_query($konek, "
            SELECT MAX(idbismanps_service) AS last_id FROM uraianbismanps_service");
    $data = mysqli_fetch_assoc($query);

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

    $idbismanps_service = 'URS' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
    echo $total = $hservice * $qty;
    $sql = mysqli_query($konek, "INSERT INTO uraianbismanps_service VALUES ('$idbismanps_service','$iddetail_bismanps','$idjenis_service','$hservice','$qty','$total','$kete')");
    // echo $nofile;
    header("Location: ../penawaranbisman-tambah.php?nofile=$nofile");
}
if (isset($_POST["tmbbar"])) {
    include "../koneksi.php";
    $nofile = $_GET['nofile'];
    $barang = $_POST['barang'];
    $hbarang = str_replace('.', '', $_POST['hbarang']);
    $qty = $_POST['qtybarang'];
    $iddetail_bismanps = $_GET['iddetail'];

    $sqlr = mysqli_query($konek, "SELECT idbarang FROM barang WHERE nama_barang='$barang'");
    $datar = mysqli_num_rows($sqlr);
    if ($datar == 0) {
        $query = mysqli_query($konek, "
        SELECT MAX(idbarang) AS last_id FROM barang");
        $data = mysqli_fetch_assoc($query);

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

        $idbarang = 'BRG' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
        $sql = mysqli_query($konek, "INSERT INTO barang VALUES ('$idbarang','$barang','$hbarang')");
    } else {
        $sqlr = mysqli_query($konek, "SELECT idbarang FROM barang WHERE nama_barang='$barang'");
        $datar = mysqli_fetch_assoc($sqlr);
        $idbarang = $datar['idbarang'];
    }
    $query = mysqli_query($konek, "
        SELECT MAX(idbismanps_barang) AS last_id FROM uraianbismanps_barang");
    $data = mysqli_fetch_assoc($query);

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

    $idbismanps_barang = 'URB' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
    $total = $hbarang * $qty;
    $sql = mysqli_query($konek, "INSERT INTO uraianbismanps_barang VALUES ('$idbismanps_barang','$iddetail_bismanps','$idbarang','$qty','$hbarang','$total','')");
    // cek idjenis
    header("Location: ../penawaranbisman-tambah.php?nofile=$nofile");
}
if (isset($_GET["revisi"])) {
    include "../koneksi.php";
    $nofile = $_GET["nofile"];

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

    $lastId = $data['last_id'];

    if ($lastId) {
        $urutan = (int) substr($lastId, 3, 5);
    } else {
        $urutan = 0;
    }

    $urutan++;
    $idrevisi_bismanps = 'RRP' . str_pad($urutan, 5, '0', STR_PAD_LEFT);



    $ceknorev = mysqli_query($konek, "SELECT 
    IFNULL(MAX(
        CAST(SUBSTRING_INDEX(nofile_revisi_bismanps, '.', -1) AS UNSIGNED)
    ), 0) AS revisi_terakhir
    FROM revisi_bisman_ps
    WHERE nofile_revisi_bismanps LIKE '{$nofile}.%'");
    $data = mysqli_fetch_assoc($ceknorev);

    $angka = $data["revisi_terakhir"] + 1;

    echo $nofile_revisi = $nofile . "." . $angka;
    echo "</br>";

    $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_bisman_ps(idrevisi_bismanps,nofile_revisi_bismanps,idbismanps,idpegawai,idcustomer,idpic,tgl_bismanps,status_checkbismanps,tgl_checkbismanps,checker,keterangan_status)
                                                                SELECT '$idrevisi_bismanps','$nofile_revisi',idbismanps,idpegawai,idcustomer,idpic,tgl_bismanps,status_checkbismanps,tgl_checkbismanps,checker,keterangan_status FROM bisman_ps WHERE nofile_bismanps='$nofile'");

    $cariidrax = mysqli_query($konek, "SELECT * FROM bisman_ps WHERE nofile_bismanps='$nofile'");
    $idbismanps = mysqli_fetch_assoc($cariidrax);
    echo $idbismanps = $idbismanps["idbismanps"];

    //masuk detail
    $query = mysqli_query($konek, "
    SELECT MAX(iddetail_revisi_bismanps) AS last_id FROM revisi_detail_bismanps");
    $data = mysqli_fetch_assoc($query);

    $lastId = $data['last_id'];

    if ($lastId) {
        $urutan = (int) substr($lastId, 3, 5);
    } else {
        $urutan = 0;
    }
    $sqldetail = mysqli_query($konek, "SELECT * FROM detail_bismanps WHERE idbismanps = '$idbismanps'");
    while ($row = mysqli_fetch_assoc($sqldetail)) {

        echo "</br>";
        echo $urutan++;
        echo "</br>";
        echo $iddetail_revisi_bismanps = 'RRD' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
        // echo $idunit = $row['idunit'];
        $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_detail_bismanps values('$iddetail_revisi_bismanps','$idrevisi_bismanps','{$row['idunit']}','{$row['no_unit']}','{$row['wo_unit']}')");

        echo $iddetail_bismanps = $row["iddetail_bismanps"];
        echo "</br>";
        $sqldetailr = mysqli_query($konek, "SELECT * FROM uraianbismanps_barang WHERE iddetail_bismanps = '$iddetail_bismanps'");
        $query = mysqli_query($konek, "SELECT MAX(idbismanps_barang_revisi) AS last_id FROM revisi_uraianbismanps_barang");
        $data = mysqli_fetch_assoc($query);

        $lastId = $data['last_id'];

        if ($lastId) {
            $urutanb = (int) substr($lastId, 3, 5);
        } else {
            $urutanb = 0;
        }
        while ($rows = mysqli_fetch_assoc($sqldetailr)) {
            //masuk detail
            echo $urutanb++;
            echo "</br>";
            echo $idbismanps_barang_revisi = 'RRB' . str_pad($urutanb, 5, '0', STR_PAD_LEFT);
            $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_uraianbismanps_barang values('$idbismanps_barang_revisi','$iddetail_revisi_bismanps','{$rows['idbarang']}','{$rows['qty']}','{$rows['harga_barangbisman']}','{$rows['totalbarang_bisman']}','{$rows['keterangan']}')");
        }

        $sqldetailr = mysqli_query($konek, "SELECT * FROM uraianbismanps_service WHERE iddetail_bismanps = '$iddetail_bismanps'");
        $query = mysqli_query($konek, "SELECT MAX(idbismanps_service_revisi) AS last_id FROM revisi_uraianbismanps_service");
        $data = mysqli_fetch_assoc($query);

        $lastId = $data['last_id'];

        if ($lastId) {
            $urutanb = (int) substr($lastId, 3, 5);
        } else {
            $urutanb = 0;
        }
        while ($rows = mysqli_fetch_assoc($sqldetailr)) {
            //masuk detail
            echo $urutanb++;
            echo "</br>";
            echo $idbismanps_service_revisi = 'RRS' . str_pad($urutanb, 5, '0', STR_PAD_LEFT);
            $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_uraianbismanps_service values('$idbismanps_service_revisi','$iddetail_revisi_bismanps','{$rows['idjenis_service']}','{$rows['harga_servicebisman']}','{$rows['qty_service']}','{$rows['totalservice_bisman']}','{$rows['keterangan']}')");
        }
    }
    header("Location: ../penawaranbisman-revisi.php?nofile=$nofile_revisi");
}
if (isset($_GET["revisike"])) {
    include "../koneksi.php";
    $nofile = $_GET["nofile"];

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

    $lastId = $data['last_id'];

    if ($lastId) {
        $urutan = (int) substr($lastId, 3, 5);
    } else {
        $urutan = 0;
    }

    $urutan++;
    $idrevisi_bismanps = 'RRP' . str_pad($urutan, 5, '0', STR_PAD_LEFT);


    $base_nofile = preg_replace('/\.\d+$/', '', $nofile);
    $ceknorev = mysqli_query($konek, "SELECT 
    IFNULL(MAX(
        CAST(SUBSTRING_INDEX(nofile_revisi_bismanps, '.', -1) AS UNSIGNED)
    ), 0) AS revisi_terakhir
    FROM revisi_bisman_ps
    WHERE nofile_revisi_bismanps LIKE '$base_nofile.%';");
    $data = mysqli_fetch_assoc($ceknorev);

    $angka = $data["revisi_terakhir"] + 1;

    echo $nofile_revisi = $base_nofile . "." . $angka;
    echo "</br>";

    $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_bisman_ps(idrevisi_bismanps,nofile_revisi_bismanps,idbismanps,idpegawai,idcustomer,idpic,tgl_bismanps,status_checkbismanps,tgl_checkbismanps,checker,keterangan_status)
                                                                SELECT '$idrevisi_bismanps','$nofile_revisi',idbismanps,idpegawai,idcustomer,idpic,tgl_bismanps,status_checkbismanps,tgl_checkbismanps,checker,keterangan_status FROM revisi_bisman_ps WHERE nofile_revisi_bismanps='$nofile'");

    $cariidrax = mysqli_query($konek, "SELECT * FROM revisi_bisman_ps WHERE nofile_revisi_bismanps='$nofile'");
    $revisi_bisman_ps = mysqli_fetch_assoc($cariidrax);
    echo $revisi_bisman_ps = $revisi_bisman_ps["idrevisi_bismanps"];

    //masuk detail
    $query = mysqli_query($konek, "
    SELECT MAX(iddetail_revisi_bismanps) AS last_id FROM revisi_detail_bismanps");
    $data = mysqli_fetch_assoc($query);

    $lastId = $data['last_id'];

    if ($lastId) {
        $urutan = (int) substr($lastId, 3, 5);
    } else {
        $urutan = 0;
    }
    $sqldetail = mysqli_query($konek, "SELECT * FROM revisi_detail_bismanps WHERE idrevisi_bismanps = '$revisi_bisman_ps'");
    while ($row = mysqli_fetch_assoc($sqldetail)) {

        echo "</br>";
        $urutan++;
        echo "</br>";
        echo $iddetail_revisi_bismanpsnew = 'RRD' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
        echo "</br>";
        $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_detail_bismanps values('$iddetail_revisi_bismanpsnew','$idrevisi_bismanps','{$row['idunit']}','{$row['no_unit']}','{$row['wo_unit']}')");

        echo $iddetail_revisi_bismanps = $row["iddetail_revisi_bismanps"];
        echo "</br>";
        $sqldetailr = mysqli_query($konek, "SELECT * FROM revisi_uraianbismanps_barang WHERE iddetail_revisi_bismanps = '$iddetail_revisi_bismanps'");
        while ($rows = mysqli_fetch_assoc($sqldetailr)) {
            //masuk detail
            $query = mysqli_query($konek, "SELECT MAX(idbismanps_barang_revisi) AS last_id FROM revisi_uraianbismanps_barang");
            $data = mysqli_fetch_assoc($query);

            $lastId = $data['last_id'];

            if ($lastId) {
                $urutanb = (int) substr($lastId, 3, 5);
            } else {
                $urutanb = 0;
            }
            $urutanb++;
            echo "</br>";
            echo $idbismanps_barang_revisi = 'RRB' . str_pad($urutanb, 5, '0', STR_PAD_LEFT);
            $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_uraianbismanps_barang values('$idbismanps_barang_revisi','$iddetail_revisi_bismanpsnew','{$rows['idbarang']}','{$rows['qty']}','{$rows['harga_barangbisman']}','{$rows['totalbarang_bisman']}','{$rows['keterangan']}')");
        }

        $sqldetailr = mysqli_query($konek, "SELECT * FROM revisi_uraianbismanps_service WHERE iddetail_revisi_bismanps = '$iddetail_revisi_bismanps'");
        while ($rows = mysqli_fetch_assoc($sqldetailr)) {
            //masuk detail
            $query = mysqli_query($konek, "SELECT MAX(idbismanps_service_revisi) AS last_id FROM revisi_uraianbismanps_service");
            $data = mysqli_fetch_assoc($query);

            $lastId = $data['last_id'];

            if ($lastId) {
                $urutanb = (int) substr($lastId, 3, 5);
            } else {
                $urutanb = 0;
            }
            $urutanb++;
            echo "</br>";
            echo $idbismanps_service_revisi = 'RRS' . str_pad($urutanb, 5, '0', STR_PAD_LEFT);
            $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_uraianbismanps_service values('$idbismanps_service_revisi','$iddetail_revisi_bismanpsnew','{$rows['idjenis_service']}','{$rows['harga_servicebisman']}','{$rows['qty_service']}','{$rows['totalservice_bisman']}','{$rows['keterangan']}')");
        }
    }
    header("Location: ../penawaranbisman-revisi.php?nofile=$nofile_revisi");
}
if (isset($_POST["tmbunitedit"])) {
    include "../koneksi.php";
    $nofile = $_GET['nofile'];
    $unit = $_POST['unit'];
    $wo = $_POST['wo'];
    $nounit = $_POST['nounit'];
    $idbismanps = $_POST['idbismanps'];

    // cek unit
    $sql = mysqli_query($konek, "SELECT * FROM unit WHERE nama_unit='$unit'");
    $data = mysqli_num_rows($sql);

    if ($data == 0) {
        $query = mysqli_query($konek, "
            SELECT MAX(idunit) AS last_id FROM unit");
        $data = mysqli_fetch_assoc($query);

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

        $idunit = 'UNT' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
        $sqlmasuk = mysqli_query($konek, "INSERT INTO unit VALUES ('$idunit','$unit')");
        // echo "tada";
    } else {
        $crunit = mysqli_query($konek, "SELECT idunit FROM unit where nama_unit='$unit'");
        $datau = mysqli_fetch_assoc($crunit);
        $idunit = $datau['idunit'];
        // echo "ada";
    }

    // kode id
    $query = mysqli_query($konek, "
            SELECT MAX(iddetail_revisi_bismanps ) AS last_id FROM revisi_detail_bismanps");
    $data = mysqli_fetch_assoc($query);

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

    $iddetail_revisi_bismanps = 'RRD' . str_pad($urutan, 5, '0', STR_PAD_LEFT);

    $sql = mysqli_query($konek, "INSERT INTO revisi_detail_bismanps VALUES ('$iddetail_revisi_bismanps','$idbismanps','$idunit','$nounit','$wo')");
    header("Location: ../penawaranbisman-revisi.php?nofile=$nofile");
}
if (isset($_POST["edtunit"])) {
    $nofile_revisi = $_GET["nofile"];
    echo $iddetail_revisi_bismanps = $_POST["iddetail_revisi_bismanps"];
    echo "<br>";
    echo $nama_unit = $_POST["unit"];
    echo "<br>";
    echo $nounit = $_POST["nounit"];
    echo "<br>";
    echo $wounit = $_POST["wo"];
    echo "<br>";

    $cariunit = mysqli_query($konek, "SELECT * FROM unit WHERE nama_unit = '$nama_unit'");
    $dunit = mysqli_fetch_assoc($cariunit);

    echo $idunit = $dunit["idunit"];

    $editunit = mysqli_query($konek, "UPDATE revisi_detail_bismanps SET idunit='$idunit',no_unit='$nounit',wo_unit='$wounit' WHERE iddetail_revisi_bismanps='$iddetail_revisi_bismanps'");

    header("Location: ../penawaranbisman-revisi.php?nofile=$nofile_revisi");
}
if (isset($_POST['simpanrevisi'])) {
    include '../koneksi.php';

    echo $bu = $_POST['bu'];
    echo "<br>";
    echo $nama_customer = $_POST['nama_customer'];
    // echo $tt = $_POST['tt'];
    echo "<br>";
    echo $pic = $_POST['tt'] . " " . $_POST['pic'];
    // $pic = $tt ." ". $pice;
    echo "<br>";
    echo $telp = $_POST['telp'];
    echo "<br>";
    echo $alamat = $_POST['alamat'];
    echo "<br>";
    echo $email = $_POST['email'];
    echo "<br>";
    echo $nofile = $_POST['nofile'];
    echo "<br>";

    date_default_timezone_set('Asia/Jakarta');
    $tgl = date("Y-m-d");
    echo $status_check = $_POST["check"];
    echo "<br>";

    if ($status_check == 'Sudah') {
        echo $checker = $_POST['checker'];
        echo "<br>";
        echo $tglcheck = $_POST['tgl_check'];
    } else {
        echo $tglcheck = $tgl;
        echo "<br>";
        echo $checker = "-";
    }

    $caricust = mysqli_query($konek, "SELECT * FROM customer WHERE bu='$bu' AND nama_customer = '$nama_customer'");
    $datac = mysqli_fetch_assoc($caricust);

    echo $idcustomer = $datac["idcustomer"];

    $caripic = mysqli_query($konek, "SELECT * FROM pic WHERE nama_pic = '$pic' AND idcustomer = '$idcustomer'");
    $datapic = mysqli_fetch_assoc($caripic);

    echo $idpic = $datapic["idpic"];

    $updateraxrev = mysqli_query($konek, "UPDATE revisi_bisman_ps SET idcustomer='$idcustomer',tgl_bismanps='$tgl',idpic='$idpic',status_checkbismanps='$status_check',tgl_checkbismanps='$tglcheck',checker='$checker' WHERE nofile_revisi_bismanps = '$nofile'");

    header("Location: ../penawaran-servicebisman.php");
}
if (isset($_POST["edtbarang"])) {
    include "../koneksi.php";
    echo $ide = $_POST["idrbr"];
    echo "<br>";
    echo $nama_barang = $_POST["barang"];
    echo "<br>";
    echo $hargab = str_replace('.', '', $_POST["hbarang"]);
    echo "<br>";
    echo $qty = $_POST["qtybarang"];
    echo "<br>";
    echo $kete = $_POST["kete"];
    echo "<br>";

    $namabarang = mysqli_query($konek, "SELECT * FROM barang WHERE nama_barang = '$nama_barang'");
    $idbarang = mysqli_fetch_assoc($namabarang);
    echo $idbarange = $idbarang["idbarang"];

    $total = $qty * $hargab;
    $nofile = $_GET["nofile"];

    $updateurbar = mysqli_query($konek, "UPDATE revisi_uraianbismanps_barang SET idbarang ='$idbarange',qty='$qty',harga_barangbisman='$hargab',totalbarang_bisman='$total',keterangan='$kete' WHERE idbismanps_barang_revisi='$ide'");
    header("Location: ../penawaranbisman-revisi.php?nofile=$nofile");
}
if (isset($_POST["edtbarangtmbae"])) {
    include "../koneksi.php";
    echo $ide = $_POST["idrbr"];
    echo "<br>";
    echo $nama_barang = $_POST["barang"];
    echo "<br>";
    echo $hargab = str_replace('.', '', $_POST["hbarang"]);
    echo "<br>";
    echo $qty = $_POST["qtybarang"];
    echo "<br>";
    echo $kete = $_POST["kete"];
    echo "<br>";

    $namabarang = mysqli_query($konek, "SELECT * FROM barang WHERE nama_barang = '$nama_barang'");
    $idbarang = mysqli_fetch_assoc($namabarang);
    echo $idbarange = $idbarang["idbarang"];

    $total = $qty * $hargab;
    $nofile = $_GET["nofile"];

    $updateurbar = mysqli_query($konek, "UPDATE uraianbismanps_barang SET idbarang ='$idbarange',qty='$qty',harga_barangbisman='$hargab',totalbarang_bisman='$total',keterangan='$kete' WHERE idbismanps_barang='$ide'");
    header("Location: ../penawaranbisman-tambah.php?nofile=$nofile");
}
if (isset($_POST["edtservice"])) {
    include "../koneksi.php";
    echo $ide = $_POST["idrsr"];
    echo "<br>";
    echo $jservice = $_POST["jservice"];
    echo "<br>";
    echo $hservice = str_replace('.', '', $_POST["hservice"]);
    echo "<br>";
    echo $qty = $_POST["qty"];
    echo "<br>";
    echo $kete = $_POST["kete"];
    echo "<br>";

    $namaservice = mysqli_query($konek, "SELECT * FROM jenis_service WHERE jenis_service = '$jservice'");
    $idjenis_service = mysqli_fetch_assoc($namaservice);
    echo $idjenis_service = $idjenis_service["idjenis_service"];

    $total = $qty * $hservice;
    $nofile = $_GET["nofile"];

    $updateurbar = mysqli_query($konek, "UPDATE revisi_uraianbismanps_service SET idjenis_service ='$idjenis_service',harga_servicebisman='$hservice',qty_service='$qty',totalservice_bisman='$total',keterangan='$kete' WHERE idbismanps_service_revisi='$ide'");
    header("Location: ../penawaranbisman-revisi.php?nofile=$nofile");
}
if (isset($_POST["edtservicetmbae"])) {
    include "../koneksi.php";
    echo $ide = $_POST["idrsr"];
    echo "<br>";
    echo $jservice = $_POST["jservice"];
    echo "<br>";
    echo $hservice = str_replace('.', '', $_POST["hservice"]);
    echo "<br>";
    echo $qty = $_POST["qty"];
    echo "<br>";
    echo $kete = $_POST["kete"];
    echo "<br>";

    $namaservice = mysqli_query($konek, "SELECT * FROM jenis_service WHERE jenis_service = '$jservice'");
    $idjenis_service = mysqli_fetch_assoc($namaservice);
    echo $idjenis_service = $idjenis_service["idjenis_service"];

    $total = $qty * $hservice;
    $nofile = $_GET["nofile"];

    $updateurbar = mysqli_query($konek, "UPDATE uraianbismanps_service SET idjenis_service ='$idjenis_service',harga_servicebisman='$hservice',qty_service='$qty',totalservice_bisman='$total',keterangan='$kete' WHERE idbismanps_service='$ide'");
    header("Location: ../penawaranbisman-tambah.php?nofile=$nofile");
}
if (isset($_GET["hpsser"])) {
    $nofile = $_GET["nofile"];
    $iddetail_revisi_bismanps = $_GET["iddetail"];

    $sqlservice = mysqli_query($konek, "SELECT * FROM revisi_uraianbismanps_service WHERE iddetail_revisi_bismanps = '$iddetail_revisi_bismanps'");
    $iddetail_revisi_bismanps = mysqli_fetch_assoc($sqlservice);
    $idbismanps_service_revisi = $iddetail_revisi_bismanps["idbismanps_service_revisi"];

    $hps = mysqli_query($konek, "DELETE FROM revisi_uraianbismanps_service WHERE idbismanps_service_revisi='$idbismanps_service_revisi'");
    header("Location: ../penawaranbisman-revisi.php?nofile=$nofile");
}
if (isset($_GET["hpsbar"])) {

    $nofile = $_GET["nofile"];
    $iddetail_revisi_bismanps = $_GET["iddetail"];

    $sqlbarang = mysqli_query($konek, "SELECT * FROM revisi_uraianbismanps_barang WHERE iddetail_revisi_bismanps = '$iddetail_revisi_bismanps'");
    $iddetail_revisi_bismanps = mysqli_fetch_assoc($sqlbarang);
    $idbismanps_barang_revisi = $iddetail_revisi_bismanps["idbismanps_barang_revisi"];

    $hps = mysqli_query($konek, "DELETE FROM revisi_uraianbismanps_barang WHERE idbismanps_barang_revisi='$idbismanps_barang_revisi'");
    header("Location: ../penawaranbisman-revisi.php?nofile=$nofile");
}
if (isset($_POST["tmbrbar"])) {
    include "../koneksi.php";
    echo $nofile = $_GET['nofile'];
    echo "<br>";
    echo $barang = $_POST['barang'];
    echo "<br>";
    echo $hbarang = str_replace('.', '', $_POST['hbarang']);
    echo "<br>";
    echo $qty = $_POST['qtybarang'];
    echo "<br>";
    echo $kete = $_POST['kete'];
    echo "<br>";
    echo $iddetail_revisi_bismanps = $_GET['iddetail'];

    $sqlr = mysqli_query($konek, "SELECT idbarang FROM barang WHERE nama_barang='$barang'");
    $datar = mysqli_num_rows($sqlr);
    if ($datar == 0) {
        $query = mysqli_query($konek, "
        SELECT MAX(idbarang) AS last_id FROM barang");
        $data = mysqli_fetch_assoc($query);

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

        $idbarang = 'BRG' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
        $sql = mysqli_query($konek, "INSERT INTO barang VALUES ('$idbarang','$barang','$hbarang')");
    } else {
        $sqlr = mysqli_query($konek, "SELECT idbarang FROM barang WHERE nama_barang='$barang'");
        $datar = mysqli_fetch_assoc($sqlr);
        $idbarang = $datar['idbarang'];
    }
    $query = mysqli_query($konek, "
        SELECT MAX(idbismanps_barang_revisi) AS last_id FROM revisi_uraianbismanps_barang");
    $data = mysqli_fetch_assoc($query);

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

    $idbismanps_barang = 'RUB' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
    $total = $hbarang * $qty;
    $sql = mysqli_query($konek, "INSERT INTO revisi_uraianbismanps_barang VALUES ('$idbismanps_barang','$iddetail_revisi_bismanps','$idbarang','$qty','$hbarang','$total','$kete')");
    // cek idjenis
    header("Location: ../penawaranbisman-revisi.php?nofile=$nofile");
}
if (isset($_POST["tmbrser"])) {
    include "../koneksi.php";
    echo $nofile = $_GET['nofile'];
    $jservice = $_POST['jservice'];
    $hservice = str_replace('.', '', $_POST['hservice']);
    $qty = $_POST['qty'];
    $kete = $_POST['kete'];
    $iddetail_bismanps = $_GET['iddetail'];

    // cek idjenis
    $sqlr = mysqli_query($konek, "SELECT idjenis_service FROM jenis_service WHERE jenis_service='$jservice'");
    $datar = mysqli_num_rows($sqlr);
    if ($datar == 0) {
        $query = mysqli_query($konek, "
            SELECT MAX(idjenis_service) AS last_id FROM jenis_service");
        $data = mysqli_fetch_assoc($query);

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

        $idjenis_service = 'JSC' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
        $sqlmasuk = mysqli_query($konek, "INSERT INTO jenis_service VALUES ('$idjenis_service','$jservice','$hservice')");
    } else {
        $sqlr = mysqli_query($konek, "SELECT idjenis_service FROM jenis_service WHERE jenis_service='$jservice'");
        $datar = mysqli_fetch_assoc($sqlr);
        $idjenis_service = $datar['idjenis_service'];
    }

    // kode id
    $query = mysqli_query($konek, "
            SELECT MAX(idbismanps_service_revisi) AS last_id FROM revisi_uraianbismanps_service");
    $data = mysqli_fetch_assoc($query);

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

    $idbismanps_service = 'RUS' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
    $total = $hservice * $qty;
    $sql = mysqli_query($konek, "INSERT INTO revisi_uraianbismanps_service VALUES ('$idbismanps_service','$iddetail_bismanps','$idjenis_service','$hservice','$qty','$total','$kete')");
    // echo $nofile;
    header("Location: ../penawaranbisman-revisi.php?nofile=$nofile");
}