<template>
    <view class="page-container">
        <!-- 入库信息卡片 -->
        <view class="card_wrap">
            <u--form labelPosition="top" :model="warehouseInfo" ref="form" class="address-section">
                <view class="address-header">
                    <u-icon name="car-fill" size="36rpx" color="#108cff" class="location-icon"></u-icon>
                    <text class="address-title">入库信息</text>
                </view>
                <!-- 编码和快递单号同一行 -->
                <u-row class="info-row" justify="space-between">
                    <u-col span="4.5">
                        <u-form-item label="编码" prop="codeStorage">
                            <u--input v-model="warehouseInfo.codeStorage" placeholder="请输入编码" class="info-input" />
                        </u-form-item>
                    </u-col>
                    <u-col span="4.5">
                        <u-form-item label="快递单号" prop="expressOrderNo">
                            <u--input v-model="warehouseInfo.expressOrderNo" placeholder="请输入快递单号" class="info-input" />
                        </u-form-item>
                    </u-col>
                    <u-col span="2">
                        <u-form-item label="物流图片" prop="uploadedImage">
                            <view class="image-uploader" @click="selectImage">
                                <u-icon v-if="!warehouseInfo.uploadedImage" name="camera-fill" size="48rpx"
                                    color="#909399" class="camera-icon"></u-icon>
                                <image v-else :src="warehouseInfo.uploadedImage" mode="aspectFill"
                                    class="image-preview">
                                </image>
                            </view>
                        </u-form-item>
                    </u-col>
                </u-row>

                <!-- 单独一行的收单物品 -->
                <u-row class="info-row" justify="space-between">
                    <u-col span="5.8">
                        <u-form-item label="收单物品" prop="item">
                            <u--input v-model="warehouseInfo.item" placeholder="请输入收单物品" class="info-input" />
                        </u-form-item>
                    </u-col>
                    <!-- 收单类型 -->
                    <u-col span="5.8">
                        <u-form-item label="收单类型" prop="customerServiceNameLabel" @tap="selectCustomerServiceName">
                            <view class="click-wrapper">
                                <u--input v-model="warehouseInfo.customerServiceNameLabel" placeholder="点击选择收单类型"
                                    class="info-input" disabled />
                            </view>
                        </u-form-item>
                        <!-- 收单类型选择 -->
                        <u-picker :show="showCustomerServicePicker" :columns="customerServiceColumns" confirm
                            keyName="label" @confirm="handleConfirmCustomerService"
                            @cancel="showCustomerServicePicker = false"></u-picker>
                    </u-col>
                </u-row>

                <u-row class="info-row" justify="space-between">
                    <!-- 收单类别 -->
                    <u-col span="5.8">
                        <u-form-item label="类别" prop="category" @tap="selectCategory">
                            <view class="click-wrapper">
                                <u--input v-model="warehouseInfo.categoryLabel" placeholder="点击选择类别" class="info-input"
                                    disabled />
                            </view>
                        </u-form-item>
                        <!-- 收单类型选择 -->
                        <u-picker :show="showCategoryPicker" :columns="categoryColumns" confirm keyName="label"
                            @confirm="handleConfirmCategory" @cancel="showCategoryPicker = false"></u-picker>
                    </u-col>


                    <!-- 是否需要查码 -->
                    <u-col span="5.8">
                        <u-form-item label="是否需要查码" prop="needCheckCode" @tap="selectNeedCheckCode">
                            <view class="click-wrapper">
                                <u--input v-model="warehouseInfo.needCheckCodeLabel" placeholder="点击选择是否需要查码"
                                    class="info-input" disabled />
                            </view>
                        </u-form-item>
                        <!-- 是否需要查码选择 -->
                        <u-picker :show="showNeedCheckCodePicker" :columns="needCheckCodeColumns" confirm
                            keyName="label" @confirm="handleConfirmNeedCheckCode"
                            @cancel="showNeedCheckCodePicker = false"></u-picker>
                    </u-col>

                </u-row>



                <!-- 查码费和表款同一行 -->
                <u-row class="info-row" justify="space-between">
                    <u-col span="5.8">
                        <u-form-item label="表款">
                            <u--input v-model="warehouseInfo.watchPrice" placeholder="请输入表款" class="info-input"
                                type="number" />
                        </u-form-item>
                    </u-col>
                    <u-col span="5.8">
                        <u-form-item label="查码费">
                            <u--input v-model="warehouseInfo.checkCodeFee" placeholder="请输入查码费" class="info-input"
                                type="number" :disabled="warehouseInfo.needCheckCode == '2'" />
                        </u-form-item>
                    </u-col>
                </u-row>

                <u-row class="info-row" justify="space-between">
                    <u-col span="5.8">
                        <u-form-item label="好处费">
                            <u--input v-model="warehouseInfo.benefitFee" placeholder="请输入好处费" class="info-input"
                                type="number" />
                        </u-form-item>
                    </u-col>
                    <u-col span="5.8">
                        <u-form-item label="运费">
                            <u--input v-model="warehouseInfo.freight" placeholder="请输入运费" class="info-input"
                                type="number" />
                        </u-form-item>
                    </u-col>
                </u-row>

                <!-- 维修金额和毛业绩同一行 -->
                <u-row class="info-row" justify="space-between">
                    <u-col span="5.8">
                        <u-form-item label="维修金额">
                            <u--input v-model="warehouseInfo.repairAmount" placeholder="请输入维修金额" class="info-input"
                                type="number" />
                        </u-form-item>
                    </u-col>
                    <u-col span="5.8">
                        <u-form-item label="分单比例（0~100）">
                            <u--input v-model="warehouseInfo.splitRatio" placeholder="请输入分单比例(0~100)" class="info-input"
                                type="number" />
                        </u-form-item>
                    </u-col>
                </u-row>
                <!-- 业绩 -->
                <u-row class="info-row" justify="space-between">
                    <u-col span="3.8">
                        <u-form-item label="成本合计">
                            <u--input :disabled="true" :value="computedTotalCost" placeholder="成本合计自动计算"
                                class="info-input" type="number" />
                        </u-form-item>
                    </u-col>
                    <u-col span="3.8">
                        <u-form-item label="业绩">
                            <u--input :disabled="true" :value="computedPerformance" placeholder="业绩自动计算"
                                class="info-input" type="number" />
                        </u-form-item>
                    </u-col>
                    <u-col span="3.8">
                        <u-form-item label="毛业绩">
                            <u--input :disabled="true" :value="computedGrossPerformance" placeholder="毛业绩自动计算"
                                class="info-input" type="number" />
                        </u-form-item>
                    </u-col>
                </u-row>

                <!-- 收单备注 -->
                <u-row class="info-row">
                    <u-col span="12">
                        <u-form-item label="收单备注">
                            <u--textarea v-model="warehouseInfo.remarks" placeholder="请输入收单备注" class="info-input"
                                confirmType="done" rows="4" />
                        </u-form-item>
                    </u-col>
                </u-row>
            </u--form>
        </view>

        <!-- 新添加的卡片 -->
        <view class="card_wrap">
            <view class="address-section">
                <view class="address-header">
                    <u-icon name="list" size="36rpx" color="#108cff" class="location-icon"></u-icon>
                    <text class="address-title">分成信息</text>
                    <u-button type="primary" plain shape="circle" size="mini" class="add-button" @click="addSplit">
                        <u-icon name="plus" size="24rpx" color="#108cff"></u-icon>
                        <text>添加</text>
                    </u-button>
                </view>
                <!-- 分成信息表格 -->
                <view class="split-table">
                    <u-row class="split-table-header">
                        <u-col span="4">
                            <text class="header-text">关联</text>
                        </u-col>
                        <u-col span="2">
                            <text class="header-text">分成人</text>
                        </u-col>

                        <u-col span="2">
                            <text class="header-text">比例</text>
                        </u-col>
                        <u-col span="1">
                            <text class="header-text">类型</text>
                        </u-col>
                        <u-col span="1">
                            <text class="header-text">公司</text>
                        </u-col>
                        <u-col span="2" class="action-column">
                            <text class="header-text">操作</text>
                        </u-col>
                    </u-row>
                    <u-row v-for="(item, index) in profitSharingList" :key="item.uuid" class="split-table-row">
                        <u-col span="4">
                            <view class="table-cell">
                                <!-- <select v-model="item.deptId" class="custom-select">
                                    <option v-for="option in associationOptions" :key="option.id" :value="option.id">
                                        {{ option.label }}
                                    </option>
                                </select> -->
                                <u-button @click='handleSelectOrg(item)' :text="item.orgName" plain></u-button>
                            </view>
                        </u-col>

                        <u-col span="2">
                            <view class="table-cell">
                                <!-- <select v-model="item.userId" class="custom-select">
                                    <option value="">无</option>
                                    <option v-for="person in item.personOptions" :key="person.id" :value="person.id">
                                        {{ person.label }}
                                    </option>
                                </select> -->
                                <u-button @click="handleSelectPerson(item)" :text="item.userName" plain></u-button>
                            </view>
                        </u-col>


                        <u-col span="2">
                            <view class="table-cell">
                                <u--input v-model="item.commissionRate" type="number" class="percentage-input"
                                    @input="handlePercentageInput(item)" min="0" max="100" precision="0" />
                            </view>
                        </u-col>
                        <u-col span="1">
                            <view class="table-cell">
                                <view :class="['account-type', item.accountType == '1' ? 'frontend' : 'backend']"
                                    @click="toggleAccountType(item)" style="cursor: pointer;">
                                    {{ item.accountType == '1' ? '前' : '后' }}
                                </view>
                            </view>
                        </u-col>
                        <u-col span="1">
                            <view class="table-cell">
                                <view class="radio-wrapper" @click="toggleBelongToCompany(item)">
                                    <view :class="['radio-circle', item.isCompanyPerformance == '1' ? 'active' : '']">
                                        <u-icon v-if="item.isCompanyPerformance == '1'" name="checkmark" size="20rpx"
                                            color="#fff"></u-icon>
                                    </view>
                                </view>
                            </view>
                        </u-col>
                        <u-col span="2" class="action-column">
                            <view class="table-cell">
                                <u-button type="error" plain shape="circle" size="mini"
                                    @click="deleteRow(item.id, item.uuid)" class='delectBtn'>
                                    <u-icon name="trash" size="20rpx" color="#ff6b6b"></u-icon>
                                </u-button>
                            </view>
                        </u-col>
                    </u-row>
                </view>
            </view>
        </view>

        <!-- 确认入库按钮 -->
        <div class="confirm-button-container">
            <u-button class='next-btn' type="success" @click="confirmWarehouseEntry" style="border-radius: 11px;">
                <u-icon name="checkmark-circle-fill" size="28rpx" color="#fff"></u-icon>
                <text style="margin-left: 8rpx;">确认入库</text>
            </u-button>
        </div>

        <!-- 组织选择 -->
        <u-picker :show="showOrgPicker" title="请选择组织" :columns="columnsOrgList" keyName="label"
            @confirm="handleOrgConfirmOrg" @cancel=' showOrgPicker = false'></u-picker>


        <!-- 人员选择 -->
        <u-picker :show="showPersonPicker" title="请选择分成人" :columns="columnsPersonList" keyName="label"
            @confirm="handleConfirmPerson" @cancel='handleCancelPerson'></u-picker>

        <u-toast ref="uToast"></u-toast>




    </view>
