<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\Train as TrainModel;

class Train extends BaseController
{
    public function index()
    {
        return view('train/index');
    }

    public function form()
    {
        $id   = input('id/d, 0');
        $info = TrainModel::findOrEmpty($id);
        return view('train/form', [
            'info' => $info,
        ]);
    }

    public function list()
    {
        $limit = input('limit');
        $page  = input('page');

        $where = [];
        $title = input('title', '');
        if (!empty($title)) {
            $where[] = ['title', 'like', "%{$title}%"];
        }
        $contact = input('contact', '');
        if (!empty($contact)) {
            $where[] = ['contact|mobile', 'like', "%{$contact}%"];
        }
        $status = input('status', 0);
        if (!empty($status)) {
            $where[] = ['status', '=', $status];
        }

        $list  = TrainModel::order(['priority' => 'desc', 'id' => 'desc'])->where($where)->limit($limit)->page($page)->select()->append(['status_text']);
        $count = TrainModel::where($where)->count();
        if ($count == 0) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "未查询到数据",
            ]));
        }
        exit(json_encode([
            'code'  => 0,
            'msg'   => "",
            'count' => $count,
            'data'  => $list,
        ]));
    }

    public function field()
    {
        $id   = input('id/d');
        $info = TrainModel::find($id);
        if ($info == null) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "数据不存在",
            ]));
        } else {
            $info->save([
                input('field/s') => input('value/s'),
            ]);
        }
        exit(json_encode([
            'code' => 0,
        ]));
    }

    public function edit()
    {
        $id = input('id/d');
        if (empty($id)) {
            TrainModel::create([
                'title'       => input('title/s'),
                'contact'     => input('contact/s'),
                'mobile'      => input('mobile/s'),
                'address'     => input('address/s'),
                'status'      => input('status/d') == 1 ? 1 : 2,
                'priority'    => input('priority/d'),
                'create_time' => time(),
            ]);
        } else {
            $info = TrainModel::find($id);
            $info->save([
                'title'    => input('title/s'),
                'contact'  => input('contact/s'),
                'mobile'   => input('mobile/s'),
                'address'  => input('address/s'),
                'status'   => input('status/d') == 1 ? 1 : 2,
                'priority' => input('priority/d'),
            ]);
        }
        exit(json_encode([
            'code' => 0,
        ]));
    }

    public function del()
    {
        $id     = input('id/d');
        $info   = TrainModel::where('id', $id)->select();
        $result = $info->delete();
        if ($result) {
            exit(json_encode([
                'code' => 0,
                'msg'  => "",
            ]));
        }
        exit(json_encode([
            'code' => 1,
            'msg'  => "删除失败，请稍后重试",
        ]));
    }
}
