<?php
/**
 * Created by PhpStorm.
 * User: 中闽 < 1464674022@qq.com >
 * Date: 2019/12/5
 * Time: 17:44
 */

namespace app\admin\controller;

use app\admin\controller\base\Permissions;
use app\common\model\ConfigOption as optionModel;
use think\Db;
use think\Log;

class ConfigOption extends Permissions
{
    public function index()
    {
        $pid = $this->request->param('pid', 0, 'intval');
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $where = [
                'pid' => $pid,
            ];
            if (isset($post['keywords']) and !empty($post['keywords'])) {
                $where['name'] = ['like', '%' . $post['keywords'] . '%'];
            }
            $model = new optionModel();
            $count = $model->where($where)->count();
            $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('sort desc')->select();

            foreach ($data as $k => $v) {
                $v['cate_name'] = $v->config['name'];
                $v['thumb_url'] = geturl($v['image']);
                $v['group_type'] = $v->config['type'];
                $data[$k] = $v;
            }

            return array('code' => 0, 'count' => $count, 'data' => $data);
        } else {
            $this->assign('config', \app\common\model\Config::get($pid));
            return $this->fetch();
        }
    }

    public function publish()
    {
        $id = $this->request->param('id', 0, 'intval');
        $pid = $this->request->param('pid', 0, 'intval');
        $this->assign('pid', $pid);
        $model = new optionModel();
        $post = $this->request->post();
        if ($this->request->isPost()) {
            $validate = new \think\Validate([
                ['name', 'require', '标题不能为空'],
                ['pid', 'require', '请选择分类'],
            ]);
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
        } else {
            $this->assign('config', \app\common\model\Config::get($pid));
        }
        if ($id > 0) {
            //是修改操作
            $link = $model->where('id', $id)->find();
            if (empty($link)) {
                $this->error('id不正确');
            }
            if ($this->request->isPost()) {
                if (false == $model->allowField(true)->save($post, ['id' => $id])) {
                    $this->error('修改失败');
                } else {
                    $this->success('修改成功', 'index', ['pid' => $pid]);
                }
            } else {
                $this->assign('link', $link);
                return $this->fetch();
            }
        } else {
            //是新增操作
            if ($this->request->isPost()) {

                if ($model->where('pid', $pid)->count() == 0) {
                    $config = \app\common\model\Config::get($pid);
                    if (!$config) {
                        $this->error('pid异常');
                    }
                    if ($config->type == \app\common\model\Config::TYPE_RADIO) {
                        $post['single_status'] = 1;
                    }
                }

                if (false == $model->allowField(true)->save($post)) {
                    $this->error('添加失败');
                } else {
                    $this->success('添加成功', 'index', ['pid' => $pid]);
                }
            } else {
                return $this->fetch();
            }
        }
    }

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

    public function deletes()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $ids = $post['ids'];
            $model = new optionModel();
            if ($model->where('id', 'in', $ids)->delete()) {
                Log::log("批量删除链接：" . implode(',', $ids));
                $this->success('删除成功');
            }
        }
    }

    //排序
    public function sort()
    {
        if ($this->request->isPost() && $this->request->has('ids')) {
            $post = $this->request->post();
            $i = 0;
            foreach ($post['ids'] as $k => $id) {
                $sort = Db::name('config_option')->where('id', $id)->value('sort');
                $newsort = $post['sorts'][$k]??$sort;
                if ($sort != $newsort) {
                    if (false == Db::name('config_option')->where('id', $id)->update(['sort' => $newsort])) {
                        $this->error('更新失败');
                    } else {
                        $i++;
                    }
                }
            }
            $this->success('成功更新' . $i . '个数据', 'index');
        } else {
            $this->error('无数据更新', 'index');
        }
    }

    public function status()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (false == Db::name('config_option')->where('id', $post['id'])->update(['status' => $post['status']])) {
                $this->error('设置失败');
            } else {
                $this->success('设置成功', 'index');
            }
        }
    }

    //单选状态
    public function single_status()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            Db::name('config_option')->where('pid', $post['pid'])->update(['single_status' => 0]);
            if (false == Db::name('config_option')->where('id', $post['id'])->update(['single_status' => $post['status']])) {
                $this->error('设置失败');
            } else {
                $this->success('设置成功', 'index');
            }
        }
    }
}