<template />

<script>
import common from '../../common/js/common'
import auth from '../../service/auth'
import { listBizAll, listProjectAll, getDictData } from '../../service/api_data'

import { Dialog, Toast } from 'vant'
export default {
    name: 'BaseData',
    data() {
        return {
            // 分页
            currentPage: 1,
            totalSize: 0,
            pageSize: 10,

            // 移动端分页 loadmore
            InitialLoading: true,
            allLoaded: false,
            bottomStatus: '',

            // 系统用户数据
            StaffData: [],
            StaffMap: {},

            // 供应商数据
            BizData: [],
            BizMap: {},

            // 项目数据
            UserProject: [],
            ProjectData: [],
            ProjectMap: {},

            // 图片预览及其他
            dialogImageUrl: '',
            dialogImageVisible: false,

            // 字典值
            // 通用字典项
            dc_map: {},
            dc_data: {}
        }
    },
    mounted() {
    },
    methods: {
        // 字典数据处理
        initDict: function(_dcKey) {
            const _this = this
            if (!_dcKey)_dcKey = _this.dc_key
            return getDictData(_dcKey).then((res) => {
                if (res.data) {
                    _this.dc_key.forEach(key => {
                        const dictValue = _this.getDictValue(res.data, key, true)
                        _this.dc_data[key] = dictValue
                        _this.dc_map[key] = _this.$common.transDcMap(dictValue)
                    })
                }
            })
        },
        initDict2: function(_dcKey) {
            const _this = this
            if (!_dcKey)_dcKey = _this.dc_key
            return getDictData(_dcKey).then((res) => {
                if (res.data) {
                    _this.dc_key.forEach(key => {
                        const dictValue = _this.getDictValue(res.data, key, false)
                        _this.dc_data[key] = dictValue
                        _this.dc_map[key] = _this.$common.transDcMap(dictValue)
                    })
                }
            })
        },
        initBiz: function() {
            const _this = this
            this.BizData = []
            return listBizAll(auth.uid()).then((res) => {
                if (res.data) {
                    res.data.forEach(function(item) {
                        const json = {
                            label: item.businessName,
                            value: item.id
                        }
                        _this.BizData.push(json)
                        _this.BizMap[item.id] = item.businessName
                    })
                }
            }).catch(() => {
            })
        },
        initProjectData: function() {
            const _this = this
            this.ProjectData = []
            return listProjectAll(auth.uid()).then((res) => {
                if (res.data) {
                    res.data.forEach(function(item) {
                        const json = {
                            label: item.name,
                            value: item.id
                        }
                        _this.ProjectData.push(json)
                        _this.ProjectMap[json.value] = item
                    })
                }
            }).catch(() => {
            })
        },
        pagination(items) {
            const currentPage = this.currentPage
            const perPage = this.pageSize

            const start = currentPage * perPage - perPage
            const end = currentPage * perPage

            return items.slice(start, end)
        },
        getDictValue: function(_dictData, _key, _hasAll) {
            const dictVal = _dictData[_key] ? common.castEval(_dictData[_key].dictValue) : []

            const returnVal = []
            dictVal.forEach(function(item) {
                const json = {
                    id: item.id,
                    isSearch: item.isSearch,
                    label: item.label,
                    value: item.value,
                    active: false
                }
                returnVal.push(json)
            })
            if (_hasAll) {
                const json = {
                    label: '全部',
                    value: '',
                    active: true
                }
                returnVal.unshift(json)
            }

            return returnVal
        },
        // 标准弹框
        confirm: function(txt, title) {
            return new Promise((resolve, reject) => {
                Dialog.confirm({
                    title: title,
                    message: txt
                }).then(res => {
                    // 用户选择去登陆
                    resolve(res)
                }).catch(err => {
                    Toast('已取消')
                    reject(err)
                })
            })
        },
        // 移动版分页加载
        handleBottomChange(status) {
            console.log('handleBottomChange:' + status)
            this.bottomStatus = status
            if (this.bottomStatus === 'drop') {
                this.currentPage++
            }
        },
        loadBottom() {
            const _this = this
            console.log('loadBottom')
            setTimeout(() => {
                _this.getData()
                this.$refs.loadmore.onBottomLoaded()
            }, 500)
        },
        imgPreview: function(fileId) {
            this.dialogImageUrl = '/smartParkH5Server/wx/img/viewImg/' + fileId
            this.dialogImageVisible = true
        },
        imgRemove: function(file, fileList) {
            const index = fileList.indexOf(file)
            fileList.splice(index, 1)
        },
        noImg: function() {
            const img = event.srcElement
            img.src = require('../../assets/no-result.jpg')
            img.onerror = null // 解绑@error事件
        }
    }
}
</script>

<style scoped>

</style>
