package com.jwipc.finance.controller;


import cn.hutool.core.date.DateUtil;
import com.jwipc.finance.convert.ReceiptVoucherConvert;
import com.jwipc.finance.dto.PaymentPlanDTO;
import com.jwipc.finance.logic.PaymentPlanDetailLogic;
import com.jwipc.finance.logic.PaymentPlanLogic;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.client.DictClient;
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 java.util.Date;

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

/**
 * <p>
 *     对账模块-对账单 前端控制器
 * </p>
 *
 * @author JT
 * @since 2020-07-31
 */
@Controller
@RequestMapping(MODULE_ROOT + "/payment/plan")
@RequiredArgsConstructor
public class PaymentPlanPageController {
    private final PaymentPlanLogic paymentPlanLogic;
    private final PaymentPlanDetailLogic paymentPlanDetailLogic;
    private final SupplierClient supplierClient;
    private final DictClient dictClient;
    private final ReceiptVoucherConvert receiptVoucherConvert;
    /**
     * 采购对账单管理
     *
     * @param model model
     * @return 页面
     */
    @GetMapping("/purchase/index")
    public String purchaseIndex(Model model,String status){
        // 角色类型 1:采购角色 2:财务角色
        model.addAttribute("status", status);
        model.addAttribute("roleType", 1);
        model.addAttribute("userId", LoginUtil.getUserId());
        return MODULE_ROOT + "/payment/plan/payment_plan_index_purchase";
    }

    /**
     * 打印开票单据-财务到票确认
     * @param id 开票id
     * @return
     */
//    @GetMapping("/print")
//    public String printStatement(Long id, Model model){
//
//        Map<String,Object> map = PaymentPlanLogic.printStatement(id);
//        for(String key:map.keySet()){
//            model.addAttribute(key,map.get(key));
//        }
//
//        return MODULE_ROOT +  "/statement/statement_print3";
//    }

    /**
     * 财务对账单管理
     *
     * @param model model
     * @return 页面
     */
    @GetMapping("/finance/index")
    public String index(Model model){
        // 角色类型 1:采购角色 2:财务角色
        model.addAttribute("roleType", 2);
        model.addAttribute("userId", LoginUtil.getUserId());
//        return MODULE_ROOT +  "/statement/index_finance";
        return MODULE_ROOT + "/payment/plan/payment_plan_index_finance";
    }


    /**
     * 付款计划首页
     *
     * @param model model
     * @return 页面
     */
    @GetMapping("/index")
    public String materialIndex(Model model){
        if(!LoginUtil.isSupplier()){
            model.addAttribute("isSupplier", 0);
        }else {
            model.addAttribute("isSupplier", 1);
//            // 限制新增采购计划标识 0:不限制 1:限制
//            model.addAttribute("restrictFlag", vpsConfigService.checkSupRestrict(loginUser.getLoginName(), 1));
        }
        model.addAttribute("userId", LoginUtil.getUserId());
        return MODULE_ROOT + "/payment/plan/payment_plan_index";
    }

    /**
     * 对账单采购审批确认时选择扣款信息弹框
     *
     * @param model model
     * @return 页面
     */
//    @GetMapping("/chose/statement/dialog")
//    public String choseStatementDialog(Model model){
//        if(LoginUtil.isSupplier()){
//            model.addAttribute("supplierCode", LoginUtil.getAccount());
//        }
//        model.addAttribute("isSupplier", LoginUtil.isSupplier()?1:0);
//        model.addAttribute("userId", LoginUtil.getUserId());
//        return MODULE_ROOT + "/statement/_chose_statement";
//    }

    /**
     * 对账单采购审批确认时选择扣款信息弹框
     *
     * @param model model
     * @return 页面
     */
//    @GetMapping("/chose/statement/with/apply/dialog")
//    public String choseStatementWithApplyDialog(Model model,String companyCode,String supplierCode,String currencyCode){
//        model.addAttribute("supplierCode", supplierCode);
//        model.addAttribute("companyCode", companyCode);
//        model.addAttribute("currencyCode", currencyCode);
//        return MODULE_ROOT + "/statement/_chose_statement_with_apply";
//    }

