var system = require("../../system")

class ecloudsignApi {
    constructor() {
        this.ecloudsignSev = system.getObject("service.ecloudsignSve");
        this.ecloudClient = system.getObject("util.ecloudClient");
        this.restClient = system.getObject("util.restClient");
        this.APIURL = "https://testapi.ecloudsign.com";//"https://api.ecloudsign.com";

    }

    //申请证书
    async applyCert(qobj) {
        var result = {};
        try {
            var params = await this.ecloudClient.rformat(qobj);
            params.info = qobj;
            var url = this.APIURL + "/ecs/account/applyCert.jspa";
            try {
                var rs = await this.restClient.execPost(params, url);
                if (rs && rs.stdout) {
                    var stdout = JSON.parse(rs.stdout) || {};
                    var code = stdout.code;

                    if (code == 0) {
                        code = 1;
                    } else if (code == 1) {
                        code = 0;
                    } else {
                    }
                    result.code = code;
                    result.msg = stdout.message;
                    result.data = stdout.data;
                } else {
                    result.code = 0;
                    result.msg = '申请证书接口返回空';
                }

            } catch (error) {
                result.code = 500;
                result.msg = '申请证书接口异常';
            }
        } catch (error) {
            result.code = 500;
            result.msg = '申请证书接口异常';
        }
        return result;
    }

    //添加签名/印章
    // mobilePhone	string	Y	用户手机
    // signImg	String	Y	签名/印章图片(base64编码)，建议用背景透明的png图片
    // signType	String	Y	类型：1签名 2印章
    async addSignOrSeal(qobj) {
        var result = {};
        try {
            var params = await this.ecloudClient.rformat(qobj);
            params.mobilePhone = qobj.mobilePhone;
            params.signImg = qobj.signImg;
            params.signType = 1;
            var url = this.APIURL + "/ecs/signInfo/addSign.jspa";
            try {
                var rs = await this.restClient.execPost(params, url);
                if (rs && rs.stdout) {
                    var stdout = JSON.parse(rs.stdout) || {};
                    var code = stdout.meta.code;

                    if (code == 0) {
                        code = 1;
                    } else if (code == 1) {
                        code = 0;
                    } else {
                    }
                    result.code = code;
                    result.msg = stdout.meta.message;
                    result.data = stdout.data;
                } else {
                    result.code = 0;
                    result.msg = '添加签名/印章接口返回空';
                }
            } catch (error) {
                result.code = 500;
                result.msg = '添加签名/印章接口异常';
            }

        } catch (error) {
            result.code = 500;
            result.msg = '添加签名/印章接口异常';
        }

        return result;
    }

    //添加模板
    // templateName	string	Y	模板调用名称
    // templateNumber	string	N	模板ID号，唯一，如果调用方不填写，那么易云章平台自动生成，字符长度最大64
    // html	string	Y	模板html内容,html结构完整，指定编码utf-8，例如：<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8”>
    // signPosition	json	N	签章位置列表,json字符串
    async addTemplate(qobj) {
        var result = {};
        try {
            var params = await this.ecloudClient.rformat(qobj);
            params.templateName = qobj.templateName;
            // params.templateNumber = qobj.templateNumber;
            params.html = qobj.html;
            // params.signPosition = qobj.signPosition;

            var url = this.APIURL + "/ecs/template/addHtmlTemplate.jspa";
            try {
                var rs = await this.restClient.execPost(params, url);
                if (rs && rs.stdout) {
                    var stdout = JSON.parse(rs.stdout) || {};
                    var code = stdout.meta.code;

                    if (code == 0) {
                        code = 1;
                    } else if (code == 1) {
                        code = 0;
                    } else {
                    }
                    result.code = code;
                    result.msg = stdout.meta.message;
                    result.data = stdout.data;
                } else {
                    result.code = 0;
                    result.msg = '添加模板接口返回空';
                }
            } catch (error) {
                result.code = 500;
                result.msg = '添加模板接口异常';
            }
        } catch (e) {
            result.code = 500;
            result.msg = '添加模板接口异常';
        }

        return result;
    }

    //生成合同
    // templateNumber	string	Y	模板编号
    // contractNum	string	N	合同编号，唯一，如果用户不填写，易云章讲自动生成，最大长度64
    // replaceVal	string	Y	模板变量值，例如{a:1312}，或者{“{a}”:1312},即，变量名称完全和模板中的一致，提供规则由用户自己定义
    async createPact(qobj) {
        var result = {};
        try {
            var params = await this.ecloudClient.rformat(qobj);
            params.templateNumber = qobj.templateNumber;
            // params.contractNum = qobj.contractNum;
            params.replaceVal = qobj.replaceVal;

            var url = this.APIURL + "/ecs/template/createContractByTemplate.jspa";
            try {
                var rs = await this.restClient.execPost(params, url);
                if (rs && rs.stdout) {
                    var stdout = JSON.parse(rs.stdout) || {};
                    var code = stdout.meta.code;

                    if (code == 0) {
                        code = 1;
                    } else if (code == 1) {
                        code = 0;
                    } else {
                    }
                    result.code = code;
                    result.msg = stdout.meta.message;
                    result.data = stdout.data;
                } else {
                    result.code = 0;
                    result.msg = '生成合同接口返回空';
                }
            } catch (error) {
                result.code = 500;
                result.msg = '生成合同接口异常';
            }
        } catch (e) {
            result.code = 500;
            result.msg = '生成合同接口异常';
        }
        return result;
    }

