<?php

namespace app\admin\controller\Enrol;

use app\BaseController;
use app\common\model\Enrol\SignUpProcess as ModelSignUpProcess;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;


class SignUpProcess extends BaseController
{
    /**
     * 获取报名流程列表
     */
    public function getSignUpProcessList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['sign_up_process_title', 'LIKE'],);

        $query = ModelSignUpProcess::where($con)
            ->field([
                'sign_up_process_id',
                'sign_up_process_guid',
                'sign_up_process_title',
                'sign_up_process_icon',
                'sign_up_process_intro',
                'sign_up_process_sort'
            ])
            ->order('sign_up_process_sort', 'asc');

        return msg("获取报名流程列表成功!", $query);
    }

    /**
     * 编辑报名流程
     */
    public function editSignUpProcess(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'sign_up_process_title|标题' => 'require',
            'sign_up_process_icon|图标' => 'require',
            'sign_up_process_intro|介绍' => 'require',
            'sign_up_process_sort|排序' => 'require'
        ]);
        $model = ModelSignUpProcess::where('sign_up_process_guid', $params['sign_up_process_guid'])->find();
        if (!$model) throwErrorMsg("该报名流程不存在", 1);
        $model->allowField([
            'sign_up_process_update_user_guid',
            'sign_up_process_title',
            'sign_up_process_icon',
            'sign_up_process_intro',
            'sign_up_process_sort'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加报名流程
     */
    public function addSignUpProcess(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'sign_up_process_title|标题' => 'require',
            'sign_up_process_icon|图标' => 'require',
            'sign_up_process_intro|介绍' => 'require',
            'sign_up_process_sort|排序' => 'require'
        ]);
        $model = ModelSignUpProcess::create($params, [
            'sign_up_process_guid',
            'sign_up_process_create_user_guid',
            'sign_up_process_update_user_guid',
            'sign_up_process_title',
            'sign_up_process_icon',
            'sign_up_process_intro',
            'sign_up_process_sort'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除报名流程
     */
    public function deleteSignUpProcess(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'sign_up_process_guid' => 'require',
        ]);
        $sign_up_process = ModelSignUpProcess::where([
            'sign_up_process_guid' => explode(',', $params['sign_up_process_guid'])
        ])->select();
        $sign_up_process->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $select = ModelSignUpProcess::field([
            'sign_up_process_title',
            'sign_up_process_intro',
            'sign_up_process_sort'
        ])
            ->order('sign_up_process_sort', 'asc')
            ->select();
        return ModelSignUpProcess::exportExcel($select);
    }
}
