<?php
/**
 *
 * @author：llbjj
 * @DateTime：2022/6/1 11:40
 * @Description： 表单管理 - 表单类型管理
 *
 */

namespace Application\Controller;

use Application\Common\Com;
use Application\Common\StatusCode;
use Application\Mvc\Controller\BasicController;
use Laminas\Db\Sql\Predicate\Operator;
use Laminas\Stdlib\ArrayUtils;

class FormgroupController extends BasicController
{
    /**
     * Notes: 左侧列表数据
     * User: llbjj
     * DateTime: 2022/6/1 20:51
     *
     * @return \Laminas\View\Model\JsonModel
     */
    public function treelistAction(){
        $formgroupWhere = 'is_del = 0';
        $formgroupDatas = $this->LocalService()->dictionaryFormGroup->fetchAll([
            'where' => $formgroupWhere,
            'columns' => ['id', 'name', 'code'],
            'order' => 'order',
        ]);
        return $this->RenderApiJson()->Success($formgroupDatas);
    }
    /**
     * Notes: 表单类型列表
     * User: llbjj
     * DateTime: 2022/6/1 11:41
     *
     */
    public function listAction(){
        $searchField = [];
        $searchField = $this->params()->fromPost();
        $limit = $searchField['limit'] ?? 10;
        $page = $searchField['page'] ?? 1;
        $offset = ($page - 1) * $limit;

        $formgroupWhere = [
            'is_del' => 0
        ];

        //表单类型列表检索表单
        $DictionaryFormGroupSearchFormData = $this->Introduce('DictionaryFormGroup')['listSearch'];
        $searchFormFieldData = array_values($DictionaryFormGroupSearchFormData);

        // 根据检索创建where条件
        if(!empty($searchField)) $formgroupWhere = ArrayUtils::merge($formgroupWhere, Com::CreateSearchWhere($searchField, $DictionaryFormGroupSearchFormData));

        //表单类型总数
        $formgroupCount = $this->LocalService()->dictionaryFormGroup->getCount($formgroupWhere);
        //表单类型列表信息
        $formgroupDatas = $this->LocalService()->dictionaryFormGroup->fetchAll([
            'where' => $formgroupWhere,
            'columns' => ['id', 'name', 'code', 'order'],
            'order' => 'order',
            'limit' => $limit,
            'offset' => $offset
        ]);


        return $this->RenderApiJson()->Success($formgroupDatas, '', ['total' => $formgroupCount, 'searchFormFieldData' => $searchFormFieldData]);
    }

    /**
     * Notes: 表单类型增改
     * User: llbjj
     * DateTime: 2022/6/1 11:42
     *
     */
    public function editAction(){
        $id = $this->params()->fromPost('id', 0);
        $formFieldData = array_values($this->Introduce('DictionaryFormGroup')['editForm']);

        $formGroupData = [];
        if(!empty($id)){
            $formGroupData = $this->LocalService()->dictionaryFormGroup->fetchOne([
                'where' => [
                    'is_del' => 0,
                    'id' => $id,
                ],
                'columns' => ['id', 'name', 'code', 'order']
            ]);
        }else{
            $formGroupData['order'] = $this->LocalService()->dictionaryFormGroup->getMaxOrder('order');
        }
        return $this->RenderApiJson()->Success($formGroupData, '', ['formFieldData' => $formFieldData]);
    }

    /**
     * Notes: 保存表单类型信息
     * User: llbjj
     * DateTime: 2022/6/1 11:42
     *
     */
    public function saveAction(){
        if($this->getRequest()->isPost()){
            $this->Check('DictionaryFormGroup.editForm');

            $dictionaryFormGroupSv = $this->LocalService()->dictionaryFormGroup;

            $submitData = $this->params()->fromPost();
            if(isset($submitData['old_data_str'])) unset($submitData['old_data_str']);

            $id = 0;
            $oper_type = 0;
            if(!empty($submitData['id'])){
                // 编辑检测信息是否存在
                if(!$dictionaryFormGroupSv->getCount([
                    'id' => $submitData['id'],
                    'is_del' => 0
                ])) return $this->RenderApiJson()->Error(StatusCode::E_FIELD_VALIDATOR, '信息不存在！');

                $id = $submitData['id'];
                $oper_type = 1;
            }else{
                $submitData['is_del'] = 0;
            }

            // 验证名称是否重复
            $dictionaryFormGroupSv->check_field_repeat('name', $submitData['name'], [
                'is_del' => 0,
                new Operator('id', Operator::OP_NE, $id)
            ], '表单类型名称【'.$submitData['name'].'】已存在！');
            // 验证编码是否重复
            $dictionaryFormGroupSv->check_field_repeat('code', $submitData['code'], [
                'is_del' => 0,
                new Operator('id', Operator::OP_NE, $id)
            ], '表单类型编码【'.$submitData['code'].'】已存在！');

            $result = $dictionaryFormGroupSv->save($submitData);
            $event_id = $id ?: $result;
            if($result) $this->LocalService()->log->saveFileLog($event_id, 'DictionaryFormGroup.editForm', $submitData, $oper_type);
            return $this->RenderApiJson()->Success(['id' => $event_id]);
        }else{
            return $this->RenderApiJson()->Error(StatusCode::E_ACCESS);
        }
    }

    /**
     * Notes: 删除表单类型 - 表单类型下已有表单信息，则不可删除
     * User: llbjj
     * DateTime: 2022/6/1 11:43
     *
     */
    public function deleteAction(){
        if($this->getRequest()->isPost()){
            $id = $this->params()->fromPost('id', 0);
            $dictionaryFormGroupSv = $this->LocalService()->dictionaryFormGroup;
            // 检测信息是否存在
            if(!$dictionaryFormGroupSv->getCount([
                'id' => $id,
                'is_del' => 0
            ])) return $this->RenderApiJson()->Error(StatusCode::E_FIELD_VALIDATOR, '信息不存在！');

            // 检测表单类型下是否有表单信息，有则不能删除
            $dictionaryFormCount = $this->LocalService()->dictionaryForm->getCount([
                'group_id' => $id,
                'is_del' => 0
            ]);
            if($dictionaryFormCount) return $this->RenderApiJson()->Error(StatusCode::E_FIELD_VALIDATOR, '此表单类型下已存在表单信息，不可删除！');

            $result = $dictionaryFormGroupSv->upDelState([
                'id' => $id,
            ], 1);

            if($result) $this->LocalService()->log->saveFileLog($id, 'DictionaryFormGroup', [], 2);
            return $this->RenderApiJson()->Success();
        }else{
            return $this->RenderApiJson()->Error(StatusCode::E_ACCESS);
        }
    }
}