<?php

namespace app\controller\admin\system;

use app\BaseController;
use app\entity\SysOrganization;
use app\service\CurdService;
use think\response\Json;

class OrganizationController extends BaseController
{
    public function lists(): Json
    {
        $model = SysOrganization::withSearch(['organizationType', 'organizationName'], [
            'organizationType' => $this->request->get('organizationType/d', 0),
            'organizationName' => $this->request->get('organizationName/s', ''),
        ])->append(['organizationTypeName']);

        $lists = CurdService::getList($this->request, $model);

        return $this->writeSuccess('ok', $lists);
    }

    public function update()
    {
        $data = $this->request->post([
            'parentId' => 0,
            'organizationType' => 0,
            'sortNumber' => 100,
            'organizationName' => '',
            'organizationFullName' => '',
            'organizationCode' => '',
            'comments' => '',
            'organizationId' => ''
        ]);
        $sysDictionary = SysOrganization::findOrFail($data['organizationId']);
        $sysDictionary->save([
            'parent_id' => $data['parentId'],
            'sort_number' => $data['sortNumber'],
            'organization_type' => $data['organizationType'],
            'organization_name' => $data['organizationName'],
            'organization_full_name' => $data['organizationFullName'],
            'organization_code' => $data['organizationCode'],
            'comments' => $data['comments'],
        ]);
        return $this->writeSuccess('更新成功');
    }

    public function add()
    {
        $data = $this->request->post([
            'parentId' => 0,
            'organizationType' => 0,
            'sortNumber' => 100,
            'organizationName' => '',
            'organizationFullName' => '',
            'organizationCode' => '',
            'comments' => '',

        ]);
        $sysDictionary = new SysOrganization();
        $sysDictionary->save([
            'parent_id' => $data['parentId'],
            'sort_number' => $data['sortNumber'],
            'organization_type' => $data['organizationType'],
            'organization_name' => $data['organizationName'],
            'organization_full_name' => $data['organizationFullName'],
            'organization_code' => $data['organizationCode'],
            'comments' => $data['comments'],
        ]);
        return $this->writeSuccess('添加成功');
    }

    public function remove(SysOrganization $model)
    {
        $model->delete();

        return $this->writeSuccess('删除成功');
    }
}