    /**
     * 对账单新增页面
     *
     * @param model model
     * @param optType 操作类型；0-新增；
     * @return 页面
     */
    @GetMapping("/newPaymentPlan")
    public String newPaymentPlan(Model model){
        PaymentPlanDTO data = new PaymentPlanDTO();
        data.setPaymentPlanYear(DateUtil.format(new Date(),"yyyy"));
        data.setPaymentPlanMonth(DateUtil.format(DateUtil.offsetMonth(new Date(),0),"MM"));
        model.addAttribute("data", data);
        return MODULE_ROOT + "/payment/plan/payment_plan_add";
    }

    /**
     * 对账单修改页面
     *
     * @param model model
     * @param optType 操作类型；1-编辑；2-查看；3-主管确认；4-财务确认
     * @return 页面
     */
    @AuditLogInter(value = "详情数据【optType：1编辑、2查看、 3采购主管确认、4财务确认】", module = "付款计划")
    @GetMapping("/detail")
    public String detail(Model model, Integer optType, Long id){
        PaymentPlanDTO data = new PaymentPlanDTO();
        if (id!=null){
            data = paymentPlanLogic.getById(id);
//            data.setDifferenceAmount(data.getTaxAmount().subtract(data.getNotTaxAmount()));
//            model.addAttribute("inStockStartDate", DateUtil.format(data.getInStockStartDate(),"yyyy-MM-dd"));
//            model.addAttribute("inStockEndDate", DateUtil.format(data.getInStockEndDate(),"yyyy-MM-dd"));
        }else {
            data.setPaymentPlanYear(DateUtil.format(new Date(),"yyyy"));
            data.setPaymentPlanMonth(DateUtil.format(DateUtil.offsetMonth(new Date(),0),"MM"));
        }
        model.addAttribute("data", data);
        model.addAttribute("paymentTypeDesc", (data.getPlanType()!=null&&data.getPlanType().equals("Y"))?"区分预付和月结":"不区分预付和月结");
        //操作类型
        model.addAttribute("optType", optType);
        model.addAttribute("lookFlag", optType==2||optType==3||optType==4);
//        model.addAttribute("isSupplier", LoginUtil.isSupplier() ? 1 : 0);
        return MODULE_ROOT + "/payment/plan/payment_plan_detail";
    }

    @AuditLogInter(value = "查看明细数据", module = "付款计划")
    @GetMapping("/look")
    public String look(Model model, Integer optType, Long id){
        PaymentPlanDTO data = new PaymentPlanDTO();
        if (id!=null){
            data = paymentPlanLogic.getById(id);
        }else {
            data.setPaymentPlanYear(DateUtil.format(new Date(),"yyyy"));
            data.setPaymentPlanMonth(DateUtil.format(DateUtil.offsetMonth(new Date(),0),"MM"));
        }
        model.addAttribute("data", data);
        model.addAttribute("paymentTypeDesc", (data.getPlanType()!=null&&data.getPlanType().equals("Y"))?"区分预付和月结":"不区分预付和月结");
        model.addAttribute("optType", optType);

        return MODULE_ROOT + "/payment/plan/payment_plan_look";
    }

    @AuditLogInter(value = "查看汇总数据", module = "付款计划")
    @GetMapping("/summary")
    public String summary(Model model, Long id){
        PaymentPlanDTO data = new PaymentPlanDTO();
        if (id!=null){
            data = paymentPlanLogic.getById(id);
        }else {
            data.setPaymentPlanYear(DateUtil.format(new Date(),"yyyy"));
            data.setPaymentPlanMonth(DateUtil.format(DateUtil.offsetMonth(new Date(),0),"MM"));
        }
        model.addAttribute("data", data);
        model.addAttribute("paymentTypeDesc", (data.getPlanType()!=null&&data.getPlanType().equals("Y"))?"区分预付和月结":"不区分预付和月结");
        return MODULE_ROOT + "/payment/plan/payment_plan_summary";
    }


