<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>
        <uni-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"
        ></uni-data-picker>
      </view>
    </qn-header>
    <view class="content">
      <scroll-list ref="list" :option="option" @load="upCallback" @refresh="downCallback">
        <!-- <packingStationItem v-for="item in list" :key="item.enterpriseId"></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: 1,
        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) {
              console.log('res:', res)
              this.list = res.records
              // this.list = []
              // this.list = [...this.list, ...[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
              resolve({ list: this.list, total: 50 })
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    downCallback() {
      this.pagination.pageNum = 1
      console.log('下拉刷新:')
      this.getList()
        .then(({ list, total }) => {
          this.$refs.list.refreshSuccess({ list, total })
        })
        .catch(() => {
          this.$refs.list.refreshFail()
        })
    },
    upCallback(page) {
      console.log('上拉加载:', page)
      this.pagination.pageNum++
      this.getList()
        .then(({ list, total }) => {
          this.$refs.list.loadSuccess({ list, total })
        })
        .catch(() => {
          this.$refs.list.loadFail()
        })
    }
  },
  watch: {
    params: {
      handler(val) {
        console.log('val:', val)
        // this.getList()
      },
      deep: true
    }
  },
  computed: {
    hasLogin() {
      return this.$store.state.qnToken != ''
    }
  },
  mounted() {
    // getArea().then((res) => {
    //   if (res) {
    //     this.items = res
    //   }
    // })
  },
  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)
    }
    // this.getList()
  }
}
</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>
