import { jx_trembling } from "@/utils/tools"
import { ref } from "vue"

/*************************************************
 * 未新建商品
*/
function rightMainFn(props: any, emit: any) {

    /*************************************************
     * 计算sku价格
    */
    function computedPrice(price: any, item: any) {
        let skuNamePrice = Math.round(price * 100) // 一斤的价格
        let specQuality = item.specQuality // 质量
        let specUnit = item.specUnit.toLowerCase() // 单位
        if (specUnit === 'kg' || specUnit === 'l') {
            specQuality = specQuality * 1000
        }
        let temPrice = Math.round(skuNamePrice * specQuality / 500)
        return temPrice
    }

    /*************************************************
     * 修改商品价格
    */
    const focusArr = ref<Array<AnyObject>>([])
    function updateSaleStatus(skuName: AnyObject, sku?: AnyObject) {
        if (!props.isFocus) {
            addFocus(skuName)
            // 修改sku的可售
            sku!.storeSkuStatus = sku!.storeSkuStatus ? 0 : 1
            // 修改 focusArr 的可售
            // 判断修改列表中是否有这个sku
            let focus = focusArr.value.find((item: AnyObject) => item.nameID === skuName.id)
            if (focus) {
                if (sku!.storeSkuStatus) {
                    // 添加sku
                    focus.skus.push({
                        skuID: sku!.id,
                        isSale: 1
                    })
                } else {
                    // 删除sku
                    focus.skus = focus.skus.filter((item: AnyObject) => item.skuID !== sku!.id)
                }
                focus.skus.length == 0 && removeFocus(skuName)
            }
        }
    }

    /*************************************************
     * 添加认领商品
    */
    function addFocus(skuName: AnyObject) {
        if (!focusArr.value.find((item: AnyObject) => item.nameID === skuName.id)) {
            let skus: Array<AnyObject> = []
            skuName.skus.forEach((item: AnyObject) => {
                if (item.storeSkuStatus) {
                    skus.push({
                        skuID: item.id,
                        stock:20,   // 默认20个库存
                        isSale: 1
                    })
                }
            })
            if (!props.isFocus) {
                focusArr.value.push({
                    nameID: skuName.id,
                    unitPrice: Math.round(skuName.price * 100),
                    isFocus: props.isFocus ? -1 : 1,
                    skus
                })
            } else {
                focusArr.value.push({
                    nameID: skuName.id,
                    isFocus: props.isFocus ? -1 : 1,
                })
            }
            emit('newCreateGoods', focusArr.value)
        }
    }

    /*************************************************
     * 删除认领商品
    */
    function removeFocus(skuName: AnyObject) {
        if (focusArr.value.find((item: AnyObject) => item.nameID === skuName.id)) {
            focusArr.value = focusArr.value.filter((item: AnyObject) => item.nameID !== skuName.id)
            emit('newCreateGoods', focusArr.value)
        }
    }

    /*************************************************
     * 输入框失焦事件
    */
    function priceBlur(skuName: AnyObject) {
        if (skuName.price <= 0) {
            skuName.price = 0.01
            skuName.skus = skuName.skus.map((sku: AnyObject) => ({
                ...sku,
                price: 0.01
            }))
        }
    }

    function priceChange(e: AnyObject, skuName: AnyObject) {
        priceChangeTrembling(e, skuName)
    }
    const priceChangeTrembling = jx_trembling((e: AnyObject, skuName: AnyObject) => {
        // 计算其他规格价格
        skuName.skus = skuName.skus.map((sku: AnyObject) => ({
            ...sku,
            price: skuName.unit === '份' ? (computedPrice(e.detail.value, sku) / 100).toFixed(2) : Number(e.detail.value).toFixed(2)
        }))
        let focus = focusArr.value.find((item: AnyObject) => item.nameID === skuName.id)
        if (focus) {
            if (e.detail.value) {
                focus.unitPrice = Math.round(e.detail.value * 100)
            } else {
                focus.unitPrice = 1
            }
        }
    }, 500)

    function removeGoods(e: AnyObject, skuName: AnyObject) {
        if (e.detail.value.length == 0) {
            removeFocus(skuName)
        } else {
            addFocus(skuName)
        }
    }


    return {
        updateSaleStatus,   // 修改商品价格
        priceBlur,          // 失去焦点事件
        priceChange,        // 输入框改变事件
        focusArr,           // 创建商品数据
        removeGoods,        // 移除商品
    }
}

export default rightMainFn