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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.site.siteInfo.service.SiteCompanyInfoService;
import com.sifang.modules.sunshine.SystemSetting.entity.SiteReceiptInformation;
import com.sifang.modules.sunshine.SystemSetting.service.SiteReceiptInformationService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.web.BaseController;
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.List;

@Controller
@RequestMapping("${adminPath}/siteCompany")
public class SiteCompanyController extends BaseController {

    @Autowired
    private SiteCompanyInfoService siteCompanyInfo;
    @Autowired
    private SiteReceiptInformationService receService;

    @Autowired
    AddressService addressService;

    /**
     * 跳转服务商信息--公司资料页面
     *
     * @return
     */
    @RequestMapping(value = "companyInfo")
    public String announcementList(HttpServletRequest request, Model model) {
        //获取服务商信息列表数据
        List<Record> province = addressService.getProvinces();//获取所有的省
        if (province.size() > 0) {
            model.addAttribute("province", province);
        }
        User user = UserUtils.getUser();
        /* String userId = user.getId();*/
        Record rd = siteCompanyInfo.getSiteCompanyData(user);

        if (rd != null) {
            String img = rd.getStr("license_img");
            String[] images = img.split(",");
            model.addAttribute("data", rd);
            List<Record> city = AddressUtils.getCityByProName(rd.getStr("province"));
            model.addAttribute("city", city);
            List<Record> area = AddressUtils.getAreaByCityName(rd.getStr("city"));
            model.addAttribute("area", area);
            model.addAttribute("images", images);
        }
        String siteId = CrmUtils.getSiteId(user);
        SiteReceiptInformation rece = receService.getBySiteId(siteId);
        model.addAttribute("receipt", rece);
        model.addAttribute("siteId", siteId);
        model.addAttribute("userId", user.getId());
        return "modules/site/siteInfo/companyInfo";
    }

    /**
     * 修改资料保存
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "saveBtn")
    @ResponseBody
    public Object saveBtn(HttpServletRequest request) {
        return siteCompanyInfo.saveBtn(request);
    }

    /**
     * 服务商修改密码
     */
    @RequestMapping(value = "updateSitePasswork")
    @ResponseBody
    public Object updateSitePasswork(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String siteId = request.getParameter("siteId");
        String passwork = request.getParameter("newPassword");
        String primaryPassword = request.getParameter("primaryPassword");//原密码
        return siteCompanyInfo.uodateSitePasswork(userId, primaryPassword, passwork);
    }


}
