public with sharing class QuestionnaireAllowanceController {
    //用户信息
    public user userdet { get; set; }
    public Questionnaire_Survey__c csc { get; set; }
    public Boolean btn {get;set;}
    // 问卷 ID
    public String qsId  { get; set; }
    public QuestionnaireAllowanceController() {
        qsId = ApexPages.currentPage().getParameters().get('qsId');
        btn = false;
    }
    // 画面初始化
    public void init() {
        userdet = [SELECT Work_Location__c,Job_Category__c,Post__c FROM user where id = :UserInfo.getUserId()];
        csc = new Questionnaire_Survey__c();
        if(String.isNotBlank(qsId)){
            csc = [SELECT Id, Accept_GPS__c, Accept_traffic_Text__c, Accept_traffic__c, AutoNumber__c,
                        Incharge_Hospital_Count__c, Incharge_Hospital_FarthestDistance__c, Incharge_Hospital_Location__c,
                        Incharge_Hospital_NearestDistance__c, Most_dissatisfied__c, Most_satisfied__c,Notaccept_GPS_Reason__c,
                        Other_Visitefficiency_Text__c, Other_Visitefficiency__c,Questionnaire_user__c, TeamWork_efficiency_Text__c,
                        Traffic_subsidy_Opinion__c, Traffic_subsidy_Status__c, Traffic_subsidy_balance__c, Trip_Mode_Other__c,
                        Trip_Mode__c, User_Job_Category__c, User_Post__c, User_Work_Location__c, Visit_efficiency_quality_Text__c,
                        Visit_efficiency_quality__c, Workeffectiveness_departments_Text__c, Workeffectiveness_departments__c,
                        TeamWork_efficiency__c, Traffic_expense_reason__c,Questionnaire_Status__c
                        FROM Questionnaire_Survey__c WHERE Id = :qsId];
        }
    }
    // 保存按钮
    public PageReference save() {
        Questionnaire_Survey__c qs = new Questionnaire_Survey__c();
        //一、工作情况调查
        if(String.isBlank(csc.Incharge_Hospital_Count__c)){
            csc.Incharge_Hospital_Count__c.addError('请选择负责目标医院数量。');
            return null;
        }
        if(String.isBlank(csc.Incharge_Hospital_Location__c)){
            csc.Incharge_Hospital_Location__c.addError('请选择目标医院位置。');
            return null;
        }
        if(csc.Incharge_Hospital_FarthestDistance__c <= 0 || String.isBlank(String.valueOf(csc.Incharge_Hospital_FarthestDistance__c))){
            csc.Incharge_Hospital_FarthestDistance__c.addError('请录入最远距离。');
            return null;
        }
        if(csc.Incharge_Hospital_NearestDistance__c <= 0 || String.isBlank(String.valueOf(csc.Incharge_Hospital_NearestDistance__c))){
            csc.Incharge_Hospital_NearestDistance__c.addError('请录入最近距离。');
            return null;
        }
        if(csc.Incharge_Hospital_NearestDistance__c > csc.Incharge_Hospital_FarthestDistance__c){
            csc.Incharge_Hospital_FarthestDistance__c.addError('最远距离小于最近距离。');
            return null;
        }
        if(String.isBlank(csc.Trip_Mode__c)){
            csc.Trip_Mode__c.addError('请选择出行方式。');
            return null;
        }
        if(String.valueOf(csc.Trip_Mode__c) == '④其他' && String.isBlank(csc.Trip_Mode_Other__c)){
            csc.Trip_Mode_Other__c.addError('请录入具体出行方式。');
            return null;
        }
        //二、工作效率调查
        if(String.isBlank(csc.Visit_efficiency_quality__c)){
            csc.Visit_efficiency_quality__c.addError('请选择拜访效率/质量。');
            return null;
        }
        if(String.isBlank(csc.Visit_efficiency_quality_Text__c)){
            csc.Visit_efficiency_quality_Text__c.addError('请列举具体数据或事例');
            return null;
        }
        if(String.isBlank(csc.Other_Visitefficiency__c)){
            csc.Other_Visitefficiency__c.addError('请选择他方面的工作效率。');
            return null;
        }
        if(String.isBlank(csc.Other_Visitefficiency_Text__c)){
            csc.Other_Visitefficiency_Text__c.addError('请列举具体数据或事例');
            return null;
        }
        if(String.isBlank(csc.TeamWork_efficiency__c)){
            csc.TeamWork_efficiency__c.addError('请选择团队成员工作效率。');
            return null;
        }
        if(String.isBlank(csc.TeamWork_efficiency_Text__c)){
            csc.TeamWork_efficiency_Text__c.addError('请列举具体数据或事例');
            return null;
        }
        if(String.isBlank(csc.Workeffectiveness_departments__c)){
            csc.Workeffectiveness_departments__c.addError('请选合作部门工作效率。');
            return null;
        }
        if(String.isBlank(csc.Workeffectiveness_departments_Text__c)){
            csc.Workeffectiveness_departments_Text__c.addError('请列举具体数据或事例');
            return null;
        }
        //三、满意度情况调查
//        if(String.isBlank(csc.Traffic_subsidy_Status__c)){
//            csc.Traffic_subsidy_Status__c.addError('请选择实际支出。');
//            return null;
//        }
//        if(String.valueOf(csc.Traffic_subsidy_Status__c) == '③不能满足' && (String.isBlank(String.valueOf(csc.Traffic_subsidy_balance__c)) || csc.Traffic_subsidy_balance__c <= 0)){
//            csc.Traffic_subsidy_balance__c.addError('请录入差额');
//            return null;
//        }
        if(String.isBlank(csc.Most_satisfied__c)){
            csc.Most_satisfied__c.addError('在试行市内交通补贴期间，您最满意的是什么');
            return null;
        }
        if(String.isBlank(csc.Most_dissatisfied__c)){
            csc.Most_dissatisfied__c.addError('在试行市内交通补贴期间，您最不满意的是什么');
            return null;
        }
        //四、意见和建议
        if(String.isBlank(csc.Accept_traffic__c)){
            csc.Accept_traffic__c.addError('请选择愿意接受的交通费用政策');
            return null;
        }
        if(csc.Accept_traffic__c == '③其他(请说明)' && String.isBlank(csc.Accept_traffic_Text__c)){
            csc.Accept_traffic_Text__c.addError('请列举具体数据或事例');
            return null;
        }
        if(String.isBlank(csc.Traffic_expense_reason__c)){
            csc.Traffic_expense_reason__c.addError('请说明选择以上方式的原因');
            return null;
        }
        if(String.isBlank(csc.Accept_GPS__c)){
            csc.Accept_GPS__c.addError('请选择是否接受GPS');
            return null;
        }
        if(csc.Accept_GPS__c == '②不能接受' && String.isBlank(csc.Notaccept_GPS_Reason__c)){
            csc.Notaccept_GPS_Reason__c.addError('请说明不接受GPS原因');
            return null;
        }
        if(String.isBlank(csc.Traffic_subsidy_Opinion__c)){
            csc.Traffic_subsidy_Opinion__c.addError('请提出你的意见或建议');
            return null;
        }
        if(String.isNotBlank(qsId)){
            qs.id = qsId;
        }else{
            qs.Name = '*';
        }
        qs.Questionnaire_user__c = UserInfo.getUserId();
        qs.Incharge_Hospital_Count__c = csc.Incharge_Hospital_Count__c;
        qs.Incharge_Hospital_Location__c = csc.Incharge_Hospital_Location__c;
        qs.Incharge_Hospital_FarthestDistance__c = csc.Incharge_Hospital_FarthestDistance__c;
        qs.Incharge_Hospital_NearestDistance__c = csc.Incharge_Hospital_NearestDistance__c;
        qs.Trip_Mode__c = csc.Trip_Mode__c;
        qs.Trip_Mode_Other__c = csc.Trip_Mode_Other__c;
        //二、工作效率调查
        qs.Visit_efficiency_quality__c = csc.Visit_efficiency_quality__c;
        qs.Visit_efficiency_quality_Text__c = csc.Visit_efficiency_quality_Text__c;
        qs.Other_Visitefficiency__c = csc.Other_Visitefficiency__c;
        qs.Other_Visitefficiency_Text__c = csc.Other_Visitefficiency_Text__c;
        qs.TeamWork_efficiency__c = csc.TeamWork_efficiency__c;
        qs.TeamWork_efficiency_Text__c = csc.TeamWork_efficiency_Text__c;
        qs.Workeffectiveness_departments__c = csc.Workeffectiveness_departments__c;
        qs.Workeffectiveness_departments_Text__c = csc.Workeffectiveness_departments_Text__c;
        //三、满意度情况调查
        qs.Traffic_subsidy_Status__c = csc.Traffic_subsidy_Status__c;
        qs.Traffic_subsidy_balance__c = csc.Traffic_subsidy_balance__c;
        qs.Most_satisfied__c = csc.Most_satisfied__c;
        qs.Most_dissatisfied__c = csc.Most_dissatisfied__c;
        //三、满意度情况调查
        qs.Accept_traffic__c = csc.Accept_traffic__c;
        qs.Accept_traffic_Text__c = csc.Accept_traffic_Text__c;
        qs.Traffic_expense_reason__c = csc.Traffic_expense_reason__c;
        qs.Accept_GPS__c = csc.Accept_GPS__c;
        qs.Notaccept_GPS_Reason__c = csc.Notaccept_GPS_Reason__c;
        qs.Traffic_subsidy_Opinion__c = csc.Traffic_subsidy_Opinion__c;
        Savepoint sp = Database.setSavepoint();
        try{
            upsert qs;
            btn = true;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '保存成功'));
        } catch (Exception e) {
            ApexPages.addMessages(e);
            Database.rollback(sp);
        }
        qsId = qs.Id;
        return UnabletoEdit();
    }
    // 返回不可编辑状态
    public PageReference UnabletoEdit(){
        PageReference ref = new Pagereference('/apex/QuestionnaireAllowance?qsId='+qsId);
        ref.setRedirect(true);
        return ref;
    }
    // 提交按钮
    public PageReference Sorder() {
        Savepoint sp = Database.setSavepoint();
        try {
            Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            psr.setObjectId(qsId);
            Approval.ProcessResult submitResult = Approval.process(psr);
            init();
            return  null;
        }
        catch (Exception e) {
            ApexPages.addmessages(e);
            Database.rollback(sp);
            return  null;
        }
    }

}