<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\model\DedicatedLine as DedicatedLineModel;
use app\admin\validate\DedicatedLineValidate;
use Exception;

/**
 * 专线设置
 */
class DedicatedLine extends BaseController
{

    /**
     * @api {get} /dedicatedline/list 列表
     */
    function list()
    {
        try {
            $params = $this->request->param();
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;
            $language_code = $params['language_code'] ?? $this->lang;
            
            $query = DedicatedLineModel::where('language_code', $language_code); 
            if (!empty($params['name'])) {
                $query = $query->where('name', 'like', '%'.$params['name'].'%');
            }
            $count = $query->count();
            $list = $query->order('weight','desc')
                        ->limit($limit)
                        ->page($page)
                        ->select();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['count' => $count, 'list' => $list]);
    }

    //新增/编辑
    public function update()
    { 
        try {
            $params = (new DedicatedLineValidate)->post()->goCheck('edit');
            $id = $this->request->param('id',0);
            $language_code = $params['language_code'] ?? $this->lang;

            $exists = DedicatedLineModel::where('name', $params['name'])->where('language_code', $language_code)->find();
            if ($exists ) {
                if (!$id || $id != $exists->id) {
                    return $this->error('专线已存在');
                }
            }
            if (empty($id)) {
                $dedicatedLine = new DedicatedLineModel();
            } else {
                $dedicatedLine = DedicatedLineModel::where('id', $id)->find();
                if (!$dedicatedLine) {
                    return $this->error('专线不存在');
                }
            }
            $dedicatedLine->name = $params['name'];
            $dedicatedLine->keyword = $params['keyword'] ?? '';
            $dedicatedLine->is_rec = $params['is_rec'] ?? 0;
            $dedicatedLine->weight = $params['weight'];
            $dedicatedLine->language_code = $language_code;
            $dedicatedLine->save();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([], '操作成功');
    }

    //删除
    public function delete()
    {
        try {
            $params = (new DedicatedLineValidate)->goCheck('id');
            $info = DedicatedLineModel::where('id', $params['id'])->find();
            if (!$info) {
                return $this->error('专线不存在');
            }
            $info->delete();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([],'删除成功');
    }

    //设置是否推荐
    public function setStatus()
    {
        try {
            
            $params = (new DedicatedLineValidate)->goCheck('id');
            $info = DedicatedLineModel::where('id', $params['id'])->find();
            if (!$info) {
                return $this->error('专线不存在');
            }
            $info->is_rec = $info->is_rec == 0 ? 1 : 0;
            $info->save();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([], '操作成功');
    }

}