package cn.palmte.work.controller.backend;

import cn.palmte.work.bean.FinalBean;
import cn.palmte.work.model.Project;
import cn.palmte.work.model.ProjectBudgetPlanDetail;
import cn.palmte.work.model.ProjectSettleIncome;
import cn.palmte.work.model.ProjectSettleIncomeRepository;
import cn.palmte.work.service.*;
import cn.palmte.work.utils.FreeMarkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.format.number.NumberStyleFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/project/final")
public class ProjectFinalController extends BaseController{

    @Autowired
    private ProjectFinalSevice projectFinalSevice;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectEstimateService projectEstimateService;

    @Autowired
    private ProjectBudgetService projectBudgetService;

    @Autowired
    private ProjectSettleIncomeRepository projectSettleIncomeRepository;

    @Autowired
    private ProjectSettleService projectSettleService;

    @RequestMapping("/add")
    public String add(@RequestParam("id") int id, Map<String, Object> model) {
        Project project = projectService.getProject(id);
        ProjectSettleIncome projectSettleIncome = projectSettleIncomeRepository.findNewByProjectId(id);
        String time = "";
        if(null != projectSettleIncome){
            time = projectSettleIncome.getTime();
        }

        List<ProjectBudgetPlanDetail> projectBudgetPlanDetails = projectBudgetService.getProjectBudgetPlanDetails(project);
        model.put("project", project);
        model.put("estimateBean", projectEstimateService.getEstimate(project));
        model.put("budgetBean", projectBudgetService.getBudget(project));
        model.put("settleBean",projectSettleService.getCurrentSettle(project, time));
        model.put("finalBean",new FinalBean());
        //现金表
        model.put("cashFlowBean", projectBudgetService.getCashFlowBean(project, projectBudgetPlanDetails));
        //freemarker可以利用的静态方法
        model.put("Utils", FreeMarkerUtil.fromStaticPackage("cn.palmte.work.utils.Utils"));
        return "admin/project_final_add";
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam("id") int id, Map<String, Object> model) {
        Project project = projectService.getProject(id);
        ProjectSettleIncome projectSettleIncome = projectSettleIncomeRepository.findNewByProjectId(id);
        String time = projectSettleIncome.getTime();
        List<ProjectBudgetPlanDetail> projectBudgetPlanDetails = projectBudgetService.getProjectBudgetPlanDetails(project);
        model.put("project", project);
        model.put("estimateBean", projectEstimateService.getEstimate(project));
        model.put("budgetBean", projectBudgetService.getBudget(project));
        model.put("settleBean",projectSettleService.getCurrentSettle(project, time));
        model.put("finalBean",projectFinalSevice.getFinal(project));
        //现金表
        model.put("cashFlowBean", projectBudgetService.getCashFlowBean(project, projectBudgetPlanDetails));
        //freemarker可以利用的静态方法
        model.put("Utils", FreeMarkerUtil.fromStaticPackage("cn.palmte.work.utils.Utils"));
        return "admin/project_final_edit";
    }

    @RequestMapping("/save")
    public String estimateAddSave(Project project, FinalBean finalBean, Map<String, Object> model) {
        projectFinalSevice.save(project,finalBean);
        return "redirect:/project/list";
    }

    /**
     * 保存并提交审批
     *
     * @param project
     * @param finalBean
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveAndApprove")
    public String saveAndApprove(Project project, FinalBean finalBean, Map<String, Object> model) throws Exception{
        projectFinalSevice.saveAndApprove(project,finalBean);
        return "redirect:/project/list";
    }

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        webDataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
        webDataBinder.addCustomFormatter(new NumberStyleFormatter());
    }
}
