import { ref, watch } from 'vue'
import { onLoad, onPullDownRefresh } from "@dcloudio/uni-app";
import { getStorage } from '@/utils/storage';
import { store } from '@/store'
import toast from "@/utils/toast";
import order from '@/api/https/order';
export default {
    setup() {
        const invoiceInfo = ref<AnyObject>({
            invoice_contact:{
                person:"",
                phone:""
            },
            invoice_mode:"",        // 开具方式
            invoice_material:"",    // 发票材质
            invoice_class:"",       // 发票种类
        })
        const invoice_mode_array = ref([{value:'empty',label:'无'},{value:'SYS_MANUAL',label:'人工开具'},{value:'OPEN_API',label:'开放平台'},{value:'FLASH_INVOICE',label:'闪电开票'},{value:'CODE_INVOICE',label:'扫码开票'},{value:'H5_REDIRECT',label:'H5植入'},{value:'OTHER',label:'其他形式'}])
        const invoice_mode_index = ref<number>(0)

        const invoice_material_array = ref([{value:'empty',label:'无'},{value:'PAPER',label:'纸质'},{value:'ELECTRONIC',label:'电子'}])
        const invoice_material_index = ref<number>(0)

        const invoice_class_array = ref([{value:'empty',label:'无'},{value:'VAT_COMMON',label:'普通发票'},{value:'VAT_SPECIAL',label:'专用发票'},{value:'QUOTA',label:'定额发票'}])
        const invoice_class_index = ref<number>(0)

        const invoice_contact_name = ref('')
        const invoice_contact_phone = ref('')
        const storeName = ref('')   // 门店名称
        const ebaiStore = ref<any>({})
        const logoUrl = ref('https://image.jxc4.com/image/9b9436561e7ff7d8d764787b1aa5182e.jpg')

        onLoad(async () => {
            store.commit('storeInfo/jxLoadingFn', true)
             if (getStorage('terrace') === 'jxgy') logoUrl.value = 'https://image.jxc4.com/image/9e26e6d4e8646d340c21dfe595ac4d08.jpg'
            else if (getStorage('terrace') === 'gblm') logoUrl.value = 'https://image.jxc4.com/image/9b9436561e7ff7d8d764787b1aa5182e.jpg'
            await getStores()
            store.commit('storeInfo/jxLoadingFn', false)
        })


        watch(() => store.getters['storeInfo/platformInfo'],
        (val) => {
            if(val && val.length>0){
                let findItem = val.filter((item:AnyObject) => item.vendorID === 3)
                if(findItem && findItem.length>0) {
                    ebaiStore.value = findItem[0]
                    queryEbStoreInvoice()
                }
            }
        })


        /**
         * 下拉刷新
         */
        onPullDownRefresh(async () => {
            store.commit('storeInfo/jxLoadingFn', true)
            await getStores()
            await queryEbStoreInvoice()
            store.commit('storeInfo/jxLoadingFn', false)
        })

        
        /**
         * 获取门店数据
         */
        async function getStores() {
            await store.dispatch('storeInfo/getOneStore',getStorage("storeID"))
            const stateData = store.state.storeInfo.allStoreInfo
            storeName.value = stateData.name    // 门店名


        }


        /**
         * 查询门店发票设置
         */
        async function queryEbStoreInvoice() {
            let res = await order.query_invoice_setting({
                vendorId:'3',
                vendorStoreID:ebaiStore.value.vendorStoreID

            })
            if(res.code === '0'){
                invoiceInfo.value = res.data[0].settings
                if(invoiceInfo.value.invoice_mode){
                    let findIndex = invoice_mode_array.value.findIndex(item => item.value === invoiceInfo.value.invoice_mode)
                    if(findIndex !== -1 )  invoice_mode_index.value = findIndex
                }

                if(invoiceInfo.value.invoice_material){
                    let findIndex = invoice_material_array.value.findIndex(item => item.value === invoiceInfo.value.invoice_material)
                    if(findIndex !== -1 )  invoice_material_index.value = findIndex
                }

                if(invoiceInfo.value.invoice_class){
                    let findIndex = invoice_class_array.value.findIndex(item => item.value === invoiceInfo.value.invoice_class)
                    if(findIndex !== -1 )  invoice_class_index.value = findIndex
                }
            }
        }





        /**
         * 选择确认框
         */
        function bindPickerChange(e:any,type:string) {
            if(type === 'invoice_mode') invoice_mode_index.value = e.detail.value
            if(type === 'invoice_material') invoice_material_index.value = e.detail.value
            if(type === 'invoice_class') invoice_class_index.value = e.detail.value
        }

        
        /**
         * 保存发票信息
         */
        async function saveInvoiceInfo() {
            try {
                let findItem = store.getters['storeInfo/platformInfo'].filter((item:AnyObject) => item.vendorID === 3)
                if(findItem.length === 0) return toast('未绑定淘宝闪购门店',2)
                // if(!ebStoreInfo || JSON.stringify(ebStoreInfo) === '{}') return toast('未绑定淘宝闪购门店')
                if(+invoice_mode_index.value) invoiceInfo.value.invoice_mode = invoice_mode_array.value[invoice_mode_index.value].value
                if(+invoice_material_index.value) invoiceInfo.value.invoice_material = invoice_material_array.value[invoice_material_index.value].value
                if(+invoice_class_index.value) invoiceInfo.value.invoice_class = invoice_class_array.value[invoice_class_index.value].value
                let res = await order.bath_update_invoice_setting({
                    vendorId:'3',
                    vendorStoreID:findItem[0].vendorStoreID,
                    payload:JSON.stringify(invoiceInfo.value)
                })
                if(res.success.length>0 && !res.success[0].msg) toast('修改成功',1)
                else  toast('修改失败',2)
                uni.hideLoading()
            } catch (error) {
                uni.hideLoading()
                throw error
            }
        }

        return {
            invoiceInfo,                    // 发票信息
            invoice_contact_name,
            invoice_contact_phone,
            invoice_mode_array,             // 开具方式
            invoice_mode_index,             // 开具方式索引
            invoice_material_array,         // 发票材质
            invoice_material_index,         // 发票材质索引
            invoice_class_array,            // 发票种类
            invoice_class_index,            // 发票种类索引
            bindPickerChange,               // 选择确认框
            saveInvoiceInfo,                // 保存发票信息
            storeName,                      // 门店名
            logoUrl                         // logoUrl
        }
    }
}