import {Component} from '@angular/core';
import {NavParams, ViewController} from "ionic-angular";
import {LoginService} from "../../pages/login/login.service";
import {HomeService} from "../../pages/home/home.service";
import {GlobalData} from "../../core/GlobleData";
import {Storage} from "@ionic/storage";
import { shiftInitState } from '@angular/core/src/view';

@Component({
    selector: 'search-filter',
    templateUrl: 'search-filter.html'
})
export class SearchFilterComponent {
    text: string;

    areaObj;
    industryObj;
    fundingObj;
    causeObj;
    specificationsObj;
    natureObj;
    categoryObj;
    maleObj;
    lawIndustryObj;
    areaList = [];
    industryList = [];
    fundingList = [];
    causeList = [];
    specificationsList = [];
    natureList = []
    categoryList = []
    maleList = [{name: '全部',code: ''},{name: '是',code: '1'},{name: '否',code: '0'}];
    lawIndustryList = [{code: '交通', name: '交通'},{code: '环境', name: '环境'},{code: '文化', name: '文化'},{code: '旅游', name: '旅游'},{code: '民政', name: '民政'}]
    unitType = '';

    constructor(private viewCtrl: ViewController, private params: NavParams,
                private globle: GlobalData,
                private storage: Storage,
                private loginSer: LoginService,
                private HomeSer: HomeService) {
                    this.areaObj = this.params.get("area");
                    this.industryObj = this.params.get("industry");
                    this.fundingObj = this.params.get("funding");
                    this.causeObj = this.params.get("cause");
                    this.maleObj = this.params.get("male");
                    this.lawIndustryObj = this.params.get("lawIndustry");
                    this.areaList = this.params.get("areaList");
                    this.industryList = this.params.get("industryList");
                    this.fundingList = this.params.get("fundingList");
                    this.causeList = this.params.get("causeList");
                    this.specificationsList = this.params.get("specificationsList");
                    this.natureList = this.params.get("natureList");
                    this.categoryList = this.params.get("categoryList");
                    this.unitType = this.params.get("unitType");
                    console.log(this.areaList, this.industryList, this.fundingList, this.causeList, this.unitType)
                }

    //区域选择
    selectArea(item) {
        item.active = !item.active
    }
    //行业选择
    selectIndustry(item) {
        item.active = !item.active
    }
    //经费来源选择
    selectFunding(item) {
        item.active = !item.active
    }
    //单位类别选择
    selectCause(item) {
        item.active = !item.active
    }
    //单位类别选择 --- 单选
    selectMale(item) {
        this.maleObj = item
    }
    //行业选择 --- 单选
    selectLawIndustry(item) {
        this.lawIndustryObj = item
    }
    // 规格选择
    selectSpecifications(item) {
        item.active = !item.active
    }
    //机构性质选择
    selectNature(item) {
        item.active = !item.active
    }
    //机构类别选择
    selectCategory(item) {
        item.active = !item.active
    }

    //重置
    reset() {
        this.maleObj = {code: ''}
        this.lawIndustryObj = {code: ''}
        this.areaList.map(x => x.active = false)
        this.industryList.map(x => x.active = false)
        this.fundingList.map(x => x.active = false)
        this.causeList.map(x => x.active = false)
        this.specificationsList.map(x => x.active = false)
        this.natureList.map(x => x.active = false)
        this.categoryList.map(x => x.active = false)
    }

    //确定
    confirm() {
        this.areaObj = this.areaList.filter(x => x.active)
        this.industryObj = this.industryList.filter(x => x.active)
        this.fundingObj = this.fundingList.filter(x => x.active)
        this.causeObj = this.causeList.filter(x => x.active)
        this.specificationsObj = this.specificationsList.filter(x => x.active)
        this.natureObj = this.natureList.filter(x => x.active)
        this.categoryObj = this.categoryList.filter(x => x.active)
        const data = {
            area: this.areaObj,
            industry: this.industryObj,
            funding: this.fundingObj,
            cause: this.causeObj,
            specifications: this.specificationsObj,
            nature: this.natureObj,
            category: this.categoryObj,
            male: this.maleObj,
            lawIndustry: this.lawIndustryObj,
        };
        console.log(data)
        this.viewCtrl.dismiss(data);
    }

}
