package com.jfinal.club._admin.admins;

import com.jfinal.aop.Before;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Admins;
import com.jfinal.club.common.model.Role;
import com.jfinal.club.util.CacheSessionUtil;
import com.jfinal.club.util.Cons;
import com.jfinal.club.vo.UserSessionVo;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @descriptions:
 * @author: lc
 * @date: 2020-06-30 17:14:33
 * @Version: 1.0
 **/
public class AdminsController extends BaseController {

    public void index() {
        String text = get("text");
        set("text", text);
        if (StringUtils.isNotBlank(text)) {
            text = "%" + text + "%";
        } else {
            text = "%%";
        }
        Page<Admins> p = Admins.dao.paginate(getInt("p", 1), Cons.ADMIN_PAGESIZE, "select * ", "from admins where id != 1 and username like ?", text);
        set("page", p);

    }

    public void add() {
        render("add_edit.html");
    }

    @Before(AdminsValidator.class)
    public void save() {
        Admins model = getModel(Admins.class, "adm");
        model.set("create_date", new Date()).set("lock", 0).removeNullValueAttrs().save();
        renderJson(Ret.ok("msg", "添加成功"));
    }

    public void edit() {
        Admins admins = Admins.dao.findById(getInt("id"));
        set("adm", admins);
        render("add_edit.html");
    }

    @Before(AdminsValidator.class)
    public void update() {
        Admins model = getModel(Admins.class, "adm");
        model.set("create_date", new Date()).removeNullValueAttrs().update();
        renderJson(Ret.ok("msg", "修改成功"));
    }

    public void delete() {
        Admins.dao.deleteById(getInt("id"));
        renderJson(Ret.ok("msg", "删除成功!"));
    }

    public void role() {
        Integer id = getInt("id");
        Admins admins = Admins.dao.findById(id);
        String role = admins.getRole();
        List<String> rid = null;
        if (StringUtils.isNotBlank(role)) {
            rid = Arrays.asList(role.split(","));
        }
        List<Role> roleList = Role.dao.findAll();
        List<String> finalRid = rid;
        roleList.forEach(a -> {
            a.put("active", 0);
            if (null != finalRid) {
                finalRid.forEach(b -> {
                    if (a.getId().equals(Integer.valueOf(b))) {
                        a.put("active", 1);
                    }
                });
            }
        });
        set("adm", admins);
        set("roleList", roleList);
    }

    public void radd() {
        String[] parameterValues = getRequest().getParameterValues("adm.role");
        Admins model = getModel(Admins.class, "adm");

        if (parameterValues != null) {
            String roleId = "";
            for (int i = 0; i < parameterValues.length; i++) {
                String r = parameterValues[i];
                roleId += r + ",";
            }
            roleId = roleId.substring(0, roleId.lastIndexOf(","));
            model.set("role", roleId);
        }
        model.removeNullValueAttrs().update();
        renderJson(Ret.ok("msg", "更新成功"));
    }

    /**
     * 更新当前密码
     */
    public void updpassword() {
        String sessionId = getCookie(Cons.JSESSIONID_ADMIN);
        UserSessionVo session = CacheSessionUtil.getInstance().getSession(sessionId);
        Admins admins = session.getAdmins();
        Admins byId = Admins.dao.findById(admins.getId());
        set("adm", byId);
        render("updpassword.html");
    }

}
