package com.sptg.gateway.controller.estate;


import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.BillsDto;
import com.sptg.estate.api.entity.Bills;
import com.sptg.estate.api.feigns.BillsFeign;
import com.sptg.estate.api.vo.BillsVO;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.feigns.ConfFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/bills/manage")
@RequiredArgsConstructor
public class BillsController {
    private final ConfFeign confFeign;
    private final BillsFeign billsFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;



    /**
     *    返回账单管理页面视图
     * */
    @Security(token = EstateSecurityToken.ESTATE_BILLS_MANAGER)
    @GetMapping("/manage")
    public ModelAndView managePage(){
        ModelAndView modelAndView = new ModelAndView("estate/bills-manage");
        return modelAndView;
    }

    /**
     *    返回账单详情页面视图
     * */
    @Security(token = EstateSecurityToken.ESTATE_BILLS_MANAGER)
    @GetMapping("/detailPage")
    public ModelAndView detailPage(@RequestParam("id") Long id){
        ModelAndView modelAndView = new ModelAndView("estate/bills-detail");
        modelAndView.addObject("bills",billsFeign.detail(id));
        return modelAndView;
    }

    /**
     *    返回账单编辑页面视图
     * */
    @Security(token = EstateSecurityToken.ESTATE_BILLS_EDIT)
    @GetMapping("/editPage")
    public ModelAndView editPage(@RequestParam("id") Long id){
        ModelAndView modelAndView = new ModelAndView("estate/bills-edit");
        modelAndView.addObject("bills",billsFeign.detail(id));
        return modelAndView;
    }

    /**
     *    返回新增账单页面视图
     * */
    @Security(token = EstateSecurityToken.ESTATE_BILLS_ADD)
    @GetMapping("/addPage")
    public ModelAndView addPage(BillsDto bills){
        ModelAndView modelAndView = new ModelAndView("estate/bills-add");
        modelAndView.addObject("bills",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     *  获取账单分页列表
     * */
    @Security(token = EstateSecurityToken.ESTATE_BILLS_MANAGER)
    @GetMapping("/list")
    public ResultBean<Page<Bills>> list(@RequestParam(value = "page") Integer current,
                                        @RequestParam(value = "limit") Integer size,
                                        Bills bills){
        System.out.println(current);
        System.out.println(size);
        return ResultBean.ok(billsFeign.list(current,size,bills));
    }


    /**
     * 账单分页
     */
    @GetMapping(value = "/page")
    @Security(token = EstateSecurityToken.ESTATE_BILLS_MANAGER,format = DataFormatEnum.JSON)
    public ResultBean<Page<Bills>> page(BillsDto dto, Page<Bills> page){
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        dto.setPage(page);
        return this.billsFeign.page(dto);
    }

    /**
     *  获取账单分页列表
     * */
//    @Security(token = EstateSecurityToken.ESTATE_BILLS_MANAGER)
//    @GetMapping("/page")
//    public ResultBean<Page<Bills>> page(@RequestParam(value = "page") Integer page,
//                                        @RequestParam(value = "limit") Integer limit,
//                                        Bills bills){
//        return ResultBean.ok(billsFeign.list(page,limit,bills));
//    }

    /**
     *  获取账单详情
     * */
    @Security(token = EstateSecurityToken.ESTATE_BILLS_MANAGER)
    @GetMapping("/detail")
    public BillsVO detail(@RequestParam(value = "id") Long id){
        return billsFeign.detail(id);
    }

    /**
     * 根据id删除账单
     * */
    @Security(token = EstateSecurityToken.ESTATE_BILLS_DELETE,format = DataFormatEnum.JSON)
    @PostMapping("/delete")
    public ResultBean<Boolean> delete(@RequestBody BillsDto billsDto){
        return billsFeign.delete(billsDto);
    }

    /**
     * 编辑账单
     * */
    @PostMapping("/edit")
    @Security(token = EstateSecurityToken.ESTATE_BILLS_EDIT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> edit(@RequestBody BillsDto billsDto){
        return this.billsFeign.edit(billsDto);
    }

    /**
     * 新增账单
     * */
    @PostMapping("/add")
    @Security(token = EstateSecurityToken.ESTATE_BILLS_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> add(@RequestBody BillsDto billsDto){
//        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        return this.billsFeign.add(billsDto);
    }
}
