<?php

namespace App\Form\Controllers;


use App\Form\Facades\FormRecordFacade;
use App\Http\Controllers\BaseController;
use App\Form\Services\FormInfoService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class FormController extends BaseController
{
    private $service;
    public function __construct(FormInfoService $service)
    {
        $this->service = $service;
    }

    public function getList(Request $request)
    {
        $params=$request->only(['page_size','page','keyword','date']);
        $rst = $this->service->getList($params);
        return $this->jsonResponse('ok', $rst);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function formById(Request $request)
    {
        $this->validate($request,[
            'id' => 'required'
        ],[
            'id.required'    => 'id不能为空'
        ]);
        $ret=$this->service->getData($request->input('id'));
         return $this->jsonResponse('ok', $ret);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function addForm(Request $request)
    {
        $params=$request->only(['id','name','form_items','status','sort','seo_data', 'seo_id']);
        $rst=$this->service->addData($params);
        return $this->jsonResponse('ok', $rst);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function addItem(Request $request)
    {

        $this->validate($request,[
            'form_id' => 'required'
        ],[
            'form_id.required'    => 'form_id不能为空'
        ]);
        $params=$request->all();
        $rst= $this->service->addItem($params);
        return $this->jsonResponse('ok', $rst);
    }

    /**
     * 删除表单项
     * @param Request $request
     * @return mixed
     */
    public function delItem(Request $request)
    {

        $this->validate($request,[
            'form_id' => 'required',
            'id' => 'required'
        ],[
            'form_id.required'    => 'form_id不能为空',
            'id.required'    => 'id不能为空',
        ]);
        $params=$request->all();
        $rst= $this->service->delItem($params);
        return $this->jsonResponse('ok', $rst);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function sortItem(Request $request)
    {
        $this->validate($request,[
            'ids' => 'required'
        ],[
            'ids.required'    => 'ids不能为空'
        ]);
        return $this->service->sortItem($request->input('ids'));
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function sortItemDetail(Request $request)
    {
        $this->validate($request,[
            'id' => 'required'
        ],[
            'id.required'    => 'id不能为空'
        ]);
        return $this->service->sortItemDetail($request->input('id'));
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function formByEditor(Request $request)
    {
        $data = $this->service->setFormByEditor($request->all());
        return $this->jsonResponse('OK', $data);
    }

    /**
     * 更新表单状态
     * */
    public function stopAndDelFormById(Request $request){
        $this->validate($request,[
            'id' => 'required',
            'status'=>'required'
        ],[
            'id.required'    => 'id不能为空',
            'status.required'    => 'status不能为空'
        ]);
        $id=$request->input('id');
        $status=$request->input('status');
        $data = $this->service->stopAndDelFormById($id,$status);
        return $this->jsonResponse('OK', $data);
    }

    /**
     * 获取对应公司下询盘列表
     * @param Request $request
     * */
    public function correspondCompanyFormList(Request $request) {
        $params=$request->all();
        $rst = $this->service->correspondCompanyFormList($params);
        return $this->jsonResponse('ok',$rst);
    }

    /**
     * 获取未读询盘数据总数量
     * */
    public function getCompanyFormRecordNotReadCount() {
        $rst = $this->service->getCompanyFormRecordNotReadCount();
        return $this->jsonResponse('ok',$rst);
    }

    /**
     * 添加表单数据
     * */
    public function addFormRecord(Request $request)
    {
        $this->validate($request, [
            'form_id' => 'required',
            'form_data' => 'array'
        ]);
        $params = $request->all();
        $referer = $request->header('referer');
        $params['referer']=$referer;
        //dd($params);
        $ip=getClientIp(0, true);
        $ret= FormRecordFacade::addFormRecord($params,$ip);
        return $this->jsonResponse('ok',$ret);
    }

}
