package com.sifang.modules.sunshine.SystemSetting.controller;


import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.setting.service.MemberService;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTraderEmploye;
import com.sifang.modules.sunshine.SystemSetting.service.TraderEmployeService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.service.SystemService;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
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}/traderEmploye")
public class TraderEmployeController extends BaseExceptionController {

    @Autowired
    private TraderEmployeService traderEmployeeService;

    @Autowired
    MemberService memberService;
    @Autowired
    SystemService systemService;

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

        return "modules/yangguang/systemSetting/trader/traderEmploye";
    }

    /**
     * 获取服务商员工设置列表数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getTraderEmployeeData")
    @ResponseBody
    public String getTraderEmployeeData(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user = UserUtils.getUser();
        String traderId;
        if(User.USER_TYPE_ADVISOR.equals(user.getUserType())){
            CrmTraderEmploye emp = traderEmployeeService.findByUserId(user.getId());
            traderId= emp.getTraderId();
        }else{
            traderId= traderEmployeeService.getTraderId(user.getId());
        }
        return  traderEmployeeService.getPageData(page,map,traderId);
//        List<Record> members = traderEmployeeService.getTaderEmployeData(map, traderId);
//        page.setData(members);
//        page.setCount(Long.valueOf(members.size()));
//        return page.renderJson();
    }

    /**
     * 跳转添加员工页面
     *
     * @return
     */
    @RequestMapping(value = "addEmployee")
    public String addEmployee(HttpServletRequest request,Model model) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            CrmTraderEmploye emp = traderEmployeeService.getById(id);
            User user = systemService.getUser(emp.getUserId());
            model.addAttribute("emp", emp);
            model.addAttribute("user", user);
        }
        List<Record> roles = memberService.getMemberRoles();
        model.addAttribute("roles", roles);
        return "modules/yangguang/systemSetting/trader/addEmploye";
    }

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


    /**
     * 跳转修改员工信息页面
     *
     * @return
     */
    @RequestMapping(value = "updateEmployee")
    public String updateEmployee(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        int imgsCount = 0;
        User user = UserUtils.getUser();
        String traderId ;
        if(User.USER_TYPE_ADVISOR.equals(user.getUserType())){
            CrmTraderEmploye emp = traderEmployeeService.findByUserId(user.getId());
            traderId= emp.getTraderId();
        }else{
            traderId= traderEmployeeService.getTraderId(user.getId());
        }
        Record rd = traderEmployeeService.getSiteEmployeeById(id, traderId);
        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/yangguang/systemSetting/trader/updateEmployee";
    }

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

    /**
     * 添加保存工程师
     * @param request
     * @return
     */
    @RequestMapping(value = "employeSave")
    @ResponseBody
    public Result siteSave(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        traderEmployeeService.saveTraderEmploye(map);
        return Result.ok();
    }


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

    /**
     * 保存员工数据权限
     */
    @ResponseBody
    @RequestMapping(value = "saveTraderRights")
    public Result saveTraderRights(HttpServletRequest request){
        String id=request.getParameter("id");
        String dataAuthority=request.getParameter("dataAuthority");
        traderEmployeeService.saveTraderRights(id,dataAuthority);
        return Result.ok();
    }


}
