<?php

namespace App\Http\Controllers\Admin\Config\Setting;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Config;
use App\Http\Traits\TossTrait;

class CompanyController extends Controller
{
    use TossTrait;

    private Config $configModel;
    private array $tossBanks;

    public function __construct(Config $config)
    {
        $this->configModel = $config;
        $this->tossBanks = $this->getBanks();
    }

    /**
     * 회사
     * @method GET
     * @see /admin/config/setting/company
     */
    public function index()
    {
        return view('admin.config.setting.company', [
            'config' => $this->configModel->getAllMeta(),
            'banks' => $this->tossBanks
        ]);
    }

    /**
     * 회사 저장
     * @method POST
     * @see /admin/config/setting/company
     */
    public function store(Request $request)
    {
        $rules = [
            'company_name' => 'string|nullable',
            'company_reg_no' => 'string|nullable',
            'company_owner' => 'string|nullable',
            'company_phone' => 'string|nullable',
            'company_fax' => 'string|nullable',
            'company_retail_sale_no' => 'string|nullable',
            'company_added_sale_no' => 'string|nullable',
            'company_zip_code' => 'string|nullable',
            'company_address' => 'string|nullable',
            'company_hosting' => 'string|nullable',
            'company_admin_name' => 'string|nullable',
            'company_admin_email' => 'string|nullable',
            'company_site_url' => 'string|nullable',
            'company_bank_code' => 'numeric|nullable',
            'company_bank_owner' => 'string|nullable|max:20',
            'company_bank_number' => 'string|nullable|max:20'
        ];

        $attributes = [
            'company_name' => '회사명',
            'company_reg_no' => '사업자등록번호',
            'company_owner' => '대표자명',
            'company_phone' => '대표전화번호',
            'company_fax' => 'FAX 번호',
            'company_retail_sale_no' => '통신판매업신고번호',
            'company_added_sale_no' => '부가통신 사업자번호',
            'company_zip_code' => '사업장 우편번호',
            'company_address' => '사업장 주소',
            'company_hosting' => '호스팅서비스',
            'company_admin_name' => '정보관리책임자명',
            'company_admin_email' => '정보관리책임자 이메일',
            'company_site_url' => '홈페이지 주소',
            'company_bank_code' => '회사 계좌 정보 - 은행 번호',
            'company_bank_name' => '회사 계좌 정보 - 은행명',
            'company_bank_owner' => '회사 계좌 정보 - 예금주',
            'company_bank_number' => '회사 계좌 정보 - 계좌번호'
        ];

        $posts = $this->validate($request, $rules, [], $attributes);
    
        if ($posts['company_bank_code']) {
            $posts['company_bank_name'] = $this->tossBanks[$posts['company_bank_code']];
        }

        $this->configModel->save($posts, $attributes);

        $message = '회사 정보가 저장되었습니다.';
        return redirect()->route('admin.config.setting.company.index')->with('message', $message);
    }
}
