<template>
    <Cn-page :loading="loading">
        <view slot="body">
            <view class="select-enineering">
                <uni-indexed-list
                    :options="engineeringListFilter"
                    :showSelect="false"
                    @click="confirm"
                ></uni-indexed-list>
            </view>
        </view>
    </Cn-page>
</template>
<script>
import { pinyin } from 'pinyin-pro'
import { queryEngineering } from '@/common/api/engineering'

export default {
    data() {
        return {
            loading: false,
            engineeringList: [],
            options: {},
        }
    },
    computed: {
        engineeringListFilter() {
            let result = []
            this.engineeringList.forEach((item) => {
                let arr = pinyin(item.name[0], { toneType: 'none', type: 'array' })
                let letter = arr[0][0].toUpperCase()
                console.log(letter)
                let index = result.findIndex((item) => item.letter === letter)
                if (index === -1) {
                    result.push({
                        letter,
                        data: [item.name],
                    })
                } else {
                    result[index].data.push(item.name)
                }
            })
            return result
        },
    },
    onLoad(options) {
        this.options = options
        this.engineeringList = uni.getStorageSync('engineeringList')
        this.userInfo = uni.getStorageSync(this.$cacheKey.userInfo)
        if (!(this.userInfo.authorities === 'app_vip_user' || this.userInfo.authorities === 'engineering_user')) {
            
        }
    },
    onShow() {
        queryEngineering().then((res) => {
            this.engineeringList = res.data
        })
    },
    onNavigationBarButtonTap(e) {
        if (this.userInfo.authorities === 'app_vip_user' || this.userInfo.authorities === 'engineering_user') {
            uni.navigateTo({
                url: `/pages/engineering/new`,
            })
        } else {
            uni.showToast({
                title: '暂无权限',
                icon: 'none',
            })
        }
    },
    methods: {
        confirm(e) {
            console.log(e)
            let engineering = this.engineeringList.find((item) => item.name === e.item.name)
            if (this.options.from === 'once') {
                // 创建项目的时候选择工程 用完即删
                uni.setStorageSync('onceSelectEngineering', engineering)
            } else {
                uni.setStorageSync('engineering', engineering)
            }
            uni.navigateBack()
        },
    },
}
</script>
<style lang="scss">
.index {
    padding: 34rpx;
}
</style>
