package com.loan.system.controller.wechat;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.server.HttpServerResponse;
import com.loan.system.domain.dto.DocumentDTO;
import com.loan.system.domain.entity.Contract;
import com.loan.system.domain.entity.Document;
import com.loan.system.domain.pojo.ContractInformation;
import com.loan.system.utils.PoiWordUtil;
import com.loan.system.domain.enums.StepEnum;
import com.loan.system.domain.enums.StepPropertyEnum;
import com.loan.system.domain.pojo.Result;
import com.loan.system.domain.vo.ContractVO;
import com.loan.system.domain.vo.StepVO;
import com.loan.system.service.ContractService;
import com.loan.system.service.DocumentService;
import com.loan.system.service.StepService;
import com.loan.system.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/wechat/contract")
@Api(tags = "合同签订接口")
public class ContractController {
    @Value("${upload.templatePath}")
    private String contractPath;

    @Autowired
    private ContractService contractService;
    @Autowired
    private StepService stepService;
    @Autowired
    private DocumentService documentService;

    @GetMapping("/{caseId}")
    @ApiOperation("显示所有合同详情（内部人员）")
    public Result findContractsDetails(@PathVariable("caseId") Long caseId){

        List<ContractVO> contracts = contractService.findContractByCaseId(caseId);

        return ResultUtil.success("success",contracts);
    }

    @PostMapping("/{id}")
    @ApiOperation("推送合同")
    public Result pushContract(@PathVariable("id") Long contractId){
        contractService.updateIsPushById(contractId);
        return ResultUtil.success("success");
    }

    @GetMapping("/case/{caseId}")
    @ApiOperation("显示所有合同详情（客户）")
    public Result findContractsByCustomer(@PathVariable("caseId") Long caseId, HttpServletResponse response){
        //显示已经推送过的合同
        List<ContractVO> contracts = contractService.findContractByCaseIdAndIsPush(caseId,true);
        String downloadName = UUID.randomUUID().toString();
        ContractInformation contractInformation = new ContractInformation();

        PoiWordUtil.writeApprove(response,contractPath,contractInformation,downloadName);

        return ResultUtil.success("success",contracts);
    }
    //签署的电子信息先以附件上传
    //再修改合同状态
    @PutMapping("/{id}")
    @ApiOperation("签署合同(客户)")
    public Result updateContract(@PathVariable("id")Long contractId,@RequestParam Long signId,@RequestParam Long commitedId){
        contractService.updateContractById1(contractId,commitedId,signId);
        return ResultUtil.success("success");
    }

    @PutMapping("/case/{caseId}")
    @ApiOperation("合同签订完成")
    public Result completeContract(@PathVariable("caseId")Long caseId){
        //合同签约完成
        stepService.updateStatusByCaseId(StepEnum.COMPLETED.getMsg(), StepPropertyEnum.CONTRACT_SIGN_PARENT.getLabel(),caseId);
        stepService.updateStatusByCaseId(StepEnum.COMPLETED.getMsg(),StepPropertyEnum.CONTRACT_SIGN.getLabel(),caseId);

//        //出款开始环节
//        stepService.updateStatusByCaseId(StepEnum.PROCESS.getMsg(),StepPropertyEnum.DISBURSE_PARENT.getLabel(),caseId);
//        stepService.updateStatusByCaseId(StepEnum.PROCESS.getMsg(),StepPropertyEnum.PLAN_REPORT.getLabel(),caseId);

//        //回款开始环节
//        stepService.updateStatusByCaseId(StepEnum.PROCESS.getMsg(),StepPropertyEnum.REPAY_PARENT.getLabel(),caseId);
//        stepService.updateStatusByCaseId(StepEnum.PROCESS.getMsg(),StepPropertyEnum.REPAY_START.getLabel(),caseId);
//
//        //取证开始环节
//        stepService.updateStatusByCaseId(StepEnum.PROCESS.getMsg(),StepPropertyEnum.COLLATERAL_RECEIVE.getLabel(),caseId);
//        stepService.updateStatusByCaseId(StepEnum.PROCESS.getMsg(),StepPropertyEnum.PLAN_SUBMISSION.getLabel(),caseId);
//
//        //送证开始环节
//        stepService.updateStatusByCaseId(StepEnum.PROCESS.getMsg(),StepPropertyEnum.COLLATERAL_DELIVERY.getLabel(),caseId);
//        stepService.updateStatusByCaseId(StepEnum.PROCESS.getMsg(),StepPropertyEnum.PLAN_SUBMISSION_2.getLabel(),caseId);

        return ResultUtil.success("success");
    }




}
