<template>
    <!-- 人员入场 -->
    <NavBarModal navBarTitle="人员入场" />

    <view :style="{ paddingTop: safeAreaInsets?.top + 54 + 'px' }">
        <view
            :key="item.name"
            class="person-handle"
            v-for="item in handleList"
            @tap="handleTapPersonEntry(item)"
            :style="{
                background: `linear-gradient(to bottom, ${item.bgc_color_1}, ${item.bgc_color_2})`,
            }"
        >
            <image :src="item.icon" mode="widthFix" class="icon-img" />
            <view>
                <view>{{ item.title }}</view>
            </view>

            <up-loading-icon
                :vertical="true"
                duration="2000"
                color="#3c9cff"
                :show="showLoading"
                textColor="#3c9cff"
                text="正在识别中，请稍后..."
                style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%)"
            />
        </view>
    </view>
</template>

<script setup name="personEntry">
import AddIcon from '@/static/image/person/add.png'
import EditIcon from '@/static/image/person/edit.png'
import FaceIcon from '@/static/image/person/face.png'
import ProSettingIcon from '@/static/image/person/pro_setting.png'
import NavBarModal from '@/components/NavBarModal/index.vue'
import { ref } from 'vue'

const showLoading = ref(false) // 是否显示加载中
const { safeAreaInsets } = uni.getSystemInfoSync()
const handleList = [
    {
        name: 'add',
        icon: AddIcon,
        title: '新增人员信息',
        bgc_color_1: '#9ee07f',
        bgc_color_2: '#57cd7e',
        url: `/pages/person-entry/child-pages/addAndEditPerson?type=${1}`,
    },
    {
        name: 'edit',
        icon: EditIcon,
        title: '修改人员信息',
        bgc_color_1: '#14e3fe',
        bgc_color_2: '#3ebefe',
        url: '/pages/person-entry/child-pages/editPerson',
    },

    {
        name: 'face',
        icon: FaceIcon,
        title: '人脸识别查找',
        bgc_color_1: '#d8c1ea',
        bgc_color_2: '#91cbf0',
        url: '/pages/person-entry/child-pages/faceRecognition',
    },
    {
        name: 'proSetting',
        icon: ProSettingIcon,
        title: '上海人员多工程设置',
        bgc_color_1: '#ffb152',
        bgc_color_2: '#ff9130',
        url: '/pages/person-entry/child-pages/projectSetting',
    },
]

// 点击跳转
const handleTapPersonEntry = (item) => {
    if (item.name === 'face') {
        uni.chooseImage({
            count: 1, // 最多选择1张图片
            sizeType: ['original', 'compressed'],
            sourceType: ['camera'], // 选择图片的来源
            isUploadFile: true,
            success: (res) => {
                showLoading.value = true
                const files = [
                    {
                        file: res.tempFiles[0].path,
                        name: 'file',
                        uri: res.tempFiles[0].path,
                    },
                ]
                uni.uploadFile({
                    url: '/bmw/appRecognition/getFaceRecognition',
                    files: files,
                    name: 'file',
                    isUploadFile: true,
                    success: (res) => {
                        console.log(res, 'res人脸识别结果')
                        showLoading.value = false
                        const data = JSON.parse(res.data)
                        if (data.code === 200) {
                            uni.$u.toast('人脸识别成功')
                            setTimeout(() => {
                                uni.navigateTo({
                                    url: `/pages/person-details/index?id=${data.data.userId}`,
                                })
                            }, 500)
                        } else {
                            uni.$u.toast(
                                '人脸识别失败，请重新识别' + data.msg === null ? '' : data.msg,
                            )
                        }
                    },
                    fail: (err) => {
                        showLoading.value = false
                        console.log(err, 'err人脸识别失败')
                        uni.$u.toast('人脸识别失败')
                    },
                })
            },
            fail: (err) => {
                // uni.$u.toast({
                //     title: '选择图片失败',
                //     icon: 'none',
                // })
            },
        })
    } else {
        uni.navigateTo({
            url: item.url,
        })
    }
}
</script>

<style scoped lang="scss">
.person-handle {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 70rpx 0;
    border-radius: 20rpx;
    margin: 20rpx;
    .icon-img {
        width: 64rpx;
        height: 64rpx;
    }

    view {
        margin-left: 28rpx !important;
        color: #fff;
        font-size: 48rpx;
        font-weight: bold;
        margin-left: 20rpx;
        font-family: PingFang SC;
    }
}
</style>
