import { Image, ScrollView, Text, View } from "@tarojs/components";
import { memo, useEffect } from "react";
import classnames from "classnames";
import styles  from './index.module.scss'
import Popup from "@/components/popup";
import Taro from "@tarojs/taro";
import { alert } from "@/common/common";
import { formatImgUrl } from "@/common/fotmat";
import useCheckAuthorize from "@/use/useCheckAuthorize";
import { GetPayCode } from "@/api/onlinePay";


type Param = {
    show?: true|false,
    onClose?: () => void
}
export default memo(({show = true, onClose}:Param) => {
    const base64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQAQMAAAC6caSPAAAABlBMVEX///8AAABVwtN+AAACFElEQVR42uyaMbKzMAyExVC8kiNwFI5GjsZRcgRKCgb9I69sY+Ik7R+8O/OKF/N1Gnklr1AURVEURf2sBnUd+P9PZdxFpFddRaZ4uhEhAiR+euBznM2q+hCRacXpROSuiJ0vjphG1YfVkRXM2lm5ECFSR/QpYn+hw+hBhMgnRCScCxA0JSJE3t1i4XdrTPa5e5gvFx+R9pBseqdN9AnTO6+9Pobjq08m8ttIKUP02ZdT0GcRaQuxDhOKyWvMTK/VmMi8droMBxEiNUw6Xawhaagxm5LipGRe+KUpEWkVifMz2hDOXcHwHqV/IdI6IvH3U41JaEqhV6HGLhiRuyBhRj7yyIMrKXnhNYw7L0t7Is0i0mF7snW5w+xp17/4w8+1wxBpF4G79ZEIhbWn2yqY3ortIdIu0mHhYrcYxuqwhRvwkhi88HULR6R5xBd3mmwPmlIaqdGciBBJyKmQjEeNddH2VGuMyO8jeaGCeyfnlAzxLVwlp0SkUeTsbv0dedxlNh8TN7qVRyEizSJlFg4exn7Oo/Xy9zEJSaQt5KS0hzmnaWuml0jDSJm1LqakwbNw28X0ErkPUuaUxg0F40mV+FooRIjUs9Y4n31S8ljTS40RIYLgW1yoxAQkPMzbtC2RlpF0i0mekrwpESFSn5IUq9vYkJba2pbIfZBT1trfkUNSpc/BNiJEKIqiKIqi/gv9CwAA//9IKiY1VjApygAAAABJRU5ErkJggg=='



    const {check} = useCheckAuthorize({scope:'scope.writePhotosAlbum', msg:'您没授权，无法保存图片'})
    const saveImageCheck = async () => {
        const res = await check()
        res&&saveImage()
    }
    //保存图片
    const saveImage = () => {
        const save = Taro.getFileSystemManager()
        const number = Math.random();
        const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64) || [];
        let filePath = Taro.env.USER_DATA_PATH +'/pic.'+ format
        alert.loading('正在保存图片')
        save.writeFile({
            filePath: filePath,
            data: base64,
            encoding: 'base64',
            success: function (res) {
                console.log('aa::',res)
                console.log('aa2::',filePath)
                Taro.saveImageToPhotosAlbum({
                    filePath: filePath,
                    success: function (res) {
                        alert.success('图片保存成功')
                    },
                    fail: function (err) {
                        console.log('err::', err)
                    }
                })
            }
        })
    }
    //预览图片
    const showImage = () => {
        Taro.previewImage({
            current: formatImgUrl(''), // 当前显示图片的http链接
            urls: [formatImgUrl('')] // 需要预览的图片http链接列表
        })
    }

    // const {fetchData} = GetPayCode()
    // useEffect(() => {
    //     fetchData({
    //         title: "面料销售电子确认单",
    //         company: "什么什么公司123",
    //         order_type: "散剪",
    //         sale_user: "小崔",
    //         order_created_time:"2022/02/01 12:32:13",
    //         order_no:"XS-211005888",
    //         department:"嘻嘻嘻",
    //         shipment_mode:"自提",
    //         target_user_name:"大崔",
    //         target_address:"阿斯顿发斯蒂芬",
    //         target_description:"无",
    //         pay_account:"1234567890123450001",
    //         bank_account_name:"佛山市浩川长盛科技有限公司",
    //         bank_name:"招商银行佛山分行禅城支行",
    //         pay_type:"现结",
    //         client:"客户名称",
    //         phone:"15818085802",
    //         order_total_length:"12",
    //         order_total_price:"23000",
    //         qrcode:"www.zzfzyc.com/checkorder/XS-211005888",
    //         order_total_weight:"300.00",
    //         list: [{product_code:'5215',product_name:'26S双纱亲水滑爽棉',product_color_code:'053',product_color_name:'洋红',num:'4',weight:'123.23',sale_price:'43',total_price:'4510.7'}]
    //     })
    // }, [])
    
    
    //复制功能
    return (
        <View className={styles.scanPay_main}>
            <Popup show={show} showTitle={false} onClose={onClose}>
                <View className={styles.scanPay_con}>
                    <View className={classnames('iconfont icon-a-moreback', styles.miconfont_title)} onClick={onClose}></View>
                    <View className={styles.title}>扫码支付</View>
                    <View className={styles.desc}>
                        <Text className={classnames(styles.miconfont, 'iconfont, icon-zhuyi')}></Text>
                        扫码支付成功后，自动更新状态
                    </View>
                    <ScrollView scrollY className={styles.scanPay_list}>
                        <Image mode="widthFix" src={base64} onClick={showImage}></Image>
                    </ScrollView>
                    <View className={styles.btns} onClick={saveImageCheck}>保存电子确认单</View>
                </View>
            </Popup>
        </View>
        
    )
})