<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: sai <1430792918@qq.com>
// +----------------------------------------------------------------------
namespace plugin\saiadmin\app\controller\system;

use plugin\saiadmin\app\validate\system\SystemDeptValidate;
use plugin\saiadmin\basic\BaseController;
use plugin\saiadmin\app\logic\system\SystemDeptLogic;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;

/**
 * 渠道控制器
 */
class SystemDeptController extends BaseController
{
    /**
     * 构造
     */
    public function __construct()
    {
        $this->logic = new SystemDeptLogic();
        $this->validate = new SystemDeptValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    #[Permission('渠道数据列表', 'core:dept:index')]
    public function index(Request $request) : Response
    {
        $where = $request->more([
            ['name', ''],
            ['code', ''],
            ['status', ''],
        ]);
        $data = $this->logic->tree($where);
        return $this->success($data);
    }

    /**
     * 读取数据
     * @param Request $request
     * @return Response
     */
    #[Permission('渠道数据读取', 'core:dept:read')]
    public function read(Request $request) : Response
    {
        $id = $request->input('id', '');
        $model = $this->logic->read($id);
        if ($model) {
            $data = is_array($model) ? $model : $model->toArray();
            return $this->success($data);
        } else {
            return $this->fail('not found');
        }
    }

    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    #[Permission('渠道数据添加', 'core:dept:save')]
    public function save(Request $request): Response
    {
        $data = $request->post();
        $this->validate('save', $data);
        $result = $this->logic->add($data);
        if ($result) {
            return $this->success('add success');
        } else {
            return $this->fail('add failed');
        }
    }

    /**
     * 更新数据
     * @param Request $request
     * @return Response
     */
    #[Permission('渠道数据修改','core:dept:update')]
    public function update(Request $request): Response
    {
        $data = $request->post();
        $this->validate('update', $data);
        $result = $this->logic->edit($data['id'], $data);
        if ($result) {
            return $this->success('update success');
        } else {
            return $this->fail('update failed');
        }
    }

    /**
     * 删除数据
     * @param Request $request
     * @return Response
     */
    #[Permission('渠道数据删除','core:dept:destroy')]
    public function destroy(Request $request) : Response
    {
        // DELETE + JSON body 须用 input()；post() 仅表单 POST 有效（与 SystemUserController 一致）
        $ids = $request->input('ids', '');
        if (empty($ids)) {
            return $this->fail('please select data to delete');
        }
        $deleteTables = $request->input('delete_tables', []);
        if (!is_array($deleteTables)) {
            $deleteTables = [];
        }
        $idList = is_array($ids) ? $ids : explode(',', (string) $ids);
        if (!empty($deleteTables)) {
            foreach ($idList as $deptId) {
                $this->logic->destroyWithRelations((int) $deptId, $deleteTables);
            }
            return $this->success('delete success');
        }
        $result = $this->logic->destroy($ids);
        if ($result) {
            return $this->success('delete success');
        }
        return $this->fail('delete failed');
    }

    /**
     * 删除渠道前关联数据预览
     */
    #[Permission('渠道数据删除', 'core:dept:destroy')]
    public function destroyPreview(Request $request): Response
    {
        $ids = $request->input('ids', '');
        if ($ids === '' || $ids === null) {
            return $this->fail('please select data');
        }
        $idList = is_array($ids) ? $ids : explode(',', (string) $ids);
        $data = $this->logic->getDestroyPreview($idList);
        return $this->success($data);
    }

    /**
     * 为所有渠道补齐默认配置
     */
    #[Permission('渠道数据修改', 'core:dept:update')]
    public function syncChannelConfigs(Request $request): Response
    {
        $data = $this->logic->syncAllChannelConfigs();
        return $this->success($data, 'sync success');
    }

    /**
     * 可操作渠道
     * @param Request $request
     * @return Response
     */
    public function accessDept(Request $request) : Response
    {
        $where = ['status' => 1];
        $data = $this->logic->accessDept($where);
        return $this->success($data);
    }

}