</template>

<script>
export default {

    props: {
        orderDetail: {
            type: Object,
            default: () => { },
        },
        currentReceipt: {
            type: Object,
            default: () => { },
        },
    },

    watch: {
        currentReceipt: {
            handler(newVal) {
                if (newVal) {
                    console.log('这里是page4', newVal)
                    // "searchValue": null,
                    // "createBy": "12234",
                    // "createTime": "2025-12-25 13:39:25",
                    // "updateBy": "12234",
                    // "updateTime": "2025-12-27 09:33:05",
                    // "remark": null,
                    // "params": {},
                    // "id": "4347",
                    // "sendFormId": "5464",
                    // "clueId": "1973381744953516033",
                    // "item": "测试发单-VV",               //收单物品
                    // "brand": "LV",
                    // "needCheckCode": 1,
                    // "code": "111",
                    // "paymentAmount": 99.00,
                    // "phone": "18692257000",
                    // "tableFee": 999999.00,
                    // "benefitFee": 666.00,            //好处费
                    // "freight": 666666.00,             //运费
                    // "checkCodeFee": 666.00,           //查码费
                    // "totalCost": null,
                    // "sellingPrice": 125000.00,
                    // "performance": null,
                    // "receiptRemark": null,
                    // "repairAmount": 666.00,           //维修金额
                    // "grossPerformance": 666.00,       //毛业绩
                    // "expressOrderNo": "666",         //快递单号
                    // "fileIds": "",
                    // "model": "lvvvv",
                    // "splitRatio": null,
                    // "customerServiceName": "1",
                    // "deptId": "373",
                    // "category": "2",
                    // "delFlag": null,
                    // "idCard": "444",
                    // "paymentMethod": null,
                    // "bankCardNumber": "333",
                    // "bankName": "222",
                    // "customName": "111"
                    const data = newVal
                    this.warehouseInfo = {
                        codeStorage: data.code,//编码
                        expressOrderNo: data.expressOrderNo || '',//快递单号
                        uploadedImage: data.receiptRemark?.split(';')[1] || '',//物流图片 截取备注第二部分
                        item: data.item || '',//收单物品
                        checkCodeFee: data.checkCodeFee || '',//查码费
                        watchPrice: data.tableFee || '',//表款
                        benefitFee: data.benefitFee || '',//好处费
                        freight: data.freight || '',//运费
                        repairAmount: data.repairAmount || '',//维修金额
                        grossPerformance: data.grossPerformance || '',//毛业绩
                        performance: data.performance || '',//业绩
                        remarks: data.receiptRemark?.split(';')[0] || '',//收单备注  截取备注第一部分
                        splitRatio: data.splitRatio || '',//分单比例
                        customerServiceName: data.customerServiceName || '',//收单类型，1收单类，2维修类，3销售类
                        customerServiceNameLabel: this.customerServiceColumns[0].filter(item => item.value == data.customerServiceName)[0].label || '',//收单类型，1收单类，2维修类，3销售类
                        category: data.category || '',//类别
                        categoryLabel: this.categoryColumns[0].filter(item => item.value == data.category)[0].label || '',//类别
                        needCheckCode: data.needCheckCode || '',//是否需要查码
                        needCheckCodeLabel: this.needCheckCodeColumns[0].filter(item => item.value == data.needCheckCode)[0].label || '',//是否需要查码
                    }
                    this.getShareList()
                }
            },
        }
    },
    computed: {

        computedTotalCost() {
            //成本  合计 = 运费 + 好处费+ 查码费 + 表款(支付总额)+ 维修费。
            const freight = this.warehouseInfo.freight || 0
            const benefitFee = this.warehouseInfo.benefitFee || 0
            const checkCodeFee = this.warehouseInfo.checkCodeFee || 0
            const watchPrice = this.warehouseInfo.watchPrice || 0
            const repairAmount = this.warehouseInfo.repairAmount || 0
            return Number(freight) + Number(benefitFee) + Number(checkCodeFee) + Number(watchPrice) + Number(repairAmount)
        },
        computedPerformance() {
            //业绩   //sellingPrice - (totalCost// 成本合计 =   运费1 + 好处费1 + 查码费1 + 表款(支付总额)1+ 维修费1。)
            const sellingPrice = this.currentReceipt.sellingPrice || 0
            const totalCost = Number(this.warehouseInfo.watchPrice) + Number(this.warehouseInfo.freight) + Number(this.warehouseInfo.benefitFee) + Number(this.warehouseInfo.checkCodeFee) + Number(this.warehouseInfo.repairAmount)

            return sellingPrice - totalCost
        },

        computedGrossPerformance() {
            // 毛业绩  //毛利 performance*splitRatio，分单比例
            const performance = Number(this.computedPerformance) || 0
            //保留小数点后两位
            const p = performance * (Number(this.warehouseInfo.splitRatio) / 100 || 0)
            return p.toFixed(2)
        },

    },
    data() {
        return {
            // 入库信息相关的数据
            warehouseInfo: {
                codeStorage: '',//编码
                expressOrderNo: '',//快递单号
                uploadedImage: '',//物流图片
                item: '',//收单物品
                checkCodeFee: '',//查码费
                watchPrice: '',//表款
                benefitFee: '',//好处费
                freight: '',//运费
                repairAmount: '',//维修金额
                grossPerformance: '',//毛业绩
                performance: '',//业绩
                splitRatio: '',//分单比例
                remarks: '',//收单备注
                customerServiceNameLabel: '',//收单类型，1收单类，2维修类，3销售类
                customerServiceName: '',//收单类型，1收单类，2维修类，3销售类

            },
            // 分成信息相关的数据
            profitSharingList: [

            ],
            // 关联选项列表
            // associationOptions: [],
            // 分成人选项列表
            // personOptions: [],
            // 表单验证规则
            showOrgPicker: false,
            showPersonPicker: false,

            columnsOrgList: [],
            //传入的分成人名单
            columnsPersonList: [],
            showCustomerServicePicker: false,//显示选择收单类型的弹窗
            customerServiceColumns: [[
                { label: '收单类', value: '1' },
                { label: '维修类', value: '2' },
                { label: '销售类', value: '3' }
            ]],
            showCategoryPicker: false,//显示选择类别弹窗
            categoryColumns: [[
                { label: '腕表', value: '1' },
                { label: '包包', value: '2' },
                { label: '首饰', value: '4' },
                { label: '其他', value: '3' }
            ]],
            showNeedCheckCodePicker: false,//显示选择是否需要查码弹窗
            needCheckCodeColumns: [[
                { label: '是', value: '1' },
                { label: '否', value: '2' }
            ]],
            currentEditItem: '',

        };
    },
    mounted() {
        // 获取分成人名单
        this.$nextTick(() => {
            this.getCommissionUserList();
        })
    },

    methods: {
        // 选择物流图片
        selectImage() {
            uni.chooseImage({
                count: 1,
                sizeType: ['compressed'],
                sourceType: ['album', 'camera'],
                success: async (res) => {
                    const tempFilePath = res.tempFilePaths[0];
                    //把选择的图片上传给服务器，然后获取返回路径
                    const rep = await uni.$u.api.uploadFile(tempFilePath)
                    if (rep.code == 200) {
                        console.log('上传成功=====>', rep.data.url)
                        this.warehouseInfo.uploadedImage = rep.data.url
                    }
                }
            });
        },
        // 添加分成行
        addSplit() {
            // 添加新行
            this.profitSharingList.push({
                deptId: '',
                accountType: '1',
                userId: '',
                commissionRate: 0,
                isCompanyPerformance: '2',
                orgName: '',
                userName: '',
                id: '',
                uuid: Math.random()//唯一标识，仅vfor使用
            });
            //重新计算全部行的分成比例
            this.calculateTotalPercentage();
        },
        // 重新计算全部行的分成比例
        calculateTotalPercentage() {
            //获取选择前端的行的数量
            const frontItems = this.profitSharingList.filter(item => item.accountType == '1');
            const backItems = this.profitSharingList.filter(item => item.accountType == '2');
            const totalFrontItems = frontItems.length;
            const totalBackItems = backItems.length;
            console.log('选择前端的行的数量====>', totalFrontItems)
            console.log('选择后端的行的数量====>', totalBackItems)
            //把profitSharingList中的前端行的分成比例按照 100/数量 重新计算
            this.profitSharingList.map(item => {
                if (item.accountType == '1') {
                    item.commissionRate = (100 / totalFrontItems).toFixed()
                }
                if (item.accountType == '2') {
                    item.commissionRate = (100 / totalBackItems).toFixed()
                }
            })
        },

        // 切换账户类型
        toggleAccountType(item) {
            item.accountType = item.accountType == '1' ? '2' : '1';
            // 重新计算分成比例
            this.calculateTotalPercentage();
        },
        // 处理百分比输入
        handlePercentageInput(item) {
            // 确保输入是数字
            let value = Number(item.percentage);

            // 验证输入值是否在有效范围内（0-100）
            if (isNaN(value)) {
                item.percentage = 0;
            } else if (value < 0) {
                item.percentage = 0;
            } else if (value > 100) {
                item.percentage = 100;
            } else {
                // 确保是整数
                item.percentage = Math.floor(value);
            }
        },
        // 切换归属公司状态
        toggleBelongToCompany(item) {
            item.isCompanyPerformance = item.isCompanyPerformance == '1' ? '2' : '1';
        },

        // 确认入库方法
        async confirmWarehouseEntry() {


            console.log('确认入库', this.orderDetail.id)

            uni.showModal({
                title: '确认入库',
                content: `是否确认入库改订单：${this.orderDetail.item}？`,
                success: async (res) => {
                    if (res.confirm) {
                        //修改状态
                        await uni.$u.api.oderForm({
                            status: "3",
                            id: this.orderDetail.id,
                        });

                        await uni.$u.api.updateReceiptForm({
                            id: this.currentReceipt.id,
                            code: this.warehouseInfo.codeStorage || '',//编码
                            expressOrderNo: this.warehouseInfo.expressOrderNo || '',//快递单号
                            item: this.warehouseInfo.item || '',//收单物品
                            checkCodeFee: this.warehouseInfo.checkCodeFee || '',//查码费
                            tableFee: this.warehouseInfo.watchPrice || '',//表款
                            benefitFee: this.warehouseInfo.benefitFee || '',//好处费
                            freight: this.warehouseInfo.freight || '',//运费
                            repairAmount: this.warehouseInfo.repairAmount || '',//维修金额
                            grossPerformance: this.computedGrossPerformance || '',//毛业绩
                            performance: this.computedPerformance || '',//毛业绩
                            splitRatio: this.warehouseInfo.splitRatio || '',//分单比例
                            receiptRemark: this.warehouseInfo.remarks + ';' + this.warehouseInfo.uploadedImage || '',//收单备注
                            customerServiceName: this.warehouseInfo.customerServiceName || '',//收单类型，1收单类，2维修类，3销售类
                            category: this.warehouseInfo.category || '',//类别
                            needCheckCode: this.warehouseInfo.needCheckCode || '',//是否需要查码
                            totalCost: this.computedTotalCost || '',//成本合计
                        });

                        //废弃
                        //上传物流表单数据
                        // this.$emit('confirmInterStore', {
                        //     warehouseInfo: this.warehouseInfo,
                        // })
                        //保存入库信息
                        //废弃



                        //上传分成数据
                        await this.addShare()

                        this.$refs.uToast.show({
                            type: "success",
                            message: "入库成功",
                            iconUrl: "https://uviewui.com/demo/toast/success.png",
                            complete() {
                                // uni.navigateTo({
                                //     url: '/pages/pagereceivecenter/pagereceivecenter',
                                // })

                                //退回上一页
                                uni.navigateBack({
                                    delta: 1,
                                    animationType: 'pop-out'
                                })
                            },
                        });

                    } else if (res.cancel) {
                        // 用户点击了取消，不执行任何操作
                        uni.$u.toast('已取消入库');
                    }
                }
            })
        },

        // 获取分成人名单
        async getCommissionUserList() {
            const commissionUserList = await uni.$u.api.getCustomerManagerAllList()
            console.log('分成人名单', commissionUserList.data[0].children)
            // this.associationOptions = commissionUserList.data[0].children
            this.columnsOrgList = [commissionUserList.data[0].children]
        },

        //初始化分成比例
        async getShareList() {

            console.log('当前收单信息', this.currentReceipt.id)
            const { rows, total } = await uni.$u.api.selectCommissionList({
                pageSize: 9999,
                pageNum: 1,
            }, { sendFormId: this.currentReceipt.sendFormId, });
            console.log('分成比例表格数据', rows)
            const newRows = rows.filter(item => item.receiptFormId == this.currentReceipt.id)
            newRows.map(item => {
                item.uuid = Math.random()//唯一标识
            })
            this.profitSharingList = newRows
        },







        //上传分成
        addShare() {
            this.profitSharingList.forEach(async item => {
                const data = {
                    id: item.id || '',
                    accountType: item.accountType == '1' ? 1 : 2,
                    clueId: this.currentReceipt.clueId,
                    commissionRate: item.commissionRate,
                    isCompanyPerformance: item.isCompanyPerformance == '1' ? 1 : 2,
                    sendFormId: this.currentReceipt.sendFormId,
                    userId: item.userId,
                    userName: item.userName,
                    receiptFormId: this.currentReceipt.id,//关联的收单id
                }
                if (item.id) {
                    //更新
                    await uni.$u.api.clueCommissionUpdate(data);
                } else {
                    //新增
                    await uni.$u.api.clueCommissionAdd(data);
                }
            });
        },

        //删除分成
        async deleteRow(id, uuid) {
            console.log(id, 'id', uuid, 'uuid')
            // 确认删除
            uni.showModal({
                title: '确认删除',
                content: '是否确认删除当前行分成比例？',
                success: async (res) => {
                    if (res.confirm) {
                        //如果没有id说明是新增的，直接删除数组中的项
                        if (!id) {
                            this.profitSharingList = this.profitSharingList.filter(item => item.uuid != uuid)
                            uni.$u.toast('删除成功')
                            this.calculateTotalPercentage();

                            return
                        }
                        try {
                            await uni.$u.api.deleteClueCommissionForm(id)
                            uni.$u.toast('删除成功')
                            this.getShareList()
                        } catch (error) {
                            uni.$u.toast('删除失败，请稍后重试')
                        }
                        //重新计算全部行的分成比例
                        this.calculateTotalPercentage();
                    } else {
                        uni.$u.toast('已取消删除')
                    }
                }
            })
        },

        //打开选择框
        handleSelectOrg(item) {
            console.log(item.uuid, '当前选择的行的uuid')
            this.currentEditItem = item.uuid
            console.log(this.currentEditItem, '当前选择的行')
            // this.showOrgPicker = true
            this.showPersonPicker = true
            //把当前的组织赋值给当前的组件
            this.columnsPersonList = this.columnsOrgList//获取到的分成人名单
        },
        //选择当前的分成人
        handleSelectPerson(item) {
            console.log(item, '当前选择的行')
            this.currentEditItem = item.uuid
            //获取当前选择组织的分成人名单
            // 获取当前行的关联id
            const deptId = item.deptId
            //递归查找当前选择的组织
            const org = this.findOrg(this.columnsOrgList[0], deptId)
            // const org = this.columnsOrgList[0].find(org => org.id == deptId)
            console.log(org, '当前选择的组织')
            if (org) {
                this.columnsPersonList = [org.children]
            }
            // 把当前选择组织的分成人名单赋值给当前行的分成人选项列表
            this.showPersonPicker = true
        },
        //递归查找当前选择的组织
        findOrg(orgList, deptId) {
            for (const org of orgList) {
                if (org.id == deptId) {
                    return org
                }
                if (org.children && org.children.length > 0) {
                    const found = this.findOrg(org.children, deptId)
                    if (found) {
                        return found
                    }
                }
            }
            return null
        },

        //确认选择组织
        handleOrgConfirmOrg({ columnIndex, value, values },) {
            console.log(value, '选择的组织')
            //把值赋值给当前行
            this.profitSharingList.forEach(item => {
                if (item.uuid == this.currentEditItem) {
                    item.orgName = value[0].label
                    item.deptId = value[0].id
                    // item.personOptions = value[0].children
                    //将当前的行的分成人清空
                    item.userId = ''
                    item.userName = ''
                }
            })


            this.showOrgPicker = false
        },

        //确认选择人
        handleConfirmPerson({ columnIndex, value, values },) {
            console.log(value[0], '选择的人')
            console.log(value[0].isUser, '是否是用户')
            //把值赋值给当前行
            if (value[0].isUser) {
                //当前选择是用户
                this.profitSharingList.forEach(item => {
                    if (item.uuid == this.currentEditItem) {
                        item.userName = value[0].label
                        item.userId = value[0].id
                    }
                })
                this.columnsPersonList = []
                this.showPersonPicker = false
            } else {
                //当前选择是组织
                // 把组织的label赋值给当前的行的关联
                this.profitSharingList.forEach(item => {
                    if (item.uuid == this.currentEditItem) {
                        item.orgName = value[0].label
                        item.deptId = value[0].id
                    }
                })
                const childrens = value[0].children
                console.log('当前分公司的内部分成人', childrens)

                this.columnsPersonList = [childrens]
            }
        },
        // 取消选择人
        handleCancelPerson() {
            this.columnsPersonList = []
            this.showPersonPicker = false
        },
        //点击选择收单类型
        selectCustomerServiceName() {
            console.log('点击了选择收单类型')

            //打开选择收单类型的弹窗
            this.showCustomerServicePicker = true
        },
        handleConfirmCustomerService({ value }) {
            //选择了收单类型
            console.log(value[0].label, '选择的收单类型')
            console.log(value[0].value, '选择的收单类型')
            //把值赋值给当前行
            this.warehouseInfo.customerServiceNameLabel = value[0].label
            this.warehouseInfo.customerServiceName = value[0].value
            //关闭弹窗
            this.showCustomerServicePicker = false
        },
        //点击选择类别
        selectCategory() {
            console.log('点击了选择类别')
            //打开选择类别弹窗
            this.showCategoryPicker = true
        },
        handleConfirmCategory({ value }) {
            //选择了类别
            console.log(value[0].label, '选择的类别')
            console.log(value[0].value, '选择的类别')
            //把值赋值给当前行
            this.warehouseInfo.categoryLabel = value[0].label
            this.warehouseInfo.category = value[0].value
            //关闭弹窗
            this.showCategoryPicker = false
        },
        //点击选择是否需要查码
        selectNeedCheckCode() {
            console.log('点击了选择是否需要查码')
            //打开选择是否需要查码弹窗
            this.showNeedCheckCodePicker = true
        },
        handleConfirmNeedCheckCode({ value }) {
            //选择了是否需要查码
            console.log(value[0].label, '选择的是否需要查码')
            console.log(value[0].value, '选择的是否需要查码')
            //把值赋值给当前行
            this.warehouseInfo.needCheckCodeLabel = value[0].label
            this.warehouseInfo.needCheckCode = value[0].value
            //关闭弹窗
            this.showNeedCheckCodePicker = false
        },
    }
};
</script>

