-- https://www.phpmyadmin.net/
--

START TRANSACTION;



--
-- Database: `serellahijab_db`
--

DELIMITER $$
--
-- Procedures
--
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_cancel_order` (IN `p_order_id` INT)   BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE v_product_id INT;
    DECLARE v_quantity INT;
    DECLARE cur CURSOR FOR 
        SELECT product_id, quantity 
        FROM order_items 
        WHERE order_id = p_order_id;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    
    -- Update order status
    UPDATE orders SET status = 'Dibatalkan' WHERE id = p_order_id;
    
    -- Restore stock for each item
    OPEN cur;
    read_loop: LOOP
        FETCH cur INTO v_product_id, v_quantity;
        IF done THEN
            LEAVE read_loop;
        END IF;
        
        UPDATE products 
        SET stock = stock + v_quantity 
        WHERE id = v_product_id;
    END LOOP;
    CLOSE cur;
END$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_update_product_stock` (IN `p_product_id` INT, IN `p_quantity` INT)   BEGIN
    UPDATE products 
    SET stock = stock - p_quantity 
    WHERE id = p_product_id AND stock >= p_quantity;
END$$

DELIMITER ;

-- --------------------------------------------------------

--
-- Table structure for table `cart`
--

CREATE TABLE `cart` (
  `id` int(11) NOT NULL,
  `user_id` varchar(100) NOT NULL,
  `product_id` int(11) NOT NULL,
  `quantity` int(11) NOT NULL DEFAULT 1,
  `added_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `orders`
--

CREATE TABLE `orders` (
  `id` int(11) NOT NULL,
  `order_id` varchar(50) NOT NULL,
  `user_id` varchar(100) NOT NULL,
  `user_name` varchar(100) DEFAULT NULL,
  `customer_name` varchar(100) NOT NULL,
  `customer_phone` varchar(20) NOT NULL,
  `customer_email` varchar(100) DEFAULT NULL,
  `customer_address` text NOT NULL,
  `payment_method` varchar(50) DEFAULT NULL,
  `order_notes` text DEFAULT NULL,
  `total` decimal(10,2) NOT NULL,
  `status` enum('Diproses','Selesai','Dibatalkan') DEFAULT 'Diproses',
  `order_date` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `orders`
--

INSERT INTO `orders` (`id`, `order_id`, `user_id`, `user_name`, `customer_name`, `customer_phone`, `customer_email`, `customer_address`, `payment_method`, `order_notes`, `total`, `status`, `order_date`, `updated_at`) VALUES
(1, 'ORD-1700000001-1234', 'demo@serellahijab.com', 'Demo User', 'Demo User', '081234567890', 'demo@serellahijab.com', 'Jl. Merdeka No. 123, Palembang, Sumatera Selatan', 'bca', 'Kirim cepat ya', 180000.00, 'Diproses', '2024-11-15 10:30:00', '2025-11-19 17:06:38'),
(2, 'ORD-1700000002-5678', 'sarah@example.com', 'Sarah Ahmad', 'Sarah Ahmad', '081298765432', 'sarah@example.com', 'Jl. Sudirman No. 45, Jakarta Pusat', 'gopay', '', 265000.00, 'Selesai', '2024-11-14 14:20:00', '2025-11-19 17:06:38'),
(3, 'ORD-1700000003-9012', 'fatimah@example.com', 'Fatimah Zahra', 'Fatimah Zahra', '081387654321', 'fatimah@example.com', 'Jl. Ahmad Yani No. 67, Surabaya', 'dana', 'Warna hijab tolong yang soft', 155000.00, 'Diproses', '2024-11-15 09:15:00', '2025-11-19 17:06:38');

-- --------------------------------------------------------

--
-- Table structure for table `order_items`
--

CREATE TABLE `order_items` (
  `id` int(11) NOT NULL,
  `order_id` int(11) NOT NULL,
  `product_id` int(11) DEFAULT NULL,
  `product_name` varchar(200) NOT NULL,
  `quantity` int(11) NOT NULL,
  `price` decimal(10,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `order_items`
--

INSERT INTO `order_items` (`id`, `order_id`, `product_id`, `product_name`, `quantity`, `price`) VALUES
(1, 1, 1, 'Hijab Segi Empat Premium', 2, 85000.00),
(2, 1, 3, 'Hijab Instan Bergo', 1, 65000.00),
(3, 2, 2, 'Pashmina Ceruti', 2, 95000.00),
(4, 2, 4, 'Hijab Sport Active', 1, 75000.00),
(5, 3, 1, 'Hijab Segi Empat Premium', 1, 85000.00),
(6, 3, 7, 'Hijab Instan Pet', 1, 70000.00);

-- --------------------------------------------------------

--
-- Table structure for table `products`
--

CREATE TABLE `products` (
  `id` int(11) NOT NULL,
  `name` varchar(200) NOT NULL,
  `category` varchar(50) NOT NULL,
  `price` decimal(10,2) NOT NULL,
  `description` text DEFAULT NULL,
  `image` varchar(255) DEFAULT NULL,
  `badge` varchar(50) DEFAULT NULL,
  `stock` int(11) DEFAULT 100,
  `is_active` tinyint(1) DEFAULT 1,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `products`
--

INSERT INTO `products` (`id`, `name`, `category`, `price`, `description`, `image`, `badge`, `stock`, `is_active`, `created_at`, `updated_at`) VALUES
(1, 'Hijab Segi Empat Premium', 'segi empat', 85000.00, 'Bahan voal premium, halus dan adem', 'images/segiempatpremium1.jpg', 'Best Seller', 100, 1, '2025-11-19 17:06:38', '2025-11-19 17:06:38'),
(2, 'Pashmina Ceruti', 'pashmina', 45000.00, 'Pashmina lembut dengan drape sempurna', 'images/pashminaceruti1.jpg', 'New', 100, 1, '2025-11-19 17:06:38', '2025-11-19 17:08:04'),
(3, 'Hijab Instan Bergo', 'instan', 65000.00, 'Praktis dan stylish untuk aktivitas harian', 'images/instanbergo1.jpg', '', 100, 1, '2025-11-19 17:06:38', '2025-11-19 17:06:38'),
(4, 'Hijab Sport Active', 'sport', 75000.00, 'Breathable dan nyaman untuk olahraga', 'images/sport1.jpg', '', 100, 1, '2025-11-19 17:06:38', '2025-11-19 17:06:38'),
(5, 'Segi Empat Motif', 'segi empat', 90000.00, 'Motif eksklusif dan trendy', 'images/segiempatmotif1.jpg', 'Promo', 100, 1, '2025-11-19 17:06:38', '2025-11-19 17:06:38'),
(6, 'Pashmina Rawis', 'pashmina', 105000.00, 'Tekstur rawis yang elegan', 'images/pashminarawis1.jpg', '', 100, 1, '2025-11-19 17:06:38', '2025-11-19 17:06:38'),
(7, 'Hijab Instan Pet', 'instan', 70000.00, 'Dengan pet anti tembem', 'images/instanpet1.jpg', '', 100, 1, '2025-11-19 17:06:38', '2025-11-19 17:06:38'),
(8, 'Hijab Sport Mesh', 'sport', 80000.00, 'Material mesh anti panas', 'images/sportmesh1.jpg', 'Best Seller', 100, 1, '2025-11-19 17:06:38', '2025-11-19 17:06:38');

-- --------------------------------------------------------

--
-- Stand-in structure for view `v_best_selling_products`
-- (See below for the actual view)
--
CREATE TABLE `v_best_selling_products` (
`product_id` int(11)
,`product_name` varchar(200)
,`total_sold` decimal(32,0)
,`total_revenue` decimal(42,2)
,`total_orders` bigint(21)
);

-- --------------------------------------------------------

--
-- Stand-in structure for view `v_dashboard_stats`
-- (See below for the actual view)
--
CREATE TABLE `v_dashboard_stats` (
`total_orders` bigint(21)
,`orders_diproses` bigint(21)
,`orders_selesai` bigint(21)
,`orders_dibatalkan` bigint(21)
,`total_revenue` decimal(32,2)
,`total_customers` bigint(21)
,`total_products` bigint(21)
);

-- --------------------------------------------------------

--
-- Stand-in structure for view `v_top_customers`
-- (See below for the actual view)
--
CREATE TABLE `v_top_customers` (
`customer_name` varchar(100)
,`customer_phone` varchar(20)
,`customer_email` varchar(100)
,`total_orders` bigint(21)
,`total_spent` decimal(32,2)
);

-- --------------------------------------------------------

--
-- Structure for view `v_best_selling_products`
--

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_best_selling_products`  AS SELECT `oi`.`product_id` AS `product_id`, `oi`.`product_name` AS `product_name`, sum(`oi`.`quantity`) AS `total_sold`, sum(`oi`.`quantity` * `oi`.`price`) AS `total_revenue`, count(distinct `oi`.`order_id`) AS `total_orders` FROM (`order_items` `oi` join `orders` `o` on(`oi`.`order_id` = `o`.`id`)) WHERE `o`.`status` <> 'Dibatalkan' GROUP BY `oi`.`product_id`, `oi`.`product_name` ORDER BY sum(`oi`.`quantity`) DESC ;

-- --------------------------------------------------------

--
-- Structure for view `v_dashboard_stats`
--

-- --------------------------------------------------------

--
-- Structure for view `v_top_customers`
--

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_top_customers`  AS SELECT `orders`.`customer_name` AS `customer_name`, `orders`.`customer_phone` AS `customer_phone`, `orders`.`customer_email` AS `customer_email`, count(0) AS `total_orders`, sum(`orders`.`total`) AS `total_spent` FROM `orders` WHERE `orders`.`status` <> 'Dibatalkan' GROUP BY `orders`.`customer_phone`, `orders`.`customer_name`, `orders`.`customer_email` ORDER BY sum(`orders`.`total`) DESC ;

--
-- Indexes for table `cart`
--
ALTER TABLE `cart`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `user_product` (`user_id`,`product_id`),
  ADD KEY `idx_user_id` (`user_id`),
  ADD KEY `idx_product_id` (`product_id`);

--
-- Indexes for table `orders`
--
ALTER TABLE `orders`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `order_id` (`order_id`),
  ADD KEY `idx_user_id` (`user_id`),
  ADD KEY `idx_status` (`status`),
  ADD KEY `idx_order_date` (`order_date`),
  ADD KEY `idx_customer_phone` (`customer_phone`);

--
-- Indexes for table `order_items`
--
ALTER TABLE `order_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_order_id` (`order_id`),
  ADD KEY `idx_product_id` (`product_id`);

--
-- Indexes for table `products`
--
ALTER TABLE `products`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_category` (`category`),
  ADD KEY `idx_is_active` (`is_active`);

--
-- AUTO_INCREMENT for table `cart`
--
ALTER TABLE `cart`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `orders`
--
ALTER TABLE `orders`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT for table `order_items`
--
ALTER TABLE `order_items`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;

--
-- AUTO_INCREMENT for table `products`
--
ALTER TABLE `products`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `order_items`
--
ALTER TABLE `order_items`
  ADD CONSTRAINT `fk_order_items_order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;

