<?php

namespace app\agent\controller;

use app\agent\BaseController;
use app\common\model\Coach as CoachModel;

class Coach extends BaseController
{

    // 删除
    public function delCoach()
    {
        $idarr   = input('idarr/a');
        $agentid = $this->access_agent['id'];
        CoachModel::whereIn('id', $idarr)->where('status', 1)->where('agentid', $agentid)->delete();
        exit(json_encode([
            'code' => 0,
            'msg'  => "",
        ]));
    }

    // 编辑&添加
    public function editCoach()
    {
        $id      = input('id/d');
        $agentid = $this->access_agent['id'];
        if (empty($id)) {
            CoachModel::create([
                'agentid'    => $agentid,
                'title'      => input('title/s'),
                'content'    => input('content/s'),
                'num'        => input('num/d'),
                'coachtime'  => input('coachtime/s'),
                'createtime' => time(),
            ]);
        } else {
            $coach = CoachModel::where('agentid', $agentid)->find($id);
            if (empty($coach)) {
                exit(json_encode([
                    'code' => 1,
                    'msg'  => '数据异常',
                ]));
            }
            if ($coach['status'] > 1) {
                exit(json_encode([
                    'code' => 1,
                    'msg'  => '已审核的数据无法更改',
                ]));
            }
            CoachModel::update([
                'id'        => $id,
                'title'     => input('title/s'),
                'content'   => input('content/s'),
                'num'       => input('num/d'),
                'coachtime' => input('coachtime/s'),
            ]);
        }
        exit(json_encode([
            'code' => 0,
        ]));
    }

    // 列表
    public function coachList()
    {
        $statuslist = CoachModel::$status;
        return view('coach/coachlist', [
            'statuslist' => $statuslist,
        ]);
    }

    public function coachForm()
    {
        $agentid = $this->access_agent['id'];
        $id      = input('id/d, 0');
        $coach   = CoachModel::where('agentid', $agentid)->findOrEmpty($id);

        return view('coach/coachform', [
            'coach' => $coach,
        ]);
    }

    public function listCoach()
    {
        $agentid  = $this->access_agent['id'];
        $limit    = input('limit');
        $page     = input('page');
        $map      = [['agentid', '=', $agentid]];
        $keywords = input('keywords/s');
        if (!empty($keywords)) {
            $map[] = ['title', 'like', '%' . $keywords . '%'];
        }
        $status = input('status/d');
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $list  = CoachModel::where($map)->order(['id' => 'desc'])->limit($limit)->page($page)->append(['status_text'])->select();
        $count = CoachModel::where($map)->count();
        if ($count == 0) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "未查询到数据",
            ]));
        }
        exit(json_encode([
            'code'  => 0,
            'msg'   => "",
            'count' => $count,
            'data'  => $list,
        ]));
    }
}