<style lang="scss" scoped>
// 导入公共样式
@import './common.scss';

// 主样式
.page-container {
    box-sizing: border-box;
    padding: 0;
    background-color: map-get($colors, bg);
    font-family: map-get($font, family);
    -webkit-font-smoothing: map-get($font, smoothing);
    font-smoothing: map-get($font, smoothing);
}

.address-section {
    padding: map-get($sizes, padding-sm) map-get($sizes, padding);

    .u-col {
        padding: 0;
        box-sizing: border-box;
    }

    .u-col:first-child {
        padding-right: 15rpx;
    }

    .u-col:last-child {
        padding-left: 15rpx;
    }
}

// 表单行样式
.info-row {
    margin-bottom: 20rpx;
    box-sizing: border-box;
    padding: 0;
}

// 表单标签样式
.info-label {
    @include font-styles($size: tiny, $weight: regular, $color: tertiary);
    margin-bottom: 8rpx;
    display: block;
}

// 输入框样式
.info-input {
    height: 65rpx;
    border-radius: 8rpx;
    border: 1rpx solid #e5e7eb;
    padding: 20rpx 16rpx;
    width: 100%;
    box-sizing: border-box;
    @include font-styles($size: small, $weight: regular, $color: secondary);

    // textarea特殊样式
    &[type="textarea"] {
        min-height: 160rpx;
        resize: vertical;
        padding-top: 20rpx;
        padding-bottom: 20rpx;
    }
}

