<template>
  <view>
    <IndexList :dataList="dataList" nameKey="schoolName" idKey="schoolId" v-model:current="current" @click="click"></IndexList>
  </view>
</template>

<script>
  import IndexList from '@/components/liu-indexed-list/liu-indexed-list.vue'
  import { bindSchool } from '@/jtools/api/login.js'
  import useUserStore from '@/jtools/store/user.js'
  export default {
    components: {
      IndexList
    },
    data() {
      return {
        current: useUserStore().userInfo.schoolId,
        dataList: []
      }
    },
    created() {
      this._getSchoolOptions()
    },
    methods: {
      _getSchoolOptions() {
        uni.request({
          url: 'https://xueche.ahduima.com/duima/applet/school/list'
        }).then(resp => {
          if(resp.data.code == 200) {
            this.dataList = resp.data.data
          }
        })
      },
      click() {
        // 设置用户驾校
        const {schoolId,schoolName,phone } = this.dataList.find(it=>it.schoolId == this.current)
        const params = {schoolId, schoolName, schoolPhone: phone}
        bindSchool(params).then(resp => {
          if(resp.code == '0000') {
            useUserStore().userInfo.schoolId = schoolId
            useUserStore().userInfo.schoolName = schoolName
            useUserStore().userInfo.schoolPhone = phone
            uni.showToast({
              title: '绑定驾校成功！',
              icon: 'none'
            })
            // 然后返回
            uni.navigateBack()
          }
        })
      }
    }
  }
</script>

<style>

</style>