import {Component} from '@angular/core';
import {IonicPage, LoadingController, ModalController, NavController, NavParams, AlertController} from 'ionic-angular';
import {HomeService} from "../../home.service";
import {LoginService} from "../../../login/login.service";
import {InfoDetailPage} from "../info-detail/info-detail";
import {SearchFilterComponent} from "../../../../components/search-filter/search-filter";
import {DealService} from "../../../deal/deal.service";

@Component({
  selector: 'page-info-list',
  templateUrl: 'info-list.html',
})


export class InfoListPage {

  option = {
    type: '1',
    viewType: '1',  //增加用户体验的
    list: [  //默认事业单位
      {name: "事业单位", type: '1'},
      {name: "机关群团", type: '2'},
      {name: "行政执法", type: '0'},
      {name: "授权登记", type: '3'}
    ],
    history: []
  };
  info = {
    list: [],
    page: 1,
    limit: 20,
    search: '',
    totalCount: null,
    type: 3,
    typeDataArr: [],
    typeFilter: [
      {lable: '单位名称', id: 3},
      {lable: '举办单位', id: 2},
      {lable: '宗旨业务', id: 1}
    ],
    // 过滤条件
    area: [],
    industry: [],
    funding: [],
    cause: [],
    male: {code: ''},
    lawIndustry: {code: ''},

    nature: [],
    category: [],

    specifications: [],
  };

  // 过滤条件列表
  areaList = [];
  industryList = [];
  fundingList = [];
  causeList = [];
  natureList = [];
  categoryList = [];
  specificationsList = [];

  searchState = false

  arr = new Array(10)

  constructor(public navCtrl: NavController, public navParams: NavParams,private alertCtrl: AlertController,
              private loadCtrl: LoadingController, private homeSer: HomeService,
              private loginSer: LoginService, private modalCtrl: ModalController,) {
                this.getInit()
              }

  ionViewDidLoad() {
    // this.getList();
    this.option.history = JSON.parse(localStorage.getItem('history'))
    this.info.typeDataArr = this.info.typeFilter
  }


  getInit(){ // 请求
    this.getArea()
    this.getIndustryList()
    this.getFundingList()
    this.getCauseList()
    this.getSpecificationsList()
    this.getNatureList()
    this.getCategoryList()
  }

  changeType() { // 清除过滤条件
    // 过滤类型--关联--单位选择
    this.info.typeDataArr = this.info.typeFilter // 左侧下拉
    if (this.option.type === '0') this.info.typeDataArr = this.info.typeFilter.filter(x => x.id !== 1)
    if (this.option.type === '2') this.info.typeDataArr = this.info.typeFilter.filter(x => x.id == 3 || x.id == 4)
    this.info.area = [];
    this.info.industry = [];
    this.info.funding = [];
    this.info.cause = [];
    this.info.male = {code: ''};
    this.info.lawIndustry = {code: ''};
    this.info.nature = [];
    this.info.category = [];
    this.info.specifications = [];

    this.info.list = [];
    this.info.search = '';
    this.searchState = false
    this.option.history = JSON.parse(localStorage.getItem('history'))
    this.getInit()
  }

  // 搜索按钮
  getList(event?: any) {
    let list = JSON.parse(localStorage.getItem('history'))
    if (list) { //查看local是否有history
      if (list.indexOf(this.info.search) != -1) {  // 历史记录是否有该条信息
        list.splice(list.indexOf(this.info.search),1)
      }
      if (list.length>9) list.pop() // 超过十条，删除最后一个
      list.unshift(this.info.search)
      this.option.history = list // 更新页面历史记录
      localStorage.setItem('history', JSON.stringify(list)) //存local
    } else {
      let arr = new Array() // 创建数组
      arr.unshift(this.info.search) //添加入数组
      localStorage.setItem('history',JSON.stringify(arr)); // 存local
    }

    this.option.type === '2' ? this.getCreditor(event) : this.getUnit(event); //调查list接口
  }