// 防止表单label换行
::v-deep .u-form-item__body__left__content__label {
    white-space: nowrap;
}

::v-deep .u-form-item__body {
    padding: 0;
}

// 图片上传样式

.image-uploader {
    width: 100%;
    height: 65rpx;
    border: 2rpx dashed #409eff;
    border-radius: 8rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    background-color: #ecf5ff;
    box-sizing: border-box;
    overflow: hidden;
    transition: all 0.3s ease;

    &:hover {
        border-color: #66b1ff;
        background-color: #f0f9ff;
        transform: scale(1.02);
    }
}

.camera-icon {
    margin: 0;
    font-size: 32rpx;
    color: #409eff;
    transition: all 0.3s ease;
}

.image-uploader:hover .camera-icon {
    color: #66b1ff;
    transform: scale(1.1);
}

.image-preview {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 10rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
}

.image-uploader:hover .image-preview {
    transform: scale(1.02);
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15);
}

.hidden-input {
    display: none;
}

// 分成信息卡片样式
.address-header {
    display: flex;
    align-items: center;
    margin-bottom: 20rpx;

    .address-title {
        @include font-styles($size: small, $weight: medium, $color: primary);
        margin-left: 10rpx;
    }
}

.split-content {
    padding: 10rpx 0;
}

.split-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15rpx 0;
    border-bottom: 1rpx solid map-get($colors, border);

    &:last-child {
        border-bottom: none;
    }
}

