<template>
  <view class="page">
    <qn-header>
      <!-- <view>
        <uni-data-picker
          v-model="area"
          :border="false"
          class="qn-picker"
          placeholder="区域"
          popup-title="请选择城市"
          :map="{ text: 'name', value: 'id' }"
          @change="onAreaChange"
          :clear-icon="true"
          :localdata="items"
        ></uni-data-picker>
      </view> -->
      <view>
        <qn-data-picker
          v-model="params.cooperationState"
          :border="false"
          class="qn-picker"
          placeholder="客户类型"
          popup-title="请选择客户类型"
          :map="{ text: 'name', value: 'id' }"
          @change="onStatusChange"
          :clear-icon="false"
          :localdata="cooperationStateList"
        ></qn-data-picker>
      </view>
    </qn-header>
    <view class="content">
      <scroll-list ref="list" :option="option" @load="upCallback" @refresh="downCallback">
        <packingStationItem style="margin-bottom: 20rpx" v-for="item in list" :key="item.enterpriseId" :info="item"></packingStationItem>
        <!-- <packingStationItem></packingStationItem> -->
      </scroll-list>
    </view>
  </view>
</template>

<script>
import { go2 } from '@/utils/hook.js'
import qnHeader from '@/components/qn-header/qn-header.vue'
import { getCompanyList } from '@/apis/clientListApi'
import scrollList from '@/components/scroll-list/scroll-list.vue'
import packingStationItem from '@/components/bussiness-components/packingStationItem.vue'
export default {
  components: {
    qnHeader,
    scrollList,
    packingStationItem
  },
  data() {
    return {
      // items: [],
      // area: null,
      params: {
        cooperationState: 0,
        latitude: 23.12616,
        longitude: 113.38466,
        km: 5
      },
      pagination: {
        pageNum: 0, // 初始会执行一次下拉加载
        pageSize: 10
      },
      cooperationStateList: Object.freeze([
        {
          id: 0,
          name: '全部客户'
        },
        {
          id: 1,
          name: '未合作'
        },
        {
          id: 2,
          name: '已合作'
        }
      ]),
      list: [],
      option: {
        size: 10,
        auto: true,
        emptyText: '暂无数据~',
        background: '#F7F8FA'
      }
    }
  },
  methods: {
    onStatusChange(e) {
      this.pagination.pageNum = 1
      this.$refs.list.refresh()
      // this.getList()
      // const value = e.detail.value
    },
    getList() {
      return new Promise((resolve, reject) => {
        getCompanyList({ ...this.params, ...this.pagination })
          .then((res) => {
            if (res) {
              if (res.current == 1) {
                this.list = res.records
              } else {
                this.list = this.list.concat(res.records)
              }
              resolve({ list: this.list, total: res.total })
            } else {
              reject()
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    downCallback() {
      this.pagination.pageNum = 1
      this.getList()
        .then(({ list, total }) => {
          this.$refs.list.refreshSuccess({ list, total })
        })
        .catch(() => {
          this.$refs.list.refreshFail()
        })
    },
    upCallback(page) {
      this.pagination.pageNum++
      this.getList()
        .then(({ list, total }) => {
          this.$refs.list.loadSuccess({ list, total })
        })
        .catch(() => {
          this.$refs.list.loadFail()
        })
    }
  },
  watch: {
    params: {
      handler(val) {
        this.getList()
      },
      deep: true
    }
  },
  computed: {
    hasLogin() {
      return this.$store.state.qnToken != ''
    }
  },
  onLoad(option) {
    if (option) {
      option.latitude && (this.params.latitude = option.latitude)
      option.longitude && (this.params.longitude = option.longitude)
      option.km && (this.params.km = option.km)
    }
  }
}
</script>

<style lang="scss">
.page {
  display: flex;
  flex-direction: column;
  flex: 1;
  height: 100vh;
  .content {
    // flex: 1;
    flex-grow: 1;
    overflow: hidden;
  }
}
.qn-picker {
  border: none;
}
</style>
