package com.sifang.modules.contract.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.contract.service.ContractTemplateService;
import com.sifang.modules.esign.cn.tsign.hz.exception.EsignDemoException;
import com.sifang.modules.esign.service.ESignFileService;
import com.sifang.modules.esign.vo.ContractTemplateVo;
import com.sifang.modules.esign.vo.FileVo;
import com.sifang.modules.flowpath.service.DistributionGoodsApplyService;
import com.sifang.modules.sunshine.SystemSetting.service.ProjectCompanyService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.partnerManagement.service.PartnerService;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "${adminPath}/contractTemplate")
public class ContractTemplateController extends BaseExceptionController {

    Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private ContractTemplateService templateService;

    @Autowired
    private ProjectCompanyService projectCompanyService;

    @Autowired
    PartnerService partnerService;


    @RequestMapping(value = "templateSetListPage")
    public String templateSetList(Model model){
        return "modules/contract/templateSetListPage";
    }

    //列表数据
    @ResponseBody
    @RequestMapping(value = "listData")
    public String listData(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        templateService.contractTemplateData(page,map);
        return page.renderJson();
    }

    @RequestMapping(value = "addTemplatePage")
    public String addTemplatePage(String type,Model model){
        model.addAttribute("type",type);
        List company=projectCompanyService.getProjectCompany();
        model.addAttribute("company",company);
        model.addAttribute("paterns", partnerService.partnerList());
        return "modules/contract/addTemplatePage";
    }

    @ResponseBody
    @RequestMapping(value = "getTemplateUploadUrl")
    public Result getTemplateUploadUrl(@RequestParam MultipartFile file){
        String filename= file.getOriginalFilename();
        logger.info(">>>>>>>>>>>>> fileName:{}", filename);
        //上传文件
        try {
            FileVo fileVo= ESignFileService.getUploadUrl(file);
            fileVo.setName(filename);
            return Result.success(fileVo);
        } catch (EsignDemoException e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>getTemplateUploadUrl e:{}",e);
            return Result.fail(e.getMessage());
        } catch (IOException ioException) {
            ioException.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>getTemplateUploadUrl ioException:{}",ioException);
            return Result.fail(ioException.getMessage());
        }
    }

    //新建合同
    @ResponseBody
    @RequestMapping(value ="addTemplateUrl")
    public Result getUploadUrl(@RequestParam String fileId, @RequestParam String name,@RequestParam String type,@RequestParam String companyId, @RequestParam String patern,@RequestParam String filingType){
        logger.info(">>>>>>>>>>>>>>>>>=======getUploadUrl name:{},fileId:{},type:{},companyId:{}",name,fileId,type,companyId);
        try {
            //先判断该代理商是否有合同模板，如果有那么就不能在添加了
            if(templateService.checkTemplate(companyId,type,patern,filingType)){
                return Result.fail("已有合同模板，请勿重复添加!");
            }
            //获取制作合同模板页面
            FileVo fileVo1= templateService.saveContractTemplate(fileId, name,type,companyId,patern,filingType);
            return Result.success(fileVo1);
        } catch (EsignDemoException e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>getUploadUrl e:{}",e);
            return Result.fail(e.getMessage());
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>getUploadUrl Exception:{}",ex);
            return Result.fail(ex.getMessage());
        }
    }

    //编辑合同
    @ResponseBody
    @RequestMapping(value = "editContractTemplate")
    public Result editContractTemplate(HttpServletRequest request){
        String docTemplateId=request.getParameter("docTemplateId");
        try {
            Map<String,String> map=new HashMap<>();
            String docTemplateEditUrl=ESignFileService.getTemplateEditUrl(docTemplateId);
            map.put("docTemplateEditUrl", docTemplateEditUrl);
            return Result.success(map);
        } catch (EsignDemoException e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>editContractTemplate e:{}",e);
            return Result.fail(e.getMessage());
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>>>editContractTemplate Exception:{}",ex);
            return Result.fail(ex.getMessage());
        }
    }

    //删除合同
    @ResponseBody
    @RequestMapping(value = "deleteContractTemplate")
    public Result deleteContractTemplate(HttpServletRequest request){
        String id=request.getParameter("id");
        templateService.deleteContractTemplate(id);
        return Result.ok();
    }

    //查看合同
    @ResponseBody
    @RequestMapping(value = "lookContractTemplateDetail")
    public Result lookContractTemplateDetail(HttpServletRequest request){
        String docTemplateId=request.getParameter("docTemplateId");
        Result result=new Result();
        try {
            ContractTemplateVo templateVo=ESignFileService.getComponentsInfo(docTemplateId);
            result.setData(templateVo.getFileDownloadUrl());
            result.setOk();
            return result;
        } catch (EsignDemoException e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>lookContractTemplateDetail  e:{}",e);
            return Result.fail(e.getMessage());
        }

    }

    //签章位置设置
    @ResponseBody
    @RequestMapping(value = "positionSet")
    public Result positionSet(String id,Integer positionPage,Integer positionAcross,Integer positionVertical){
        templateService.positionSet(id,positionPage,positionAcross,positionVertical);
        return  Result.ok();
    }
    //华融合同位置设置
    @ResponseBody
    @RequestMapping(value = "positionSet2")
    public Result positionSet2(String id,Integer positionPage,Integer positionAcross,Integer positionVertical,Integer positionPage2,Integer positionAcross2,Integer positionVertical2){
        templateService.positionSet2(id,positionPage,positionAcross,positionVertical,positionPage2,positionAcross2,positionVertical2);
        return  Result.ok();
    }
}
