<template>
  <div class="pa-3 white fill-height d-flex flex-column">
    <entity-apply-search @entityApplicationSearch="entityApplicationSearch" />
    <entity-apply-date
      :lists="lists"
      :pagination="pagination"
      @entityApplicationSearch="entityApplicationSearch"
    />
  </div>
</template>

<script>
import EntityApplySearch from 'components/EntityApply/EntityApplySearch.vue';
import EntityApplyDate from 'components/EntityApply/EntityApplyDate.vue';
import { entityApplicationSearch } from 'config/api';
import { mapMutations, mapState } from 'vuex';

export default {
  name: 'Home',
  components: {
    EntityApplySearch,
    EntityApplyDate,
  },

  data() {
    return {
      lists: [],
      pagination: { current: 1, pageSize: 10 },
    };
  },

  computed: mapState(['placeType']),

  async created() {
    this.setPlaceType(1);
    await this.entityApplicationSearch();
  },

  methods: {
    ...mapMutations(['setPlaceType']),

    async entityApplicationSearch(condition) {
      try {
        const params = {
          param: {
            placeType: this.placeType,
            pageNum: (condition && condition.current) || 1,
            pageSize: (condition && condition.pageSize) || 10,
          },
        };
        if (condition) {
          if (condition.name) {
            params.param.name = condition.name;
          }
          if (condition.position) {
            params.param.position = condition.position;
          }
          if (condition.phone) {
            params.param.phone = condition.phone;
          }
          if (condition.company) {
            params.param.company = condition.company;
          }
          if (condition.dealStatus) {
            params.param.dealStatus = condition.dealStatus;
          }
        }
        const res = await entityApplicationSearch(params);
        const { code, msg, data } = res.data;
        if (code === 200) {
          this.lists = data.list;
          const paper = { ...this.pagination };
          paper.current = data.pageNum;
          paper.total = +data.total;
          paper.pageSize = data.pageSize;
          this.pagination = paper;
        } else {
          throw msg || '获取失败';
        }
      } catch (error) {
        this.$message.error('123');
      }
    },
  },
};
</script>
