<?php
namespace App\Controllers\CRM;

use App\Controllers\Controller;
use App\Models\CRM\FollowUp;
use App\Models\CRM\Customer;

/** 跟进记录（CRM 系统） */
class FollowUpsController extends Controller
{
    private function nav(): array
    {
        return [
            ['k' => 'dashboard', 'label' => '仪表盘',   'icon' => '📊', 'url' => 'CRM'],
            ['k' => 'customers', 'label' => '客户管理', 'icon' => '🤝', 'url' => 'CRM/customers'],
            ['k' => 'leads',     'label' => '商机线索', 'icon' => '💡', 'url' => 'CRM/leads'],
            ['k' => 'followups', 'label' => '跟进记录', 'icon' => '📞', 'url' => 'CRM/followups'],
        ];
    }

    public function index()
    {
        $follows = (new FollowUp())->all();
        $customers = (new Customer())->all();
        $cmap = [];
        foreach ($customers as $c) { $cmap[$c['id']] = $c['name']; }
        return $this->renderSubsys('crm', 'crm/followups', ['follows' => $follows, 'cmap' => $cmap], $this->nav(), 'followups');
    }

    public function create()
    {
        subsys_admin('crm') or \Core\App::forbidden('需要 CRM 管理员权限');
        $customers = (new Customer())->all();
        return $this->renderSubsys('crm', 'crm/followup_form', ['follow' => null, 'customers' => $customers], $this->nav(), 'followups');
    }

    public function store()
    {
        subsys_admin('crm') or \Core\App::forbidden('需要 CRM 管理员权限');
        if (!csrf_check()) return $this->redirect('CRM/followups');
        (new FollowUp())->insert($this->collect());
        return $this->redirect('CRM/followups');
    }

    public function edit($id)
    {
        subsys_admin('crm') or \Core\App::forbidden('需要 CRM 管理员权限');
        $follow = (new FollowUp())->find($id);
        if (!$follow) return $this->redirect('CRM/followups');
        $customers = (new Customer())->all();
        return $this->renderSubsys('crm', 'crm/followup_form', ['follow' => $follow, 'customers' => $customers], $this->nav(), 'followups');
    }

    public function update($id)
    {
        subsys_admin('crm') or \Core\App::forbidden('需要 CRM 管理员权限');
        if (!csrf_check()) return $this->redirect('CRM/followups');
        $follow = (new FollowUp())->find($id);
        if (!$follow) return $this->redirect('CRM/followups');
        (new FollowUp())->update($id, $this->collect());
        return $this->redirect('CRM/followups');
    }

    public function destroy($id)
    {
        subsys_admin('crm') or \Core\App::forbidden('需要 CRM 管理员权限');
        (new FollowUp())->delete($id);
        return $this->redirect('CRM/followups');
    }

    private function collect(): array
    {
        return [
            'customer_id' => (int)$this->post('customer_id'),
            'lead_id'     => (int)$this->post('lead_id'),
            'content'     => trim($this->post('content')),
            'next_at'     => trim($this->post('next_at')),
            'way'         => trim($this->post('way')),
            'result'      => trim($this->post('result')),
            'owner'       => trim($this->post('owner')) ?: ($_SESSION['admin_name'] ?? ''),
            'created_at'  => date('Y-m-d'),
        ];
    }
}
