<?php


namespace App\Form\Controllers;


use App\Http\Controllers\BaseController;
use App\ExcelData\Services\FormService;
use App\ExcelData\Services\FormSheetsService;
use App\ExcelData\Services\FormRecordSumService;
use App\Form\Facades\FormItemFacade;
use App\Form\Services\FormRecordService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FormRecordController extends BaseController
{
    private $service;

    /**
     * UserController constructor.
     * @param FormRecordService $service
     */
    public function __construct(FormRecordService $service)
    {
        $this->service = $service;
    }

    public function getList(Request $request)
    {
        $params=$request->all();
        $rst = $this->service->getList(
            $params
        );
        $formId = $params['form_id'] ?? '';
        if($formId){
            $rst['columns']=FormItemFacade::getItemsByFromId($formId);
        }else{
            $rst['columns']=[];
        }
        return $this->jsonResponse('ok', $rst);
    }

    /**
     * form_id 表单id
     * */
    public function exportList(Request $request)
    {
        $params = $request->all();
        $ret = (new FormSheetsService())->setParams($params)->toOss();
        return $ret;
    }

    /**
     * 获取提交记录详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRecord(Request $request)
    {
        $this->validate(
            $request,
            [
                'id' => 'required'
            ]
        );
        $rst = $this->service->getRecord(
            $request->input('id')
        );
        return $this->jsonResponse('ok', $rst);
    }

    public function addRecord(Request $request)
    {

    }

    /**
     * 删除记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delRecord(Request $request)
    {
        $this->validate(
            $request,
            [
                'id' => 'required'
            ]
        );
        $rst = $this->service->changeStatus(
            $request->input('id'),
            2
        );
        return $this->jsonResponse('ok', $rst);
    }

    /**
     * 修改状态值
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeRecordStatus(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'status' => 'required|in:0,1'
        ]);
        $rst = $this->service->changeStatus(
            $request->input('id'),
            $request->input('status')
        );
        return $this->jsonResponse('ok', $rst);
    }

    /**
     * 设置为已读
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * */
    public function recordSetIsRead(Request $request){
        $ids = $request->input('ids', []);
        $rst = $this->service->recordSetIsRead($ids);
        return $this->jsonResponse('ok', $rst);
    }

    public function centerExportList(Request $request)
    {
        $params = $request->all();
        $rst = (new FormRecordSumService())->setParams($params)->toOss();
        return $this->jsonResponse('ok', $rst);
    }
}
