-- Tabel Template Sampling
CREATE TABLE sampling_templates (
    id INT PRIMARY KEY AUTO_INCREMENT,
    template_name VARCHAR(100),
    description TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Tabel Parameter Analisis
CREATE TABLE analysis_parameters (
    id INT PRIMARY KEY AUTO_INCREMENT,
    parameter_code VARCHAR(20),
    parameter_name VARCHAR(100),
    unit VARCHAR(20),
    price DECIMAL(10,2),
    method VARCHAR(100),
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Tabel Permintaan Layanan
CREATE TABLE service_requests (
    id INT PRIMARY KEY AUTO_INCREMENT,
    request_code VARCHAR(20) UNIQUE,
    customer_id INT,
    request_type ENUM('sampling_analysis', 'analysis_only'),
    sampling_date DATE,
    sampling_location TEXT,
    status ENUM('draft', 'submitted', 'approved', 'rejected', 'in_progress', 'completed'),
    total_amount DECIMAL(10,2),
    dp_amount DECIMAL(10,2),
    dp_status ENUM('unpaid', 'paid') DEFAULT 'unpaid',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

-- Tabel Detail Parameter yang Diminta
CREATE TABLE request_parameters (
    id INT PRIMARY KEY AUTO_INCREMENT,
    request_id INT,
    parameter_id INT,
    price DECIMAL(10,2),
    notes TEXT,
    FOREIGN KEY (request_id) REFERENCES service_requests(id),
    FOREIGN KEY (parameter_id) REFERENCES analysis_parameters(id)
);

-- Tabel Quotation
CREATE TABLE quotations (
    id INT PRIMARY KEY AUTO_INCREMENT,
    quotation_code VARCHAR(20) UNIQUE,
    request_id INT,
    issue_date DATE,
    valid_until DATE,
    status ENUM('draft', 'sent', 'approved', 'rejected'),
    total_amount DECIMAL(10,2),
    notes TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (request_id) REFERENCES service_requests(id)
);

-- Contoh data template sampling
INSERT INTO sampling_templates (template_name, description) VALUES
('Air Limbah Industri', 'Template untuk sampling air limbah industri'),
('Kualitas Udara Ambien', 'Template untuk sampling kualitas udara'),
('Air Permukaan', 'Template untuk sampling air sungai/danau');

-- Contoh data parameter analisis
INSERT INTO analysis_parameters (parameter_code, parameter_name, unit, price, method) VALUES
('BOD5', 'Biochemical Oxygen Demand', 'mg/L', 250000, 'SNI 6989.72:2009'),
('COD', 'Chemical Oxygen Demand', 'mg/L', 200000, 'SNI 6989.2:2009'),
('TSS', 'Total Suspended Solid', 'mg/L', 150000, 'SNI 06-6989.3-2004'),
('PH', 'Derajat Keasaman', '-', 100000, 'SNI 06-6989.11-2004');

-- Contoh data permintaan layanan
INSERT INTO service_requests (request_code, customer_id, request_type, sampling_date, sampling_location, status, total_amount) VALUES
('REQ001', 1, 'sampling_analysis', '2024-03-20', 'Plant A - Outlet IPAL', 'approved', 700000),
('REQ002', 2, 'analysis_only', '2024-03-21', 'Lab Internal', 'submitted', 450000),
('REQ003', 3, 'sampling_analysis', '2024-03-22', 'Intake Water Treatment', 'draft', 550000);

-- Contoh data parameter yang diminta
INSERT INTO request_parameters (request_id, parameter_id, price) VALUES
(1, 1, 250000),
(1, 2, 200000),
(1, 3, 150000),
(1, 4, 100000),
(2, 1, 250000),
(2, 2, 200000),
(3, 3, 150000),
(3, 4, 100000);

-- Contoh data quotation
INSERT INTO quotations (quotation_code, request_id, issue_date, valid_until, status, total_amount) VALUES
('QUO001', 1, '2024-03-15', '2024-04-15', 'approved', 700000),
('QUO002', 2, '2024-03-16', '2024-04-16', 'sent', 450000),
('QUO003', 3, '2024-03-17', '2024-04-17', 'draft', 550000);