    /**
     * 所有对账单列表(供应商对账单)
     *
     * @param model model
     * @return 页面
     */
    @GetMapping("/index/all")
    public String indexAll(Model model){
        if(!LoginUtil.isSupplier()){
            model.addAttribute("isSupplier", 0);
        }else {
            model.addAttribute("isSupplier", 1);
        }
        return MODULE_ROOT + "/payment/plan/payment_plan_all";
    }



    /**
     * 对账管理中选择返利信息弹框
     *
     * @param model model
     * @param companyCode 公司编码
     * @param supplierCode 供应商编码
     * @param selectedRmId 已选择的返利记录ID
     * @param tempDelRmId 临时删除的返利记录ID
     * @return 页面
     */
//    @GetMapping("/chose/rebate/dialog")
//    public String choseRebateDialog(Model model, String companyCode, String supplierCode, String selectedRmId, String tempDelRmId){
//        model.addAttribute("companyCode", companyCode);
//        model.addAttribute("supplierCode", supplierCode);
//        model.addAttribute("selectedRmId", selectedRmId);
//        model.addAttribute("tempDelRmId", tempDelRmId);
//        return MODULE_ROOT + "/statement/_chose_rebate";
//    }


    /**
     * 对账单采购审批确认时选择扣款信息弹框
     *
     * @param model model
     * @param companyCode 公司编码
     * @param supplierCode 供应商编码
     * @param selectedDmId 已选择的返利记录ID
     * @param tempDelDmId 临时删除的返利记录ID
     * @return 页面
     */
//    @GetMapping("/chose/deduction/dialog")
//    public String choseDeductionDialog(Model model, String companyCode, String supplierCode, String selectedDmId, String tempDelDmId){
//        model.addAttribute("companyCode", companyCode);
//        model.addAttribute("supplierCode", supplierCode);
//        model.addAttribute("selectedDmId", selectedDmId);
//        model.addAttribute("tempDelDmId", tempDelDmId);
//        return MODULE_ROOT + "/statement/_chose_deduction";
//    }


    /**
     * 对账单编辑时选择入库凭证数据
     *
     * @param model model
     * @param supplierCode 供应商编码
     * @param currencyCode 货币代码
     * @param inStockStartDate 入库开始时间
     * @param inStockEndDate 入库结束时间
     * @param alreadyExistId 已选择的凭证ID
     * @param asId 对账单ID
     * @return 页面
     */
//    @GetMapping("/chose/voucher/dialog")
//    public String choseVoucherData(
//            Model model, String companyCode,String taxCode, String factoryCode, String supplierCode, String currencyCode,
//            String inStockStartDate, String inStockEndDate, String alreadyExistId, Long asId,String purchaseGroup,Integer voucherType
//    ){
//        model.addAttribute("companyCode", companyCode);
//        model.addAttribute("taxCode", taxCode);
//        model.addAttribute("factoryCode", factoryCode);
//        model.addAttribute("supplierCode", supplierCode);
//        model.addAttribute("currencyCode", currencyCode);
//        model.addAttribute("inStockStartDate", inStockStartDate);
//        model.addAttribute("inStockEndDate", inStockEndDate);
//        model.addAttribute("alreadyExistId", alreadyExistId);
//        model.addAttribute("asId", asId);
//        model.addAttribute("purchaseGroup", purchaseGroup);
//        model.addAttribute("voucherType", voucherType);
//        return MODULE_ROOT + "/statement/_chose_voucher";
//    }

    /**
     *xiahs
     */
//    @GetMapping("/purchase/salesContractsIndex")
//    public String salesContractsIndex(Model model){
//        model.addAttribute("userId", LoginUtil.getUserId());
//        return MODULE_ROOT + "/statement/index_sales_contract";
//    }
}
