public without sharing class ConsumableAccountController {
	//经销商用户产品分类（ET、ENG）
    public String agencyProType {get;set;}
    public String agencyProTypestr {get;set;}
    //选项卡名称
	public String filterName { get; set; }
	//选项卡类型
	public String hosptialType { get; set; }
	public ApexPages.StandardSetController setCon { get; set; }
	//画面显示数量
	public Integer size{get;set;}
	public Integer noOfRecords{get; set;}
	//显示集合
	public List<Account> pageRecords { get; set; }
	//经销商信息
	private String accountName ;
	private String accountId ;
	//排序使用
    public String sortKey { get; set; }
    public String preSortKey { get; private set; }
    public Boolean sortOrderAsc { get; private set; }
    public String[] sortOrder { get; private set; }
	//排序使用
    private String[] orderby = new String[]{ 'Name','State_Master__c','Salesdepartment_HP__c','CreatedDate','Is_Active__c','RecordType.Name','Grade__c','OCM_Category__c','City_Master__c','Town__c','Phone'};
    //上周开始、结束日期
    private Datetime lastweekstart ;
    private Datetime lastweekend ;
    public ConsumableAccountController () {
    	size = Integer.valueOf(System.Label.orderdetLimitsize);
    	pageRecords = new List<Account>();
    }
    // 画面初始化
    public void init() {
    	//排序用初始化
    	this.sortKey = '0';
        this.preSortKey = '0';
        this.sortOrderAsc = false;
        this.sortOrder = new String[11];
        this.sortOrder = new String[]{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' '};
    	User Useracc = [SELECT accountid,UserPro_Type__c FROM user WHERE id =:UserInfo.getUserId()];
    	accountId = [SELECT id,Name FROM account WHERE id =:Useracc.accountid].id;
    	agencyProType = Useracc.UserPro_Type__c;
        if(String.isBlank(Useracc.UserPro_Type__c)){
            agencyProType = 'ET';
        }
        agencyProTypestr = '%' + agencyProType + '%';
        List<Account> selectAccList = [SELECT id,Name,State_Master__c,Salesdepartment_HP__c,CreatedDate,Is_Active__c,RecordType.Name
    										,Grade__c,OCM_Category__c,City_Master__c,Town__c,Phone
    									FROM Account 
    									WHERE id in (SELECT Hospital__c FROM Agency_Hospital_Link__c WHERE Agency__c =:accountId AND Hosptial_Type__c like:agencyProTypestr)];
    	initStandardController(selectAccList);
    	makepagerecords();
    }
    //取画面显示内容
    private void initStandardController(List<Account> acc){
        // init standard controller
        setCon = new ApexPages.StandardSetController(acc);
        // sets the number of records in each page set
        setCon.setPageSize(size);
        noOfRecords = setCon.getResultSize();
    }
    public List<Account> cod2s() {
         return (List<Account>) setCon.getRecords();
    }
    //Changes the size of pagination
    public PageReference refreshPageSize() {
        setCon.setPageSize(size);
        pageRecords = cod2s();
        makepagerecords();
        return null;
    }
    public List<Account> makepagerecords() {
        pageRecords = cod2s();
         return pageRecords;
    }

    public void changelistView(){
    	String soql = this.makeSoql(filterName,accountId);
        System.debug('soql+++++++ ' + soql);
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'soql  ' + soql));
        //return;
    	List<Account> selectAccList = Database.query(soql);
    	initStandardController(selectAccList);
    	makepagerecords();
    }
    //sql文作成
    private String makeSoql(String viewName,String accountId){
    	Date st = Date.today().addDays(-7).toStartOfWeek();
    	lastweekstart = Datetime.newInstance(st.year(), st.month(), st.day(), 8, 0, 0);
    	lastweekend = lastweekstart.addDays(7);
        String soql = 'SELECT id,Name,State_Master__c,Salesdepartment_HP__c,CreatedDate,Is_Active__c,RecordType.Name';
        soql += ' ,Grade__c,OCM_Category__c,City_Master__c,Town__c,Phone FROM Account';
        soql += ' WHERE id in (SELECT Hospital__c FROM Agency_Hospital_Link__c WHERE Agency__c =:accountId AND Hosptial_Type__c like \'%' + String.escapeSingleQuotes(agencyProTypestr.replaceAll('%', '\\%')) + '%\')' ;


        if(viewName == '61. 医院_Hospital草案中的医院'){
            soql += ' AND Is_Active__c = \''+'草案中'+'\'';
        }
        if(viewName == '62. 医院_Hospital申请中的医院'){
            soql += ' AND Is_Active__c = \''+'申请中'+'\'';
        }
        if(viewName == '63. 医院_Hospital上周创建的医院'){
            soql += ' AND Is_Active__c = \''+'有效'+'\'';
            soql += ' AND CreatedDate >= :lastweekstart ';
            soql += ' AND CreatedDate < :lastweekend ';
        }
        //if(viewName == 'ET特约店'){
        //    soql += ' AND Is_Active__c = \''+'申请中'+'\'';
        //}
        //if(viewName == 'ENG特约店'){
        //    soql += ' AND Is_Active__c = \''+'申请中'+'\'';
        //}
        System.debug('soql +++++' + soql);
        return soql;
    }
    //排序
    public void accsort(){
    	if (this.sortKey == this.preSortKey) {
        // 方向が変わるのみ
            this.sortOrderAsc = !this.sortOrderAsc;
            this.sortOrder[Integer.valueOf(this.sortKey)] = (this.sortOrderAsc == true ? '↑' : '↓');
        } else {
            this.sortOrderAsc = true;
            this.sortOrder[Integer.valueOf(this.preSortKey)] = ' ';
            this.sortOrder[Integer.valueOf(this.sortKey)] = (this.sortOrderAsc == true ? '↑' : '↓');
        }
        this.preSortKey = this.sortKey;
        String soql = this.makeSoql(filterName,accountId);
        soql += ' order by ' + this.orderby[Integer.valueOf(this.sortKey)] + ' ' + (this.sortOrderAsc == true ? 'asc nulls first' : 'desc nulls last');
    	List<Account> selectAccList = Database.query(soql);
    	initStandardController(selectAccList);
    	makepagerecords();

    }
    //选项卡集合
    public  List<SelectOption> getlistViewOptions() {
        List<SelectOption> listViewOptions = new List<SelectOption>();
        listViewOptions.add(new SelectOption('01. 医院_Hospital','01. 医院_Hospital'));
        listViewOptions.add(new SelectOption('61. 医院_Hospital草案中的医院','61. 医院_Hospital草案中的医院'));
        listViewOptions.add(new SelectOption('62. 医院_Hospital申请中的医院','62. 医院_Hospital申请中的医院'));
        listViewOptions.add(new SelectOption('63. 医院_Hospital上周创建的医院','63. 医院_Hospital上周创建的医院'));
        //if(agencyProType == 'ET'){
        //    listViewOptions.add(new SelectOption('ET特约店','ET特约店'));
        //}else{
        //    listViewOptions.add(new SelectOption('ENG特约店','ENG特约店'));
        //}
        return listViewOptions;
    }
}