  //机关群团
  getCreditor(event?: any) {
    const loadCtrl = this.loadCtrl.create();
    loadCtrl.present();
    this.info.page = 1;
    const data = {
      search: this.info.search,
      page: this.info.page,
      limit: this.info.limit,
      type: '2',
      openid: "",
      jgxz: this.info.nature ? this.info.nature.map(x => x.code) : [], // 性质
      jglb: this.info.category ? this.info.category.map(x => x.code) : [], // 类别
      areacode: this.info.area ? this.info.area.map(x => x.code) : []
    };
    this.homeSer.creditorgan(data).subscribe(
      (res) => {
        this.searchState = true
        loadCtrl.dismiss();
        if (event) event.complete();
        this.option.viewType = '2';
        this.info.list = res.page.list.object;
        this.info.totalCount = res.page.totalCount;
      }
    )
  }

  //事业单位、行政执法
  getUnit(event?: any) {
    const loadCtrl = this.loadCtrl.create();
    loadCtrl.present();
    this.info.page = 1;
    const data = {
      search: this.info.search,
      page: this.info.page,
      limit: this.info.limit,
      type: '1',
      areacode: this.info.area ? this.info.area.map(x => x.code) : [],
      openid: "",
      isUnit: this.option.list.filter(x => x.type === this.option.type).map(x => x.type).toString(),
      hyfl: this.info.industry ? this.info.industry.map(x => x.code) : [],
      jfxs: this.info.funding ? this.info.funding.map(x => x.code) : [],
      dwfl: this.info.cause ? this.info.cause.map(x => x.code) : [],
      searchType: this.info.type,
      civilServantManagement: this.info.male.code,
      jggg: this.info.specifications ? this.info.specifications.map(x => x.code) : [],
      xzzf_hy: this.info.lawIndustry.code
    };
    this.homeSer.unitfind(data).subscribe(
      (res) => {
        this.searchState = true
        loadCtrl.dismiss();
        if (event) event.complete();
        this.option.viewType = '1';
        this.info.list = res.page.list.object;
        this.info.totalCount = res.page.totalCount;
      }
    )
  }

  //加载更多
  doInfinite(event) {
    this.option.type === '2' ? this.getMoreCreditor(event) : this.getMoreUnit(event);
  }

  getMoreUnit(event) {
    if (this.info.totalCount == this.info.list.length) {
      event.enable(false);
      return false;
    }
    this.info.page++;
    const data = {
      search: this.info.search,
      page: this.info.page,
      limit: this.info.limit,
      type: '1',
      areacode: this.info.area ? this.info.area.map(x => x.code) : [],
      openid: "",
      isUnit: this.option.list.filter(x => x.type === this.option.type).map(x => x.type).toString(),
      hyfl: this.info.industry ? this.info.industry.map(x => x.code) : [],
      jfxs: this.info.funding ? this.info.funding.map(x => x.code) : [],
      dwfl: this.info.cause ? this.info.cause.map(x => x.code) : [],
      searchType: this.info.type,
      civilServantManagement: this.info.male.code,
      jggg: this.info.specifications ? this.info.specifications.map(x => x.code) : [],
      xzzf_hy: this.info.lawIndustry.code,
    };
    this.homeSer.unitfind(data).subscribe(
      (res) => {
        this.searchState = true
        if (event) event.complete();
        this.info.list = this.info.list.concat(res.page.list.object);
        this.info.totalCount = res.page.totalCount;
      }
    )
  }

  getMoreCreditor(event) {
    if (this.info.totalCount == this.info.list.length) {
      event.enable(false);
      return false;
    }
    this.info.page++;
    const data = {
      search: this.info.search,
      page: this.info.page,
      limit: this.info.limit,
      type: '2',
      openid: "",
      jgxz: this.info.nature ? this.info.nature.map(x => x.code) : [], // 性质
      jglb: this.info.category ? this.info.category.map(x => x.code) : [], // 类别
      areacode: this.info.area ? this.info.area.map(x => x.code) : []
    };
    this.homeSer.creditorgan(data).subscribe(
      (res) => {
        this.searchState = true
        if (event) event.complete();
        this.info.list = this.info.list.concat(res.page.list.object);
        this.info.totalCount = res.page.totalCount;
      }
    )
  }

  //查询详情
  goToDetail(item) {
    this.navCtrl.push(InfoDetailPage, {obj: item, type: this.option.type});
  }

  // 选择类型
  switchType(){
    console.log(this.info.type)
  }

