<?php

namespace addons\Slider;

use addons\Slider\model\Slider as SliderModel;
use app\services\Addons;

/**
 * 幻灯片插件
 * @author JYmusic
 */
class Slider extends Addons
{
    public $info = [
        'name'        => 'Slider',
        'title'       => '幻灯片',
        'description' => '幻灯片插件',
        'status'      => 1,
        'author'      => 'JYmusic',
        'version'     => '0.3',
    ];

    public $addon_path = './addons/Slider/';

    /**
     * 配置列表页面
     * @var unknown_type
     */
    public $admin_list = [
        'fields'    => '', //要查的字段
        'list_grid' => array( //这里定义的是除了id序号外的表格里字段显示的表头名和模型一样支持函数和链接
            'title:幻灯片标题',
            'link_title:链接标题',
            'description:幻灯片描述',
            'create_time:创建时间',
            'id:操作:[EDIT]|编辑,[DELETE]|删除',
        ),
        'model'     => 'Slider',
        'order'     => 'level desc,id asc',
    ];

    public $custom_adminlist = 'view/index.html';

    public function install()
    {
        $prefix = config('database.prefix');
        $sql    = <<<SQL
DROP TABLE IF EXISTS `{$prefix}slider`;
CREATE TABLE `{$prefix}slider` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  `title` varchar(80) NOT NULL DEFAULT '' COMMENT '标题',
  `type` tinyint(2) NOT NULL DEFAULT '1' COMMENT '幻灯片类型',
  `description` varchar(255) NOT NULL DEFAULT '' COMMENT '描述',
  `cover_url` varchar(255) NOT NULL DEFAULT '' COMMENT '图片链接地址',
  `link_title` varchar(80) NOT NULL DEFAULT '' COMMENT '链接标题',
  `link` char(140) NOT NULL DEFAULT '' COMMENT '链接地址',
  `level` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '优先级',
  `status` tinyint(2) NOT NULL DEFAULT '1' COMMENT '状态（0：禁用，1：正常）',
  `create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '添加时间',
  `update_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='幻灯片表';
SQL;
        return $this->execSql($sql);
    }

    public function uninstall()
    {
        $prefix = config('database.prefix');
        $sql    = "DROP TABLE IF EXISTS `{$prefix}slider`;";
        return $this->execSql($sql);
    }

    /**
     * 幻灯片视图输出
     * @param array  $param 传入参数数组
     * @return \think\Response
     */
    public function indexSlider($param)
    {
        if (strtolower($param['widget']) == 'slider') {
            $type = isset($param['type']) ? $param['type'] : 1;

            $limit = isset($param['limit']) ? $param['limit'] : 6;
            $list  = (new SliderModel)->getList($limit, $type);
            $this->assign('list', $list);
            return $this->fetch('widget', ['addons_param' => $param]);
        }
    }
}
