package com.jwipc.tender.controller.pc;

import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.SysConfigClient;
import com.jwipc.sys.client.UserPurchaseGroupClient;
import com.jwipc.tender.constant.ModuleConst;
import com.jwipc.tender.logic.TenderLogic;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.constant.SysConfigKey.TENDER_AGREEMENT;
import static com.jwipc.tender.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>招标管理相关页面</p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequiredArgsConstructor
@Slf4j
@RequestMapping(ModuleConst.MODULE_ROOT + "/page")
public class TenderPageController {
    private final TenderLogic tenderLogic;
    private final UserPurchaseGroupClient userPurchaseGroupClient;
    private final SysConfigClient sysConfigClient;

    /**
     * 招标管理 列表页面
     *
     * @return
     */
    @GetMapping("index")
    public String index() {
        return MODULE_ROOT + "/index";
    }

    /**
     * 招标单 新增和编辑页面
     *
     * @param type 0:新增;1:编辑;2复制;3:发布,4:查看;5:审核
     * @return
     * @see com.jwipc.tender.constant.TenderOptType
     */
    @GetMapping("detail")
    public String detail(@RequestParam(required = false, defaultValue = "0") Long id, Model model,
                         @RequestParam(required = false, defaultValue = "0") Integer type)
    {
        //默认采购组
        model.addAttribute("pgc", userPurchaseGroupClient.getMarkPurchaseGroupCodeByUserId(LoginUtil.getUserId()));
        //默认公司
        model.addAttribute("company", LoginUtil.getDeptCode());
        //默认采购组织
        model.addAttribute("pgo", "8000");
        model.addAttribute("id", id);
        model.addAttribute("type", type);
        if (id == 0L) {
            model.addAttribute("agrement", sysConfigClient.getTextConfig(TENDER_AGREEMENT.getCode()));
        }
        return MODULE_ROOT + "/detail";
    }

    @GetMapping("agreement")
    public String bidAgreement(Long tenderId, Model model) {
        final String content = tenderLogic.getAgreement(tenderId);
        model.addAttribute("content", content);
        return MODULE_ROOT + "/agreement";
    }

    /**
     * 修改招标单时间 页面
     *
     * @return
     */
    @GetMapping("updateTime")
    public String updateTime(Long id, Model model) {
        model.addAttribute("time", tenderLogic.getTimeDTO(id));
        return MODULE_ROOT + "/update_time";
    }

    /**
     * 投标和确认情况查看 页面
     *
     * @return
     */
    @GetMapping("process")
    public String process(Long id, @RequestParam(required = false, defaultValue = "0") Integer type, Model model) {
        model.addAttribute("id", id);
        model.addAttribute("type", type);
        return MODULE_ROOT + "/process";
    }

    /**
     * 议价情况查看 页面
     *
     * @return
     */
    @GetMapping("bargainingProcess")
    public String bargainingProcess(Long id,  Model model) {
        model.addAttribute("id", id);
        return MODULE_ROOT + "/bargaining_process";
    }

    @GetMapping("zblistUI")
    public String zblistUI(Model model, String zbId, String optType) {
        model.addAttribute("zbId", zbId);
        model.addAttribute("optType", optType);
        return MODULE_ROOT + "/zblist";
    }
}