  // 选择类型
  filterConditions() {
    let modal = this.modalCtrl.create(SearchFilterComponent, {
        unitType: this.option.type,
        area: this.info.area,
        industry: this.info.industry,
        funding: this.info.funding,
        cause: this.info.cause,

        nature: this.info.nature,
        category: this.info.category,
        specifications: this.info.specifications,
        lawIndustry: this.info.lawIndustry,
        male: this.info.male,

        areaList: this.areaList,
        industryList: this.industryList,
        fundingList: this.fundingList,
        causeList: this.causeList,
        specificationsList: this.specificationsList,
        natureList: this.natureList,
        categoryList: this.categoryList,
      },
      {
        enterAnimation: 'modal-from-right-enter',
        leaveAnimation: 'modal-from-right-leave'
      });
    modal.onDidDismiss(res => {
      if (res) {
        this.info.area = res.area
        this.info.industry = res.industry
        this.info.funding = res.funding
        this.info.cause = res.cause
        this.info.male = res.male
        this.info.lawIndustry = res.lawIndustry
        this.info.specifications = res.specifications
        this.info.nature = res.nature
        this.info.category = res.category
        this.option.type === '2' ? this.getCreditor(event) : this.getUnit(event);
      }
    });
    modal.present();
  }

  // 添加历史记录
  historyField(value){
    this.info.search = value
    this.getList()
  }

  //获取区属
  getArea() {
    const data = {};
    this.loginSer.getArea(data).subscribe(
      (res) => {
        if (res.list) {
          this.areaList = res.list;
          this.areaList.unshift({code: "0", name: "全市"})
          this.areaList.splice(2, 0, {code: "-1", name: "区属"})
          this.areaList.map(x => { x.active = false })
        }
      }
    )
  }
  // 获取行业
  getIndustryList() {
    this.homeSer.getIndustryList({}).subscribe(
      (res) => {
        if (res.list) {
          this.industryList = res.list;
          this.industryList = this.industryList.filter(x => x.code == '01' || x.code == '04' || x.code == '02' || x.code == '03' || x.code == '08')
          this.industryList.push({code: "-1", name: "其他"})
          this.industryList.map(x => { x.active = false })
        }
      }
    )
  }
  // 获取经费来源
  getFundingList() {
    const data = {};
    this.homeSer.getFundingList(data).subscribe(
      (res) => {
        if (res.list) {
          this.fundingList = res.list;
          this.fundingList = this.fundingList.filter(x => x.code == '1' || x.code == '2' || x.code == '3' || x.code == '6')
          this.fundingList.map(x => { x.active = false })
        }
      }
    )
  }
  // 获取单位类别
  getCauseList() {
    const data = {};
    this.homeSer.getCauseList(data).subscribe(
      (res) => {
        if (res.list) {
          this.causeList = res.list;
          this.causeList = this.causeList.filter(x => x.code == '2' || x.code == '3' || x.code == '4' || x.code == '5' || x.code == '6' || x.code == '7' || x.code == '8')
          this.causeList.map(x => { x.active = false })
        }
      }
    )
  }
  // 获取规格
  getSpecificationsList() {
    const data = {};
    this.homeSer.getSpecificationsList(data).subscribe(
      (res) => {
        if (res.list) {
          this.specificationsList = res.list;
          // this.specificationsList = this.specificationsList.filter(x => x.code == '2' || x.code == '3' || x.code == '4' || x.code == '5' || x.code == '6' || x.code == '7' || x.code == '8')
          this.specificationsList.map(x => { x.active = false })
        }
      }
    )
  }
  // 获取机构性质
  getNatureList() {
    const data = {};
    this.homeSer.getNatureList(data).subscribe(
      (res) => {
        if (res.list) {
          this.natureList = res.list;
          // this.creditorganList = this.creditorganList.filter(x => x.code == '2' || x.code == '3' || x.code == '4' || x.code == '5' || x.code == '6' || x.code == '7' || x.code == '8')
          this.natureList.map(x => { x.active = false })
        }
      }
    )
  }
  // 获取机构类别
  getCategoryList() {
    const data = {};
    this.homeSer.getCategoryList(data).subscribe(
      (res) => {
        if (res.list) {
          this.categoryList = res.list;
          // this.creditorganList = this.creditorganList.filter(x => x.code == '2' || x.code == '3' || x.code == '4' || x.code == '5' || x.code == '6' || x.code == '7' || x.code == '8')
          this.categoryList.map(x => { x.active = false })
        }
      }
    )
  }
  
}
