package com.sifang.modules.site.siteInfo.web;


import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.setting.service.MemberService;
import com.sifang.modules.site.siteInfo.service.SiteEmployeeService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.ZtreeNode;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("${adminPath}/siteEmployee")
public class SiteEmployeeController extends BaseController {

    @Autowired
    private SiteEmployeeService siteEmployeeService;

    @Autowired
    MemberService memberService;

    /**
     * 跳转服务商员工设置
     *
     * @return
     */
    @RequestMapping(value = "siteEmployeeList")
    public String announcementList(HttpServletRequest request, Model model) {

        return "modules/site/siteInfo/siteEmployee";
    }

    /***
     * 跳转服务商权限设置 ***
     */
    @RequestMapping(value = "siteLimites")
    public String toSiteLimites() {
        return "modules/site/siteInfo/siteLimites";
    }

    /***
     * 获取角色对应的菜单 ***
     */
    @RequestMapping(value = "getMenus")
    @ResponseBody
    public String getMenus(HttpServletRequest request, Model model) {
        User user = UserUtils.getUser();
        String siteId = CrmUtils.getUserRelId(user);
        Integer role = Integer.valueOf(request.getParameter("role"));
        List<ZtreeNode> nodes = memberService.getSiteMenus(siteId, role);
        return new Gson().toJson(nodes);
    }

    /***
     * 保存修改的角色权限 ***
     */
    @RequestMapping(value = "saveMemberRoleMenus")
    @ResponseBody
    public String saveMemberRoleMenus(HttpServletRequest request, Model model) {
        Integer role = Integer.valueOf(request.getParameter("role"));
        User user = UserUtils.getUser();
        String siteId = CrmUtils.getUserRelId(user);
        String permission = request.getParameter("permission");
        String ret = memberService.saveSiteMemberRoleMenus(siteId, role, permission);
        return ret;
    }

    /**
     * 获取服务商员工设置列表数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getSiteEmployeeData")
    @ResponseBody
    public String getSiteEmployeeData(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        String siteId = null;
        User user = UserUtils.getUser();
        siteId = CrmUtils.getSiteId(user);
        List<Record> members = siteEmployeeService.getSiteEmployeeData(map, siteId);
        page.setData(members);
        page.setCount(Long.valueOf(members.size()));
        return page.renderJson();
    }

    /**
     * 跳转添加员工页面
     *
     * @return
     */
    @RequestMapping(value = "addEmployee")
    public String addEmployee() {
        return "modules/site/siteInfo/addEmployee";
    }

    /**
     * 新增员工之前进行校验登录账号是否已经存在
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "beforeSave")
    @ResponseBody
    public String beforeSave(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        String mess = siteEmployeeService.beforeSave(map);
        return mess;
    }


    /**
     * 跳转修改员工信息页面
     *
     * @return
     */
    @RequestMapping(value = "updateEmployee")
    public String updateEmployee(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        int imgsCount = 0;
        String siteId = siteEmployeeService.getSiteId(UserUtils.getUser().getId());
        Record rd = siteEmployeeService.getSiteEmployeeById(id, siteId);
        if (rd != null) {
            model.addAttribute("member", rd);
            if (StringUtils.isNotBlank(rd.getStr("license_img"))) {
                String[] imgArr = rd.getStr("license_img").split(",");
                imgsCount = imgArr.length;
                List<String> imglist = new ArrayList<>();
                for (String img : imgArr) {
                    imglist.add(img);
                }
                model.addAttribute("imglist", imglist);
            }
        }
        model.addAttribute("imgsCount", imgsCount);
        return "modules/site/siteInfo/updateEmployee";
    }

    /**
     * 修改员工保存之前进行校验登录账号是否已经存在
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "updateSaveBefore")
    @ResponseBody
    public String updateSaveBefore(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        String mess = siteEmployeeService.updateSaveBefore(map);
        return mess;
    }

    /**
     * 修改保存
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "updateSave")
    @ResponseBody
    public String updateSave(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        String ret = siteEmployeeService.updateSave(map);
        return ret;
    }

    /**
     * 删除员工信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "delEmployee")
    @ResponseBody
    public String delEmployee(HttpServletRequest request) {
        Map<String, String> params = getParams(request);
        String ret = siteEmployeeService.delEmployee(params);
        return ret;
    }


}
