package com.sifang.modules.sunshine.partnerManagement.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.partnerManagement.entity.CrmPartnerStaff;
import com.sifang.modules.sunshine.partnerManagement.service.PartnerStaffService;
import com.sifang.modules.sunshine.partnerManagement.vo.PartnerStaffVo;
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.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(value = "${adminPath}/partnerStaff")
public class PartnerStaffController extends BaseController {

    @Autowired
    private PartnerStaffService partnerStaffService;
    @Autowired
    private SystemService systemService;

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

        return "modules/partner/traderEmploye";
    }

    /**
     * 获取服务商员工设置列表数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getPartnerEmployeeData")
    @ResponseBody
    public String getPartnerEmployeeData(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user = UserUtils.getUser();
        String traderId = partnerStaffService.getPartnerId(user.getId());
        List<Record> members = partnerStaffService.getPartnerEmployeData(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)) {
            CrmPartnerStaff emp = partnerStaffService.getById(id);
            model.addAttribute("emp", emp);
            model.addAttribute("user",systemService.getUser(emp.getUserId()));
        }

        return "modules/partner/addEmploye";
    }

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


    /**
     * 跳转修改员工信息页面
     *
     * @return
     */
    @RequestMapping(value = "updateEmployee")
    public String updateEmployee(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        int imgsCount = 0;
        String traderId = partnerStaffService.getPartnerId(UserUtils.getUser().getId());
        Record rd = partnerStaffService.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/partner/updateEmployee";
    }

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

    /**
     * 添加保存工程师
     * @param request
     * @return
     */
    @RequestMapping(value = "employeSave")
    @ResponseBody
    public Object siteSave(HttpServletRequest request, PartnerStaffVo staffVo){
        return partnerStaffService.saveTraderEmploye(staffVo);
    }


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


}
