<template>
  <div class="home-wrapper">
    <!--    <TopSearch :searchVal="searchVal" :searchType="searchType" :searchTypeArr="searchTypeArr"/>-->
    <div class="nav" stype="height:500px">
      <!--搜索框-->
      <el-row class="search-nav" style="padding: 30px 0 20px 0">
        <el-col :span="6">
          <div class="pull-left" @click="gotoHome" style="cursor: pointer">
            <img class="logo-img" src="../../assets/img/logo-blue.png"/>
            <span class="logo-title">群智众测平台</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="search-nav">
            <div id="search-block ">
              <el-tabs v-model="searchType" type="card" @tab-click="handleTypeClick">
                <el-tab-pane v-for="item in searchTypeArr" v-if="item.value!=='all'" :label="item.name"
                             :name="item.value"
                             :key="item.value"></el-tab-pane>
              </el-tabs>
              <div class="search-input">
                <el-input placeholder="请输入内容" v-model="searchVal" class="input-with-select">
                  <el-button class="search-button" slot="append" type="primary" @click="handleSearchData">搜索</el-button>
                </el-input>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :span="6">
          <el-button type="primary pull-right" class="releaseBtn" @click="checkLogin()">免费发布众测需求</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="container" style="margin: 20px auto;" v-loading="loading">
      <div class="create-body">
        <div class="title h2">入驻品牌机构</div>
        <el-collapse accordion style="margin: 0 30px">
          <template style="color: black">
            <el-table
              ref="multipleTable"
              :data="curAgencyList"
              tooltip-effect="dark"
              style="width: 100%; font-size: 20px; color: black">
              <el-table-column
                label="头像"
                width="100">
                <template slot-scope="scope"><img
                  :src="scope.row.agencyPhoto==null?defaultValue.image:scope.row.agencyPhoto"
                  style="width: 50px; height: 50px;"/></template>
              </el-table-column>
              <el-table-column
                prop="name"
                label="名称"
                align="left"
                width="300">
              </el-table-column>
              <el-table-column
                prop="address"
                align="left"
                label="省市">
              </el-table-column>
              <el-table-column
                align="center"
                label="操作">
                <template slot-scope="scope">
                  <div class="btn btn-small btn-info" @click="goToDetail(scope.row.userId)">查看详情</div>
                </template>
              </el-table-column>
            </el-table>
          </template>
          <el-pagination
            v-if="curAgencyList&&curAgencyList.length"
            :page-size="9"
            layout="prev, pager, next"
            :total="totalElements"
            :current-page="activePage"
            @current-change="handlePageChange"
            class="pull-right"
          >
          </el-pagination>
        </el-collapse>
      </div>
    </div>
  </div>
</template>

<script>
  import Http from '@/js/http.js';
  import TopSearch from "../../components/commons/TopSearch";
  import {defaultValue, storageGet} from '@/js/index.js'
  import {notify} from "../../constants";
  import Apis from '@/js/api';

  export default {
    name: "AgencyResidentList",
    props: ['searchVal', 'agencyList'],
    components: {TopSearch},
    data() {
      return {
        user: {},
        isLogin: false,
        searchType: '0',
        loading: false,
        defaultValue: defaultValue,
        searchTypeArr: [
          {
            "name": "入驻机构",
            "value": "0"
          }],
        curAgencyList: this.agencyList,
        activePage: 1,
        totalElements: 0
      }
    },
    methods: {
      loadData() {
        if (storageGet('user') != null) {
          this.isLogin = true;
        }
        let url = '/api/resident/agency/more';
        let params = {
          "keyword": this.searchVal,
          "activePage": this.activePage,
          "columnFilters": [
            {
              "field": "type",
              "type": "enums",
              "enums": this.searchTypeArr,
              "value": this.searchType
            }
          ]
        }
        Http.post(url, params).then((res) => {
          this.curAgencyList = res.data.content;
          this.totalElements = res.data.totalElements;
        })
      },
      checkLogin() {
        this.checkCreateProjectAuth();
      },
      checkCreateProjectAuth() {
        if (!this.isLogin) {
          console.log("请登录后访问");
          notify('warning', '请登录后访问');
        } else if (this.isLogin) {
          Http.get(Apis.USER.IS_PART.replace('{userId}', this.user.id)).then((res) => {
            this.$router.push('/project/create');
          }).catch((error) => {
            notify('error', error.data)
          })
        }
      },
      handleSearchData() {
        let url = '/api/resident/agency/more';
        let params = {
          "keyword": this.searchVal,
          "activePage": this.activePage,
          "columnFilters": [
            {
              "field": "type",
              "type": "enums",
              "enums": this.searchTypeArr,
              "value": this.searchType
            }
          ]
        }
        Http.post(url, params).then((res) => {
          this.curAgencyList = res.data.content;
          this.totalElements = res.data.totalElements;
        })
      },
      gotoHome() {
        this.$router.push('/home');
      },
      handleTypeClick(tab) {
        this.searchType = tab.name
      },
      handlePageChange(index) {
        this.activePage = index;
        this.handleSearchData();
      },
      goToDetail(userId) {
        this.$router.push({
          name: 'NewAgencyDetail',
          path:'/agency/detail',
          query: {id: userId, type: 1}
        })
      },
      showLoading() {
        this.loading = true
      },
      hideLoading() {
        this.loading = false
      },
      setUserInfo() {
        this.user = storageGet('user') && storageGet('user').userVO;
      }
    },
    mounted() {
      this.setUserInfo();
      this.loadData();
    }
  }
</script>

<style lang="less">
  @import "../../style/search-nav.scss";
  .item-template {
    height: 80px;
  }
</style>
