<template>
    <super-dialog
        v-model="dialogVisible"
        :title="title"
        custom-class="notification-dialog"
        top="10%"
        width="30%"
        @closed="closePage"
    >
        <div
            v-loading="detailLoading"
            class="table-container"
            element-loading-background="rgba(10 ,38 ,61, 0.9)"
        >
            <el-checkbox-group v-model="moveType" class="move-type-list">
                <el-checkbox
                    :value="item.code"
                    v-for="item in moveTypeList"
                    :key="item.code"
                    :props="propsData"
                    style="margin: 5px 0"
                    >{{ item.name }}</el-checkbox
                >
            </el-checkbox-group>
        </div>
        <div slot="footer" style="display: flex; justify-content: center; width: 100%">
            <el-button type="primary" @click="submitObject" :loading="btnLoading">确定</el-button>
            <el-button @click="closePage">取消</el-button>
        </div>
    </super-dialog>
</template>

<script lang="ts" setup>
import {
    dataAdd,
    dataSupplierClass
} from '@/api/supplierMonitor/ConfigManagement/MaterialClassificationManagement'
import SuperDialog from '@/components/Dialog/index.vue'
import { refineRequestBody } from '@/utils/requestWrappers'
import { onMounted, reactive, ref, computed } from 'vue'
// 1. 定义 props
const props = defineProps({
    isShowAdd: {
        type: Boolean,
        default: false
    },
    title: {
        type: String,
        default: ''
    },
    info: {
        type: Array,
        default: () => []
    }
})

onMounted(() => {
    getClassList()
})

const btnLoading = ref(false)
const detailLoading = ref<Boolean>(false)
const propsData = { label: 'name', value: 'id', code: 'code' }
const getClassList = async () => {
    detailLoading.value = true
    let params = {
        pageBean: {
            page: -1,
            pageSize: -1
        },
        querys: [
            { group: 'main', operation: 'EQUAL', property: 'status', relation: 'AND', value: '1' }
        ]
    }
    const { total = 0, rows = [] } = await dataSupplierClass(params)
    moveTypeList.value = rows
    detailLoading.value = false
}

// 2. 定义 emit
const emit = defineEmits(['update:isShowAdd','refresh'])

const openPage = () => {}

const closePage = () => {
    dialogVisible.value = false
}

const moveTypeList = ref<any[]>([])
const moveType = ref<number[]>([])

defineExpose({
    openPage
})
// 保存绑定
const submitObject = async () => {
    let arr = []
    moveType.value.forEach((item) => {
        moveTypeList.value.forEach((i) => {
            if (i.code === item) {
                arr.push({
                    supplierMaterialCategoryId: i.id,
                    supplierMaterialCategoryCode: i.code
                })
            }
        })
    })
    let finalArr = []
    props.info.forEach((i) => {
        arr.forEach(item => {
            finalArr.push({ ...i, ...item })
        })
    })
    console.log(finalArr, 22222222)
    finalArr.forEach(item=>{
        item.categoryId = item.id
        item.categoryCode = item.code
        delete item.id
        delete item.code
    })
    try{
        btnLoading.value = true
    const { state, message } = await dataAdd(finalArr)
    if (state) {
        ElMessage.success(message)
        emit('update:isShowAdd', false)
        emit('refresh')
    } else {
        ElMessage.error(message)
    }
    }finally{
        btnLoading.value = false
    }
    
    
}
// 3. 使用 computed 定义双向绑定
const dialogVisible = computed({
    get() {
        return props.isShowAdd
    },
    set(val) {
        emit('update:isShowAdd', false)
    }
})
</script>

<style lang="scss" scoped>
.move-type-list {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    margin: 0 0 20px 10px;
}
</style>