    //发送短信验证码
    // contractNum	string	Y	合同编号唯一标识
    // mobilePhone	String	Y	必须是用户签署所用手机号码，发送验证码后，在用户签署的时候需要进行验证
    // smsTemplate	String	N	可不填，默认安装易云章通用格式进行发送，如果需要定制，需要申请
    async sendVerify(qobj) {
        var result = {};
        try {
            var params = await this.ecloudClient.rformat(qobj);
            params.contractNum = qobj.contractNum;
            params.mobilePhone = qobj.mobilePhone;
            //params.smsTemplate = qobj.smsTemplate;

            var url = this.APIURL + "/ecs/contract/sendSms.jspa";
            try {
                var rs = await this.restClient.execPost(params, url);
                if (rs && rs.stdout) {
                    var stdout = JSON.parse(rs.stdout) || {};
                    var code = stdout.meta.code;

                    if (code == 0) {
                        code = 1;
                    } else if (code == 1) {
                        code = 0;
                    } else {
                    }
                    result.code = code;
                    result.msg = stdout.meta.message;
                    result.data = stdout.data;
                } else {
                    result.code = 0;
                    result.msg = '合同签署动态验证码发送接口返回空';
                }
            } catch (error) {
                result.code = 500;
                result.msg = '合同签署动态验证码发送接口异常';
            }
        } catch (e) {
            result.code = 500;
            result.msg = '合同签署动态验证码发送接口异常';
        }
        return result;

    }

    //验证验证码并签署
    // signerInfo	string	Y	签署人信息 格式json
    // contractNum	string	Y	合同编号唯一标识
    // dynamicCode	string	Y	动态验证码
    // isFinish	int	Y	合同是否签署完成(0:是,1:否),例如合同属于双方合同，第一个人签署时，isFinish 否，当第二个签署时候，参数isFinish为是
    // signInfo	json	N	签章信息，如果指定，则用户必须已经上传签章或者签字，并且在对应的模板中指定位置

    async authorizeSign(qobj) {
        var result = {};
        try {
            var params = await this.ecloudClient.rformat(qobj);
            params.signerInfo = qobj.signerInfo;
            params.contractNum = qobj.contractNum;
            params.dynamicCode = qobj.dynamicCode;
            params.isFinish = qobj.isFinish;
            //params.signInfo = qobj.signInfo;

            var url = this.APIURL + "/ecs/contract/authorizeSign.jspa";
            try {
                var rs = await this.restClient.execPost(params, url);
                if (rs && rs.stdout) {
                    var stdout = JSON.parse(rs.stdout) || {};
                    var code = stdout.meta.code;

                    if (code == 0) {
                        code = 1;
                    } else if (code == 1) {
                        code = 0;
                    } else {
                    }
                    result.code = code;
                    result.msg = stdout.meta.message;
                    result.data = stdout.data;
                } else {
                    result.code = 0;
                    result.msg = '验证验证码并签署接口返回空';
                }
            } catch (error) {
                result.code = 500;
                result.msg = '验证验证码并签署接口异常';
            }
        } catch (e) {
            result.code = 500;
            result.msg = '验证验证码并签署接口异常';
        }
        return result;


    }

    //合同下载
    // contractNum	string	Y	合同编号
    // mobilePhone	string	Y	合同发起人的手机号码

    async downloadCont(qobj) {
        var result = {};
        try {
            var params = await this.ecloudClient.rformat(qobj);
            params.contractNum = qobj.contractNum;
            params.mobilePhone = qobj.mobilePhone;

            var url = this.APIURL + "/ecs/contract/downloadCont.jspa";
            try {
                var rs = await this.restClient.execPost(params, url);
                if (rs && rs.stdout) {
                    var stdout = JSON.parse(rs.stdout) || {};
                    var code = stdout.meta.code;

                    if (code == 0) {
                        code = 1;
                    } else if (code == 1) {
                        code = 0;
                    } else {
                    }
                    result.code = code;
                    result.msg = stdout.meta.message;
                    result.data = stdout.data;
                } else {
                    result.code = 0;
                    result.msg = '合同下载接口返回空';
                }
            } catch (error) {
                result.code = 500;
                result.msg = '合同下载接口异常';
            }
        } catch (e) {
            result.code = 500;
            result.msg = '合同下载接口异常';
        }
        return result;

    }
}

module.exports = ecloudsignApi;