<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    public function up(): void
    {
        $bodyTemplate = <<<'HTML'
<table style="width:100%; border-collapse:collapse; font-size:14px; border:2px solid #333;">
  <colgroup>
    <col style="width:15%;">
    <col style="width:35%;">
    <col style="width:15%;">
    <col style="width:35%;">
  </colgroup>
  <tr>
    <td style="border:1px solid #999; padding:8px 12px; background:#f5f5f5; font-weight:bold;">지출일자</td>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
    <td style="border:1px solid #999; padding:8px 12px; background:#f5f5f5; font-weight:bold;">부서</td>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
  </tr>
  <tr>
    <td style="border:1px solid #999; padding:8px 12px; background:#f5f5f5; font-weight:bold;">거래처</td>
    <td style="border:1px solid #999; padding:8px 12px;" colspan="3"></td>
  </tr>
  <tr style="background:#e8e8e8;">
    <td style="border:1px solid #999; padding:8px 12px; font-weight:bold; text-align:center;">계정과목</td>
    <td style="border:1px solid #999; padding:8px 12px; font-weight:bold; text-align:center;">적요</td>
    <td style="border:1px solid #999; padding:8px 12px; font-weight:bold; text-align:center;">금액</td>
    <td style="border:1px solid #999; padding:8px 12px; font-weight:bold; text-align:center;">비고</td>
  </tr>
  <tr>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
    <td style="border:1px solid #999; padding:8px 12px; text-align:right;"></td>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
  </tr>
  <tr>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
    <td style="border:1px solid #999; padding:8px 12px; text-align:right;"></td>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
  </tr>
  <tr>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
    <td style="border:1px solid #999; padding:8px 12px; text-align:right;"></td>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
  </tr>
  <tr style="background:#f5f5f5;">
    <td style="border:1px solid #999; padding:8px 12px; font-weight:bold; text-align:center;" colspan="2">합계</td>
    <td style="border:1px solid #999; padding:8px 12px; font-weight:bold; text-align:right;"></td>
    <td style="border:1px solid #999; padding:8px 12px;"></td>
  </tr>
  <tr>
    <td style="border:1px solid #999; padding:8px 12px; background:#f5f5f5; font-weight:bold;">지출 사유</td>
    <td style="border:1px solid #999; padding:8px 12px;" colspan="3" style="min-height:60px;"></td>
  </tr>
</table>
HTML;

        DB::table('approval_forms')->insertOrIgnore([
            'tenant_id' => 1,
            'name' => '지출결의서',
            'code' => 'expense',
            'category' => 'expense',
            'template' => json_encode([
                'fields' => [
                    ['name' => 'expense_date', 'type' => 'date', 'label' => '지출일자'],
                    ['name' => 'vendor', 'type' => 'text', 'label' => '거래처'],
                    ['name' => 'account', 'type' => 'text', 'label' => '계정과목'],
                    ['name' => 'amount', 'type' => 'number', 'label' => '금액'],
                    ['name' => 'description', 'type' => 'text', 'label' => '적요'],
                ],
            ], JSON_UNESCAPED_UNICODE),
            'body_template' => $bodyTemplate,
            'is_active' => true,
            'created_by' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    public function down(): void
    {
        DB::table('approval_forms')
            ->where('tenant_id', 1)
            ->where('code', 'expense')
            ->delete();
    }
};
