<?php

namespace Database\Seeders;

use App\Models\Penjualan;
use App\Models\Produk;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        //tb_produk
        $produk = new Produk([
            'kode_produk' => 'P01',
            'nama_produk' => 'Produk 1',            
        ]);
        $produk->save();
        $produk = new Produk([
            'kode_produk' => 'P02',
            'nama_produk' => 'Produk 2',            
        ]);
        $produk->save();
        $produk = new Produk([
            'kode_produk' => 'P03',
            'nama_produk' => 'Produk 3',            
        ]);
        $produk->save();

        //tb_user
        $user = new User([
            'nama_user' => 'Administrator',
            'username' => 'admin',
            'password' => Hash::make('admin'),
            'level' => 'Admin',
            'status_user' => 1,
        ]);
        $user->save();
        $user = new User([
            'nama_user' => 'User',
            'username' => 'user',
            'password' => Hash::make('user'),
            'level' => 'User',
            'status_user' => 1,
        ]);
        $user->save();

        //tb_penjualan
        $limit = 100;
        for ($a = 1; $a <= $limit; $a++) {
            $produk = Produk::inRandomOrder()->first();
            $date = strtotime(date('Y-m-d'));
            $penjualan = new Penjualan([
                'kode_produk' => $produk->kode_produk,
                'tanggal' => date('Y-m-d', strtotime(date('Y-m-d', $date) . '-' . rand(1, 12) . 'MONTHS')),
                'jumlah' => rand(1, 10),
            ]);
            $penjualan->save();
        }
    }
}
