package com.jwipc.sys.controller.pc;

import com.jwipc.security.LoginUtil;
import com.jwipc.sys.dto.PurchaseGroupDTO;
import com.jwipc.sys.dto.UserEditPageDTO;
import com.jwipc.sys.dto.UserPageAttrDTO;
import com.jwipc.sys.logic.PurchaseGroupLogic;
import com.jwipc.sys.logic.UserLogic;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping(MODULE_ROOT + "/user")
@RequiredArgsConstructor
public class UserPageController {
    private final UserLogic userLogic;
    private final PurchaseGroupLogic purchaseGroupLogic;

    @GetMapping("index")
    public String index(Model model, @RequestParam(required = false, defaultValue = "0") Integer select,
                        @RequestParam(defaultValue = "0", required = false) Integer single,
                        @RequestParam(defaultValue = "", required = false) String roleCodes,
                        @RequestParam(defaultValue = "", required = false) String purchaseGroupCode,
                        @RequestParam(defaultValue = "", required = false) String purchaseGroupId,
                        @RequestParam(defaultValue = "", required = false) String supplierFlag

    )
    {
        final UserPageAttrDTO userPageAttrDTO = userLogic.renderHanlder();
        PurchaseGroupDTO purchaseGroupDTO = purchaseGroupLogic.getByCode(purchaseGroupCode);
//        if (supplierFlag == "1") {
//            String purchaseGroupName = purchaseGroupDTO.getPurchaseGroupName();
//            model.addAttribute("purchaseGroupName", purchaseGroupName);
//        }
        model.addAttribute("pgList", userPageAttrDTO.getPurchaseGroups());
        model.addAttribute("roleList", userPageAttrDTO.getRoles());
        model.addAttribute("select", select);
        model.addAttribute("single", single);
        model.addAttribute("supplierFlag", supplierFlag);
        model.addAttribute("purchaseGroupCode", purchaseGroupCode);
        model.addAttribute("purchaseGroupId", purchaseGroupId);
        model.addAttribute("roleCodes", roleCodes);
        return MODULE_ROOT + "/user/list";
    }

    @GetMapping("supplier")
    public String supplierUser(Model model) {
        final UserPageAttrDTO userPageAttrDTO = userLogic.renderHanlder();
        model.addAttribute("pgList", userPageAttrDTO.getPurchaseGroups());
        model.addAttribute("roleList", userPageAttrDTO.getRoles());
        return MODULE_ROOT + "/user/supplier_user";
    }


    @GetMapping("add")
    public String add(Model model, @RequestParam(defaultValue = "0", required = false) Integer userType) {
        model.addAttribute("userType", userType);
        return MODULE_ROOT + "/user/add";
    }

    @GetMapping("edit")
    public String edit(Model model, Long id, @RequestParam(defaultValue = "0", required = false) Integer userType) {
        model.addAttribute("userType", userType);
        model.addAttribute("user", userLogic.getForEditPage(id));
        return MODULE_ROOT + "/user/edit";
    }

    @GetMapping("currentUserDetail")
    public String currentUserDetail(Model model) {
        UserEditPageDTO userDetail = userLogic.getForCurrentUserDetail();
        model.addAttribute("user", userDetail);
        model.addAttribute("userType", userDetail.getUserType());
        return MODULE_ROOT + "/user/detail";
    }

    @GetMapping("updatePasswordPage")
    public String updatePassword(Integer force, Model model) {
        model.addAttribute("id", LoginUtil.getUserId());
        model.addAttribute("pwdReg",userLogic.getPwdReg());
        return MODULE_ROOT + "/user/update_password";
    }

    @GetMapping("firstLoginUpdatePasswordPage")
    public String updatePassword(Model model) {
        model.addAttribute("pwdReg",userLogic.getPwdReg());
        return MODULE_ROOT + "/user/update_pwd_login_first";
    }
}
