/**
 * @desc 营业资质
 */

import merchant from "@/api/https/merchant"
import toast from "@/utils/toast"
import { computed, ref } from "vue"
import { qiniuyun } from '@/utils/qiniuUploader'
import { onLoad } from "@dcloudio/uni-app"
import { store } from "@/store"
import { getStorage } from "@/utils/storage"


function businessLicense() {
    const qiniuyunUploadImg = qiniuyun()        // 七牛云sdk


    /**
     * 获取营业执照信息
     */
    onLoad(async () => {
        await getStoreBusiness()
    })
    function getStoreBusiness() {
        let business = store.state.storeInfo.allStoreInfo
        businessData.value.licenceCode = business.licenceCode
        businessData.value.licenceType = business.licenceType
        businessData.value.licenceCorpName = business.licenceCorpName
        businessData.value.licenceOwnerName = business.licenceOwnerName
        businessData.value.licenceAddress = business.licenceAddress
        businessData.value.licenceValid = business.licenceValid
        businessData.value.licence2Code = business.licence2Code
        businessData.value.licenceExpire = business.licenceExpire
        businessData.value.licence2Valid = business.licence2Valid
        businessData.value.licence2Expire = business.licence2Expire
        imgUrls.value.licence = business.licence
        imgUrls.value.licence2Image = business.licence2Image
    }


    /**
     * 营业资质信息
     */
    const businessData = ref({
        licenceCode: '',                    // 营业编号18
        typeArr: ['个人', '公司'],           // 类型
        licenceType: 0,                     // 0个人  1公司
        licenceCorpName: '',                // 公司名称
        licenceOwnerName: '',               // 法人醒姓名
        licenceAddress: '',                 // 营业执照地址
        licenceValid: '',                   // 营业执照有效期开始
        licence2Code: '',                   // 经营许可证编号
        licenceExpire: '',                  // 营业执照有效期结束
        licence2Valid: '',                  // 经营许可证开始时间
        licence2Expire: '',                 // 经营许可证结束时间
    })

    /**
     * 切换营业执照类型
     */
    function typeChange(e: AnyObject) {
        businessData.value.licenceType = e.target.value
    }


    /**
     * 营业执照类型
     */
    const licenceTypeText = computed(() => {
        if (parseInt('' + businessData.value.licenceType, 10) === 0) {
            return '个人'
        } else if (parseInt('' + businessData.value.licenceType, 10) === 1) {
            return '公司'
        } else {
            return ''
        }
    })


    /**
     * 修改时间
     * @param {type}-1：营业执照开始时间 2：营业执照结束时间 3：经营许可证开始时间 4：经营许可证结束时间
     */
    function fnDateChange(e: AnyObject, type: number) {
        if (type == 1) {
            businessData.value.licenceValid = e.detail.value
        }
        if (type == 2) {
            businessData.value.licenceExpire = e.detail.value
        }
        if (type == 3) {
            businessData.value.licence2Valid = e.detail.value
        }
        if (type == 4) {
            businessData.value.licence2Expire = e.detail.value
        }
    }


    /**
     * 上传营业执照
     */
    function imgChangeLicence(data: AnyObject) {
        // 删除
        if (data.type == 'delet') {
            imgUrls.value.licence = ''
        }
        // 上传
        if (data.type == 'upload') {
            uploadImg(data, 'licence')
        }
    }


    /**
     * 上传经营执照
     */
    function imgChangeLicence2Image(data: AnyObject) {
        // 删除
        if (data.type == 'delet') {
            imgUrls.value.licence2Image = ''
        }
        // 上传
        if (data.type == 'upload') {
            uploadImg(data, 'licence2Image')
        }
    }


    /**
     * 上传图片到七牛云
     */
    const uploadUrl = ref<string>('https://image.jxc4.com/')
    const imgUrls:AnyObject = ref({
        licence: '',    // 营业执照
        licence2Image: '',  // 经营执照
    })
    async function uploadImg(data: AnyObject, img: string) {
        let suffix = data.img.indexOf('.png') === -1 ? '.jpg' : '.png'
        let params = {
            suffix
        }
        let res = await merchant.get_qiniu_upload_token(params)
        if (res.code == 0) {
            let filePath = data.img
            qiniuyunUploadImg.upload(
                filePath,
                (res: any) => {
                    // 得到图片网址
                    let imgUrl = uploadUrl.value + res.key
                    imgUrls.value[img] = imgUrl
                },
                (err: AnyObject) => {
                    toast('上传失败，请重试', 2)
                },
                {
                    uploadURL: 'https://up-z2.qiniup.com/',
                    key: res.data.fileName,
                    uptoken: res.data.token,
                }
            )
        } else {
            toast('上传失败', 2)
        }
    }


    /**
     * 提交修改
     */
    function confirmSubmit() {
        uni.jxConfirm({
            title: '提示',
            content: '您确定要修改信息吗？',
            success: async () => {
                let data = {
                    storeID: getStorage('storeID'),
                    payload: JSON.stringify({
                        licenceCode: businessData.value.licenceCode,
                        licenceType: businessData.value.licenceType,
                        licenceCorpName: businessData.value.licenceCorpName,
                        licenceOwnerName: businessData.value.licenceOwnerName,
                        licenceAddress: businessData.value.licenceAddress,
                        licenceValid: businessData.value.licenceValid,
                        licenceExpire: businessData.value.licenceExpire,
                        licence2Code: businessData.value.licence2Code,
                        licence2Valid: businessData.value.licence2Valid,
                        licence2Expire: businessData.value.licence2Expire,
                        licence: imgUrls.value.licence,
                        licence2Image: imgUrls.value.licence2Image
                    })
                }
                let res = await merchant.update_store(data)
                if (res.code == 0) {
                    toast('修改成功', 1)
                    setTimeout(() => {
                        uni.navigateBack()
                    }, 1500)
                } else {
                    toast('修改失败', 2)
                }
            },
        })
    }


    return {
        businessData,               // 营业资质信息
        typeChange,                 // 修改营业执照类型
        licenceTypeText,            // 营业执照类型
        fnDateChange,               // 修改时间
        imgChangeLicence, // 上传图片
        imgChangeLicence2Image, // 上传图片
        confirmSubmit,              // 提交修改
        imgUrls,                    // 上传的图片
    }
}


export default businessLicense