<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\controller\ante_config;

use app\dice\helper\AdminScopeHelper;
use app\dice\logic\ante_config\DiceAnteConfigLogic;
use app\dice\model\ante_config\DiceAnteConfig;
use app\dice\validate\ante_config\DiceAnteConfigValidate;
use plugin\saiadmin\basic\BaseController;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;

/**
 * 底注配置控制器
 */
class DiceAnteConfigController extends BaseController
{
    public function __construct()
    {
        $this->logic = new DiceAnteConfigLogic();
        $this->validate = new DiceAnteConfigValidate();
        parent::__construct();
    }

    #[Permission('底注配置列表', 'dice:ante_config:index:index')]
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['name', ''],
            ['title', ''],
            ['is_default', ''],
        ]);
        $query = $this->logic->search($where);
        AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 底注下拉选项（按渠道），供一键测试权重等使用
     */
    #[Permission('底注配置列表', 'dice:ante_config:index:index')]
    public function getOptions(Request $request): Response
    {
        $query = DiceAnteConfig::field('id,name,title,mult,is_default')->order('mult', 'asc')->order('id', 'asc');
        AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
        $list = $query->select();
        $data = $list->map(static function ($item) {
            return [
                'id'          => (int) $item['id'],
                'name'        => (string) ($item['name'] ?? ''),
                'title'       => (string) ($item['title'] ?? ''),
                'mult'        => (int) ($item['mult'] ?? 0),
                'is_default'  => (int) ($item['is_default'] ?? 0),
            ];
        })->toArray();
        return $this->success($data);
    }

    #[Permission('底注配置读取', 'dice:ante_config:index:read')]
    public function read(Request $request): Response
    {
        $id = $request->input('id', '');
        $model = $this->logic->read($id);
        $data = is_array($model) ? $model : $model->toArray();
        return $this->success($data);
    }

    #[Permission('底注配置添加', 'dice:ante_config:index:save')]
    public function save(Request $request): Response
    {
        $data = $request->post();
        $this->validate('save', $data);
        AdminScopeHelper::prepareConfigSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
        $result = $this->logic->add($data);
        return $result ? $this->success('add success') : $this->fail('add failed');
    }

    #[Permission('底注配置修改', 'dice:ante_config:index:update')]
    public function update(Request $request): Response
    {
        $data = $request->post();
        if ($data === [] || $data === null) {
            $data = $request->all();
        }
        $this->validate('update', $data);
        $requestDeptId = AdminScopeHelper::pickRequestDeptId($request->input('dept_id'), is_array($data) ? $data : []);
        $model = $this->logic->read($data['id'] ?? 0);
        if ($model) {
            $recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
            if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $requestDeptId)) {
                return $this->fail('no permission to update this record');
            }
        }
        $result = $this->logic->edit($data['id'], $data, $this->adminInfo ?? null, $requestDeptId);
        return $result ? $this->success('update success') : $this->fail('update failed');
    }

    #[Permission('底注配置删除', 'dice:ante_config:index:destroy')]
    public function destroy(Request $request): Response
    {
        $ids = $request->post('ids', '');
        if (empty($ids)) {
            return $this->fail('please select data to delete');
        }
        $result = $this->logic->destroy($ids);
        return $result ? $this->success('delete success') : $this->fail('delete failed');
    }
}
