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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.fittingManager.entity.PlatformFitting;
import com.sifang.modules.sunshine.SystemSetting.dao.EmployeInformationDao;
import com.sifang.modules.sunshine.SystemSetting.dao.SiteInformationDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmSiteEmploye;
import com.sifang.modules.sunshine.SystemSetting.service.EmployeInformationService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.fittingManager.dao.CrmSiteFittingDao;
import com.sifang.modules.sunshine.fittingManager.entity.CrmPlantformFittingApply;
import com.sifang.modules.sunshine.fittingManager.entity.CrmSiteFitting;
import com.sifang.modules.supply.order.constant.ApplyOrderConstants;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.dao.UserDao;
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(value = "${adminPath}/employeInformation")
public class EmployeInformationController extends BaseController {
    @Autowired
    EmployeInformationService employeInformationService;
    @Autowired
    EmployeInformationDao employeInformationDao;
    @Autowired
    UserDao userDao;
    @Autowired
    SiteInformationDao siteInformationDao;
    @Autowired
    CrmSiteFittingDao crmSiteFittingDao;

    @RequestMapping(value = "employeList")
    public String siteDataList(HttpServletRequest request, Model model){
        String siteId = request.getParameter("id");
        List<Record> siteList =siteInformationDao.getSiteList();
        model.addAttribute("siteList", siteList);
        model.addAttribute("siteId", siteId);
        return "modules/yangguang/systemSetting/siteManage/employeList";
    }

    /**
     * 获取工程师信息列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "siteEmploye")
    public Object getSiteEmploye(HttpServletRequest request){
        String siteId = request.getParameter("site");
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map= getParams(request);
        if(StringUtil.isNotEmpty(siteId)){
            map.put("siteId",siteId);
        }
        employeInformationService.getSiteEmploye(page,map);
        return page.renderJson();
    }


    /**
     * 跳转添加服务商页面
     * @param request
     * @return
     */
    @RequestMapping(value = "addEmploye")
    public String addSite(HttpServletRequest request, Model model){
        String id = request.getParameter("id");
        int imgsCount=0;
        List<Record> siteList =siteInformationDao.getSiteList();
        if (StringUtils.isNotBlank(id)) {
            CrmSiteEmploye emp = employeInformationService.getById(id);
            if (StringUtils.isNotBlank(emp.getLicenseImg())) {
                String[] imgArr = emp.getLicenseImg().split(",");
                imgsCount = imgArr.length;
                List<String> imglist = new ArrayList<>();
                for (String img:imgArr) {
                    imglist.add(img);
                }
                model.addAttribute("imglist", imglist);
            }
            User user = userDao.get(emp.getUserId());
            model.addAttribute("emp", emp);
            model.addAttribute("user", user);
        }
        model.addAttribute("imgsCount", imgsCount);
        model.addAttribute("siteList", siteList);
        return "modules/yangguang/systemSetting/siteManage/addEmploye";
    }

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

    /**
     * 保存诚信二维码状态
     * @param request
     * @return
     */
    @RequestMapping(value = "saveQrcode")
    @ResponseBody
    public String saveQrcode(HttpServletRequest request){
        String ids = request.getParameter("ids");
        return employeInformationService.saveQrcode(ids);
    }

    /**
     * 删除工程师
     * @param request
     * @return
     */
    @RequestMapping(value = "deleteEmploye")
    @ResponseBody
    public Object deleteSite(HttpServletRequest request){
        String id = request.getParameter("id");
        return employeInformationService.deleteEmploye(id);
    }

    /**
     * 未审核列表页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "noAudit")
    public String noAuditList(HttpServletRequest request, Model model){
        String siteId = CrmUtils.getSiteId(UserUtils.getUser());
        List<Record> empList =employeInformationDao.getEmp(siteId);
        model.addAttribute("empList", empList);
        return "modules/yangguang/fittingManager/empApply/noAudit";
    }

    /**
     * 未审核列表页数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "noAuditList")
    public Object noAuditList(HttpServletRequest request){
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map= getParams(request);
        employeInformationService.noAuditList(page,map);
        return page.renderJson();
    }

    /**
     * 工程师申请审核页面
     * @param request
     *
     */
    @RequestMapping(value = "applyToExamineForm")
    public String  applyToExamineForm(HttpServletRequest request,Model model){
        String applyId = request.getParameter("applyId");
        Record apply = employeInformationDao.getEmpApplyDetail(applyId);
        CrmSiteFitting fitting = crmSiteFittingDao.get(apply.getStr("fitting_id"));
        if (apply.getStr("warranty_type").equals(ApplyOrderConstants.WARRANTY_TYPE_ONE)) {
            apply.set("warranty_type", "保内");
        } else  if(apply.getStr("warranty_type").equals(ApplyOrderConstants.WARRANTY_TYPE_TWO)){
            apply.set("warranty_type", "保外");
        }
        apply.set("createTime", DateUtils.formatDateTime(apply.getDate("ctime")));
        model.addAttribute("apply", apply);
        model.addAttribute("fitting", fitting);
        return "modules/yangguang/fittingManager/empApply/empApplyDetail";
    }

    /**
     * 审核通过
     */
    @ResponseBody
    @RequestMapping(value = "adoptApply")
    public Result adoptApply(HttpServletRequest request){
        String id = request.getParameter("id");
        String fittingAuditNum = request.getParameter("fittingSuditNum");
        String oldFittingFlag = request.getParameter("oldFittingFlag");
        String factoryMarks = request.getParameter("factoryMarks");
        if(StringUtil.isBlank(id)){
            return Result.fail("202","id数据有误");
        }
        try {
            return employeInformationService.adoptApply(id,fittingAuditNum,oldFittingFlag,factoryMarks);
        }catch (Exception e ){
            e.printStackTrace();
            return Result.fail("205","审核失败");
        }
    }

    /**
     * 审核拒绝
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "refuseFitting")
    public Result refuseFitting(HttpServletRequest request){
        String id = request.getParameter("id");
        String oldFittingFlag = request.getParameter("oldFittingFlag");
        String factoryMarks = request.getParameter("factoryMarks");
        String refuseReason = request.getParameter("refuseReason");
        if(StringUtil.isBlank(id)){
            return Result.fail("202","id数据有误");
        }
        try {
            return employeInformationService.refuseFitting(id,refuseReason,oldFittingFlag,factoryMarks);
        }catch (Exception e ){
            e.printStackTrace();
            return Result.fail("205","审核失败");
        }
    }

    /**
     * 已审核列表页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "audited")
    public String AuditedList(HttpServletRequest request, Model model){
        String siteId = CrmUtils.getSiteId(UserUtils.getUser());
        List<Record> empList =employeInformationDao.getEmp(siteId);
        model.addAttribute("empList", empList);
        return "modules/yangguang/fittingManager/empApply/audited";
    }

    /**
     * 未审核列表页数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "auditedList")
    public Object auditedList(HttpServletRequest request){
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map= getParams(request);
        employeInformationService.auditedList(page,map);
        return page.renderJson();
    }


}
