public with sharing class SubmitForApprovalController {
    //维修合同失单数据
    public Lost_Report__c lostData {
        get;
        set;
    }
    public List<Maintenance_Contract__c> BAOJIAList {
        get;
        set;
    }
    //ID作为SQL的条件
    public String Id {
        get;
        set;
    }
    public boolean flag {
        get;
        set;
    }
    public Boolean canseePage {
        get;
        set;
    }
    public String mcId {
        get;
        set;
    }
    public String Maintenance_ContractName {
        get;
        set;
    }
    //构造器
    public SubmitForApprovalController() {
        //从地址栏获取ID
        Id = System.currentPageReference().getParameters().get('id');
        mcId = System.currentPageReference().getParameters().get('mcId');
        // 维修合同失单数据
        lostData = new Lost_Report__c();
        flag = false;
    }
    public SubmitForApprovalController(ApexPages.StandardController controller) {
        this();
    }
    //初始化方法
    public void init() {
        if (String.isNotBlank(Id)) {
            canseePage = false;
            List < Lost_Report__c > lostDataList = new List < Lost_Report__c > ();
            String lostReportSql = lostOrder(Id);
            lostDataList = Database.query(lostReportSql);
            if (lostDataList.size() > 0) {
                lostData = lostDataList[0];
                mcId = lostData.Maintenance_Contract__c;
            }
            if (lostData.Status__c == '提交' || lostData.Status__c == '审批中' || lostData.Status__c == '批准') {
                canseePage = true;
                return;
            }
        } else {
            lostData.Maintenance_Contract__c = mcId;
            lostData.Status__c = '草案中';
            // insert lostData;
        }
    }
    //保存方法
    public PageReference save() {
        flag = false;
        try {
            Boolean resultSaveFlag = dataInspect();
            if (!resultSaveFlag) {
                ControllerUtil.lostOrder(lostData);
                flag = true;
            }
        } catch (Exception e) {
            ApexPages.addmessages(e);
            return null;
        }
        return null;
    }
    //提交审批方法
    public PageReference submit() {
        save();
        
        lostData.Status__c = '提交';
        try {
            ControllerUtil.lostOrder(lostData);
            flag = true;
        } catch (Exception e) {
            ApexPages.addmessages(e);
            return null;
        }
        return null;
    }
    //数据检查警告
    public Boolean dataInspect() {
        boolean result = false;
        if (lostData.Specific_Reasons__c == null) {
            lostData.Specific_Reasons__c.addError('请选择你的具体原因');
            result = true;
        }
        if (lostData.To_Where__c == null) {
            lostData.To_Where__c.addError('请选择您的去向');
            result = true;

        }
        if (lostData.Specific_Reasons__c == '其他（手写）') {
            if (String.isBlank(lostData.Other_Reasons__c)) {
                lostData.Other_Reasons__c.addError('具体原因为"其他"时，应详细说明！');
                result = true;
            }
        } else {
            lostData.Other_Reasons__c = null;
        }
        if (lostData.To_Where__c == '医院选择第三方') {
            if (String.isBlank(lostData.Third_Party_Company__c)) {
                lostData.Third_Party_Company__c.addError('去向为"第三方公司"，应详细说明第三方名称');
                result = true;
            }
            if (lostData.Third_Party_Contract_Price__c == null) {
                lostData.Third_Party_Company__c.addError('去向为"第三方公司"，应详细说明第三方成交价格');
                result = true;
            }
        } else {
            lostData.Third_Party_Company__c = null;
            lostData.Third_Party_Contract_Price__c = null;
        }
        if (lostData.To_Where__c == '其他（手写）') {
            if (String.isBlank(lostData.Other__c)) {
                lostData.Third_Party_Company__c.addError('去向为"其他"应详细说明!');
                result = true;
            }
        } else {
            lostData.Other__c = null;
        }
        return result;
    }
    //查询具体原因的sql
    public String lostOrder(String Id) {
        String lostReportSql = 'SELECT id,Status__c,Other_Reasons__c,Other__c,Third_Party_Company__c,Third_Party_Contract_Price__c,To_Where__c,Specific_Reasons__c,Maintenance_Contract__c from Lost_Report__c where Id = \'' + Id + '\'';
        return lostReportSql;
    }
    public List<Maintenance_Contract__c> selectMC(String selectString) {
        String mcsql = '%'+selectString+'%';
        String selectmc = 'SELECT id,name from Maintenance_Contract__c where name like :mcsql';
        BAOJIAList = Database.query(selectmc);
        return BAOJIAList;
    }
}