.split-label {
    @include font-styles($size: tiny, $weight: regular, $color: tertiary);
}

.split-value {
    @include font-styles($size: small, $weight: regular, $color: secondary);
}

.add-button {
    padding: 0;
    width: 50rpx;
    height: 50rpx;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* 分成信息表格样式 */
.split-table {
    width: 100%;
    margin-top: 20rpx;
    background-color: #fff;
    border-radius: 8rpx;
    overflow: hidden;
}

.split-table-header {
    background-color: #f5f7fa;
    padding: 15rpx 0;
    border-bottom: 1rpx solid #e4e7ed;
}

.split-table-row {
    padding: 15rpx 0;
    border-bottom: 1rpx solid #e4e7ed;
    align-items: center;

    &:last-child {
        border-bottom: none;
    }
}

.header-text {
    @include font-styles($size: tiny, $weight: regular, $color: tertiary);
    text-align: center;
    display: block;
}

.table-cell {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 10rpx;
    box-sizing: border-box;
}

.action-column {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* 自定义下拉框样式 - 隐藏原生箭头 */
.custom-select {
    width: 100%;
    height: 50rpx;
    border-radius: 0;
    border: 1rpx solid #e5e7eb;
    text-align: center;
    /* 增加右侧内边距，为自定义箭头留出空间 */
    @include font-styles($size: tiny, $weight: regular, $color: secondary);
    background-color: #fff;
    /* 确保隐藏所有浏览器的原生箭头 */
    appearance: none !important;
    -webkit-appearance: none !important;
    -moz-appearance: none !important;
    -o-appearance: none !important;
    cursor: pointer;
    /* 使用自定义SVG箭头 */
    background-repeat: no-repeat;
    background-position: right 8rpx center;
    background-size: 12rpx 12rpx;
    box-sizing: border-box;
    position: relative;

    /* 防止箭头在焦点状态下显示 */
    &:focus {
        outline: none;
        border-color: #409eff;
        box-shadow: 0 0 0 2rpx rgba(64, 158, 255, 0.2);
    }

    /* 针对IE/Edge浏览器的特殊处理 */
    &::-ms-expand {
        display: none !important;
    }

    /* 针对某些旧版浏览器的额外处理 */
    &::-webkit-select-arrow {
        display: none !important;
    }

    /* 添加伪元素覆盖可能残留的箭头 */
    &::after {
        content: '';
        position: absolute;
        right: 8rpx;
        top: 50%;
        transform: translateY(-50%);
        width: 12rpx;
        height: 12rpx;
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23909399' d='M6 8l-4-4h8z'/%3E%3C/svg%3E");
        background-repeat: no-repeat;
        background-size: 100% 100%;
        pointer-events: none;
        z-index: 1;
    }
}

/* 百分比输入框样式 */
.percentage-input {
    text-align: center;
    background-color: #f9f9f9;
    color: #606266;
    text-align: center;
    padding: 0rpx !important;
}

/* 账户类型标签样式 */
.account-type {
    padding: 4rpx 16rpx;
    border-radius: 12rpx;
    font-size: 24rpx;
    font-weight: 500;
    color: #fff;
    text-align: center;
}

.account-type.frontend {
    background-color: #409eff;
}

.account-type.backend {
    background-color: #909399;
}

/* 调整表格布局，使其更紧凑 */
.split-table {
    width: 100%;
    margin-top: 10rpx;
}

.split-table-header,
.split-table-row {
    padding: 10rpx 0;
}

.table-cell {
    padding: 0 5rpx;
}



/* 归属公司单选框样式 */
.radio-wrapper {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.radio-circle {
    width: 32rpx;
    height: 32rpx;
    border-radius: 50%;
    border: 2rpx solid #dcdfe6;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.radio-circle.active {
    border-color: #67c23a;
    background-color: #67c23a;
}

/* 确认入库按钮样式 */
.confirm-button-container {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 40rpx 0;
    margin-top: 20rpx;
}

/* 响应式布局调整 */
@media screen and (max-width: 750rpx) {
    .split-table {
        overflow-x: auto;
    }

    .custom-select {
        width: 100%;
    }

    .account-type {
        padding: 2rpx 10rpx;
        font-size: 22rpx;
    }
}

@media screen and (min-width: 751rpx) {
    .table-cell {
        padding: 0 10rpx;
    }
}

.u-button {
    height: 60rpx;
}

.delectBtn {
    height: 40rpx;

    .u-button {
        width: 40rpx;
    }



}

.delectBtn ::v-deep .u-button--mini {
    min-width: 20rpx !important;
}



.next-btn {
    position: fixed;
    bottom: 10rpx;
    left: 2.5%;
    width: 95%;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    @include font-styles($size: medium, $weight: bold, $color: #fff);
    border-radius: 0rpx 0rpx 20rpx 20rpx;
}
</style>
