<?php

namespace app\admin\controller;

use app\admin\controller\base\Permissions;
use think\Db;
use think\Session;

class Announcement extends Permissions
{
    private function getModel()
    {
        return new \app\common\model\Announcement();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $where = [];
            if (isset($post['ids']) and !empty($post['ids'])) {
                $where['id'] = ['in', $post['ids']];
            }
            if (!empty($post["title"])) {
                $where["title"] = ['like', '%' . $post["title"] . '%'];
            }
            if (isset($post["admin_id"]) and "" != $post["admin_id"]) {
                $where["admin_id"] = $post["admin_id"];
            }
            if (isset($post["create_time"]) and !empty($post["create_time"])) {
                $timerang = explode(' - ', $post["create_time"]);
                $min_time = strtotime($timerang[0]);
                $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime($timerang[1]??'');
                $where["create_time"] = [['>=', $min_time], ['<=', $max_time]];
            }

            $model = $this->getModel();
            $count = $model->where($where)->count();
            $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('id desc')->select();

            $adminModel = new \app\admin\model\Admin();
            $adminIdAndName = $adminModel->column('nickname', 'id');
            foreach ($data as $key => $value) {
                $value['admin_name'] = $adminIdAndName[$value['admin_id']]??"";
                $data[$key] = $value;
            }
            return array('code' => 0, 'count' => $count, 'data' => $data);
        } else {
            //创建人
            $this->assign('admins', Db::name('admin')->select());
            return $this->fetch();
        }
    }


    public function publish()
    {
        $id = $this->request->param('id', 0, 'intval');
        $model = $this->getModel();
        $post = $this->request->post();
        if ($this->request->isPost()) {
            //验证
            $validate = new \think\Validate([
                ['title|标题', 'max:50'],
                ['content|内容', 'max:5000'],
            ]);
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
        }

        if ($id > 0) {
            //修改
            $data = $model->where('id', $id)->find();
            if (empty($data)) {
                $this->error('id不正确');
            }
            if ($this->request->isPost()) {
                $post['admin_id'] = Session::get(self::ADMIN_ID);
                if (false == $model->allowField(true)->save($post, ['id' => $id])) {
                    $this->error('修改失败');
                } else {
                    $this->success('修改成功');
                }
            } else {
                $this->assign('data', $data);
                return $this->fetch();
            }
        } else {
            //新增
            if ($this->request->isPost()) {
                $post['admin_id'] = Session::get(self::ADMIN_ID);
                if (false == $model->allowField(true)->save($post)) {
                    $this->error('添加失败');
                } else {
                    $this->success('添加成功', 'index');
                }
            } else {
                return $this->fetch();
            }
        }
    }

    public function delete()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', 0, 'intval');
            if (false == $this->getModel()->where('id', $id)->delete()) {
                $this->error('删除失败');
            } else {
                $this->success('删除成功', 'index');
            }
        }
    }

    public function deletes()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $ids = $post['ids'];
            if ($this->getModel()->where('id', 'in', $ids)->delete()) {
                $this->success('删除成功');
            }
        }
    }

}