<?php

namespace app\admin\controller\Enrol;

use app\BaseController;
use app\common\model\Enrol\EnrolAq as ModelEnrolAq;
use app\Request;
use think\facade\Request as  FacadeRequest;
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 EnrolAq extends BaseController
{
    /**
     * 获取招生问答列表
     * @param bool $isExport 是否导出
     */
    public function getEnrolAqList(Request $request, $isExport = false): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['enrol_aq_question', 'LIKE'], ['enrol_aq_status', '='], ['enrol_aq_answer_status', '='],);

        $query = ModelEnrolAq::where($con)
            ->field([
                'enrol_aq_id',
                'enrol_aq_guid',
                'enrol_aq_question',
                'enrol_aq_answer',
                'enrol_aq_sort',
                'enrol_aq_status',
                'enrol_aq_answer_status'
            ])
            ->append(['enrol_aq_answer_status_bool'])
            ->withAttr(
                'enrol_aq_answer_status_bool',
                function ($value, $data) {
                    if ($data['enrol_aq_answer_status'] == 1) return true;
                    if ($data['enrol_aq_answer_status'] == 2) return false;
                }
            )
            ->order('enrol_aq_sort', 'asc');

        return $isExport ? $query->select()->toArray() : msg("获取招生问答列表成功!", $query);
    }

    /**
     * 编辑招生问答
     */
    public function editEnrolAq(Request $request): array
    {
        Db::startTrans();
        try {

            $params = $request->param();
            $this->validate($params, []);


            $model = ModelEnrolAq::where('enrol_aq_guid', $params['enrol_aq_guid'])->find();
            if (!$model) throwErrorMsg("该招生问答不存在", 1);

            if (isset($params['enrol_aq_answer']) && $params['enrol_aq_answer']) {
                if ($model->enrol_aq_answer_status == 1) {
                    $params['enrol_aq_status'] = 2;
                    $params['enrol_aq_answer_status'] = 2;
                }
            }

            $model->allowField([
                'enrol_aq_update_user_guid',
                'enrol_aq_question',
                'enrol_aq_answer',
                'enrol_aq_sort',
                'enrol_aq_status',
                'enrol_aq_answer_status'
            ])->save($params);

            Db::commit();
            return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 删除招生问答
     */
    public function deleteEnrolAq(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'enrol_aq_guid' => 'require',
            ]);
            $enrol_aq = ModelEnrolAq::where([
                'enrol_aq_guid' => explode(',', $params['enrol_aq_guid'])
            ])->select();
            $enrol_aq->delete();

            Db::commit();
            return msg('删除成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        ModelEnrolAq::exportExcel(self::getEnrolAqList($request, true));
    }
}
