<?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 raxi_ps WHERE nofile_raxips='$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(idraxips) AS last_id FROM raxi_ps");
            $data = mysqli_fetch_assoc($query);

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

            $idraxips = '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 raxi_ps VALUES ('$idraxips','$nofile','$idpegawai','$idcustomer','$idpic','$tgl','$status_check','$tglcheck','$checker','$ketstatus')");
        }
    }

    header("Location: ../penawaranraxi-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 idraxi ps
    $sqlr = mysqli_query($konek, "SELECT idraxips FROM raxi_ps WHERE nofile_raxips='$nofile'");
    $datar = mysqli_fetch_assoc($sqlr);
    $idraxips = $datar['idraxips'];
    // 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_raxips) AS last_id FROM detail_raxips");
    $data = mysqli_fetch_assoc($query);

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

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

    $sql = mysqli_query($konek, "INSERT INTO detail_raxips VALUES ('$iddetail_raxips','$idraxips','$idunit','$nounit','$wo')");
    header("Location: ../penawaranraxi-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_raxips = $_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(idraxips_service) AS last_id FROM uraianraxips_service");
    $data = mysqli_fetch_assoc($query);

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

    $idraxips_service = 'URS' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
    echo $total = $hservice * $qty;
    $sql = mysqli_query($konek, "INSERT INTO uraianraxips_service VALUES ('$idraxips_service','$iddetail_raxips','$idjenis_service','$hservice','$qty','$total','$kete')");
    // echo $nofile;
    header("Location: ../penawaranraxi-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_raxips = $_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(idraxips_barang) AS last_id FROM uraianraxips_barang");
    $data = mysqli_fetch_assoc($query);

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

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

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

    $lastId = $data['last_id'];

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

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



    $ceknorev = mysqli_query($konek, "SELECT 
    IFNULL(MAX(
        CAST(SUBSTRING_INDEX(nofile_revisi_raxips, '.', -1) AS UNSIGNED)
    ), 0) AS revisi_terakhir
    FROM revisi_raxi_ps
    WHERE nofile_revisi_raxips 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_raxi_ps(idrevisi_raxips,nofile_revisi_raxips,idraxips,idpegawai,idcustomer,idpic,tgl_raxips,status_checkraxips,tgl_checkraxips,checker,keterangan_status)
                                                                SELECT '$idrevisi_raxips','$nofile_revisi',idraxips,idpegawai,idcustomer,idpic,tgl_raxips,status_checkraxips,tgl_checkraxips,checker,keterangan_status FROM raxi_ps WHERE nofile_raxips='$nofile'");

    $cariidrax = mysqli_query($konek, "SELECT * FROM raxi_ps WHERE nofile_raxips='$nofile'");
    $idraxips = mysqli_fetch_assoc($cariidrax);
    echo $idraxips = $idraxips["idraxips"];

    //masuk detail
    $query = mysqli_query($konek, "
    SELECT MAX(iddetail_revisi_raxips) AS last_id FROM revisi_detail_raxips");
    $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_raxips WHERE idraxips = '$idraxips'");
    while ($row = mysqli_fetch_assoc($sqldetail)) {

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

        echo $iddetail_raxips = $row["iddetail_raxips"];
        echo "</br>";
        $sqldetailr = mysqli_query($konek, "SELECT * FROM uraianraxips_barang WHERE iddetail_raxips = '$iddetail_raxips'");
        $query = mysqli_query($konek, "SELECT MAX(idraxips_barang_revisi) AS last_id FROM revisi_uraianraxips_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 $idraxips_barang_revisi = 'RRB' . str_pad($urutanb, 5, '0', STR_PAD_LEFT);
            $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_uraianraxips_barang values('$idraxips_barang_revisi','$iddetail_revisi_raxips','{$rows['idbarang']}','{$rows['qty']}','{$rows['harga_barangraxi']}','{$rows['totalbarang_raxi']}','{$rows['keterangan']}')");
        }

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

        $lastId = $data['last_id'];

        if ($lastId) {
            $urutane = (int) substr($lastId, 3, 5);
        } else {
            $urutane = 0;
        }
        $sqldetailr = mysqli_query($konek, "SELECT * FROM uraianraxips_service WHERE iddetail_raxips = '$iddetail_raxips'");
        while ($rows = mysqli_fetch_assoc($sqldetailr)) {
            //masuk detail

            echo $urutane++;
            echo "</br>";
            echo $idraxips_service_revisi = 'RRS' . str_pad($urutane, 5, '0', STR_PAD_LEFT);
            $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_uraianraxips_service values('$idraxips_service_revisi','$iddetail_revisi_raxips','{$rows['idjenis_service']}','{$rows['harga_serviceraxi']}','{$rows['qty_service']}','{$rows['totalservice_raxi']}','{$rows['keterangan']}')");
        }
    }
    header("Location: ../penawaranraxi-revisi.php?nofile=$nofile_revisi");
}
if (isset($_GET["revisike"])) {
    include "../koneksi.php";
    $nofile = $_GET["nofile"];

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

    $lastId = $data['last_id'];

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

    $urutan++;
    $idrevisi_raxips = '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_raxips, '.', -1) AS UNSIGNED)
    ), 0) AS revisi_terakhir
    FROM revisi_raxi_ps
    WHERE nofile_revisi_raxips 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_raxi_ps(idrevisi_raxips,nofile_revisi_raxips,idraxips,idpegawai,idcustomer,idpic,tgl_raxips,status_checkraxips,tgl_checkraxips,checker,keterangan_status)
                                                                SELECT '$idrevisi_raxips','$nofile_revisi',idraxips,idpegawai,idcustomer,idpic,tgl_raxips,status_checkraxips,tgl_checkraxips,checker,keterangan_status FROM revisi_raxi_ps WHERE nofile_revisi_raxips='$nofile'");

    $cariidrax = mysqli_query($konek, "SELECT * FROM revisi_raxi_ps WHERE nofile_revisi_raxips='$nofile'");
    $revisi_raxi_ps = mysqli_fetch_assoc($cariidrax);
    echo $revisi_raxi_ps = $revisi_raxi_ps["idrevisi_raxips"];

    //masuk detail
    $query = mysqli_query($konek, "
    SELECT MAX(iddetail_revisi_raxips) AS last_id FROM revisi_detail_raxips");
    $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_raxips WHERE idrevisi_raxips = '$revisi_raxi_ps'");
    while ($row = mysqli_fetch_assoc($sqldetail)) {

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

        echo $iddetail_revisi_raxips = $row["iddetail_revisi_raxips"];
        echo "</br>";
        $sqldetailr = mysqli_query($konek, "SELECT * FROM revisi_uraianraxips_barang WHERE iddetail_revisi_raxips = '$iddetail_revisi_raxips'");
        while ($rows = mysqli_fetch_assoc($sqldetailr)) {
            //masuk detail
            $query = mysqli_query($konek, "SELECT MAX(idraxips_barang_revisi) AS last_id FROM revisi_uraianraxips_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 $idraxips_barang_revisi = 'RRB' . str_pad($urutanb, 5, '0', STR_PAD_LEFT);
            $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_uraianraxips_barang values('$idraxips_barang_revisi','$iddetail_revisi_raxipsnew','{$rows['idbarang']}','{$rows['qty']}','{$rows['harga_barangraxi']}','{$rows['totalbarang_raxi']}','{$rows['keterangan']}')");
        }

        $sqldetailr = mysqli_query($konek, "SELECT * FROM revisi_uraianraxips_service WHERE iddetail_revisi_raxips = '$iddetail_revisi_raxips'");
        while ($rows = mysqli_fetch_assoc($sqldetailr)) {
            //masuk detail
            $query = mysqli_query($konek, "SELECT MAX(idraxips_service_revisi) AS last_id FROM revisi_uraianraxips_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 $idraxips_service_revisi = 'RRS' . str_pad($urutanb, 5, '0', STR_PAD_LEFT);
            $masukrevisi = mysqli_query($konek, "INSERT INTO revisi_uraianraxips_service values('$idraxips_service_revisi','$iddetail_revisi_raxipsnew','{$rows['idjenis_service']}','{$rows['harga_serviceraxi']}','{$rows['qty_service']}','{$rows['totalservice_raxi']}','{$rows['keterangan']}')");
        }
    }
    header("Location: ../penawaranraxi-revisi.php?nofile=$nofile_revisi");
}
if (isset($_POST["tmbunitedit"])) {
    include "../koneksi.php";
    $nofile = $_GET['nofile'];
    $unit = $_POST['unit'];
    $wo = $_POST['wo'];
    $nounit = $_POST['nounit'];
    $idraxips = $_POST['idraxips'];

    // 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_raxips ) AS last_id FROM revisi_detail_raxips");
    $data = mysqli_fetch_assoc($query);

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

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

    $sql = mysqli_query($konek, "INSERT INTO revisi_detail_raxips VALUES ('$iddetail_revisi_raxips','$idraxips','$idunit','$nounit','$wo')");
    header("Location: ../penawaranraxi-revisi.php?nofile=$nofile");
}
if (isset($_POST["edtunit"])) {
    $nofile_revisi = $_GET["nofile"];
    echo $iddetail_revisi_raxips = $_POST["iddetail_revisi_raxips"];
    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_raxips SET idunit='$idunit',no_unit='$nounit',wo_unit='$wounit' WHERE iddetail_revisi_raxips='$iddetail_revisi_raxips'");

    header("Location: ../penawaranraxi-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 '<br>';
    echo $idcustomer = $datac["idcustomer"];

    $caripic = mysqli_query($konek, "SELECT * FROM pic WHERE nama_pic = '$pic' AND idcustomer = '$idcustomer'");
    $datapic = mysqli_fetch_assoc($caripic);
    echo '<br>';
    echo $idpic = $datapic["idpic"];

    // $updateraxrev = mysqli_query($konek, "UPDATE revisi_raxi_ps SET idcustomer='$idcustomer',tgl_raxips='$tgl',idpic='$idpic',status_checkraxips='$status_check',tgl_checkraxips='$tglcheck',checker='$checker' WHERE nofile_revisi_raxips = '$nofile'");

    // header("Location: ../penawaran-serviceraxi.php");
}
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 uraianraxips_barang SET idbarang ='$idbarange',qty='$qty',harga_barangraxi='$hargab',totalbarang_raxi='$total',keterangan='$kete' WHERE idraxips_barang='$ide'");
    header("Location: ../penawaranraxi-tambah.php?nofile=$nofile");
}
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_uraianraxips_barang SET idbarang ='$idbarange',qty='$qty',harga_barangraxi='$hargab',totalbarang_raxi='$total',keterangan='$kete' WHERE idraxips_barang_revisi='$ide'");
    header("Location: ../penawaranraxi-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 uraianraxips_service SET idjenis_service ='$idjenis_service',harga_serviceraxi='$hservice',qty_service='$qty',totalservice_raxi='$total',keterangan='$kete' WHERE idraxips_service='$ide'");
    header("Location: ../penawaranraxi-tambah.php?nofile=$nofile");
}
if (isset($_POST["edtservicetmb"])) {
    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 uraianraxips_service SET idjenis_service ='$idjenis_service',harga_serviceraxi='$hservice',qty_service='$qty',totalservice_raxi='$total',keterangan='$kete' WHERE idraxips_service_revisi='$ide'");
    header("Location: ../penawaranraxi-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_uraianraxips_service SET idjenis_service ='$idjenis_service',harga_serviceraxi='$hservice',qty_service='$qty',totalservice_raxi='$total',keterangan='$kete' WHERE idraxips_service_revisi='$ide'");
    header("Location: ../penawaranraxi-revisi.php?nofile=$nofile");
}
if (isset($_GET["hpssere"])) {
    $nofile = $_GET["nofile"];
    $ide = $_GET["iddetail"];

    $hps = mysqli_query($konek, "DELETE FROM uraianraxips_service WHERE idraxips_service='$ide'");
    header("Location: ../penawaranraxi-tambah.php?nofile=$nofile");
}
if (isset($_GET["hpsser"])) {
    $nofile = $_GET["nofile"];
    $iddetail_revisi_raxips = $_GET["iddetail"];

    $sqlservice = mysqli_query($konek, "SELECT * FROM revisi_uraianraxips_service WHERE iddetail_revisi_raxips = '$iddetail_revisi_raxips'");
    $iddetail_revisi_raxips = mysqli_fetch_assoc($sqlservice);
    $idraxips_service_revisi = $iddetail_revisi_raxips["idraxips_service_revisi"];

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

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

    $sqlbarang = mysqli_query($konek, "SELECT * FROM revisi_uraianraxips_barang WHERE iddetail_revisi_raxips = '$iddetail_revisi_raxips'");
    $iddetail_revisi_raxips = mysqli_fetch_assoc($sqlbarang);
    $idraxips_barang_revisi = $iddetail_revisi_raxips["idraxips_barang_revisi"];

    $hps = mysqli_query($konek, "DELETE FROM revisi_uraianraxips_barang WHERE idraxips_barang_revisi='$idraxips_barang_revisi'");
    header("Location: ../penawaranraxi-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_raxips = $_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(idraxips_barang_revisi) AS last_id FROM revisi_uraianraxips_barang");
    $data = mysqli_fetch_assoc($query);

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

    $idraxips_barang = 'RUB' . str_pad($urutan, 5, '0', STR_PAD_LEFT);
    $total = $hbarang * $qty;
    $sql = mysqli_query($konek, "INSERT INTO revisi_uraianraxips_barang VALUES ('$idraxips_barang','$iddetail_revisi_raxips','$idbarang','$qty','$hbarang','$total','$kete')");
    // cek idjenis
    header("Location: ../penawaranraxi-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_raxips = $_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(idraxips_service_revisi) AS last_id FROM revisi_uraianraxips_service");
    $data = mysqli_fetch_assoc($query);

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

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