<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            title="选择商品"
            :async="true"
            width="1000px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="queryParams" label-width="80px" inline>
                <el-form-item prop="type" label="商品分类">
                    <el-tree-select
                        :props="{ children: 'sons', label: 'name', value: 'id' }"
                        class="flex-1"
                        v-model="queryParams.category_id"
                        :data="optionsData.lists.lists"
                        clearable
                        :default-expand-all="false"
                        placeholder="请选择分类"
                        check-strictly
                    />
                </el-form-item>
                <el-form-item prop="type" label="商品搜索">
                    <el-input v-model="queryParams.name" placeholder="请输入商品名称">
                        <template #append>
                            <el-button :icon="Search" @click="resetPage" />
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                </el-form-item>
            </el-form>
            <el-table
                ref="tableRef"
                size="large"
                class="mt-[14px]"
                :data="pager.lists"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" />
                <el-table-column label="商品信息">
                    <template #default="{ row }">
                        <div class="flex items-center">
                            <el-avatar :src="row.image" style="height: 60px; min-width: 60px" />

                            <el-tooltip :content="row.name" placement="top-start">
                                <div class="ml-[10px] text_hidden">{{ row.name }}</div>
                            </el-tooltip>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="商品分类" prop="category_name" />
                <el-table-column label="商品价格" prop="sell_price">
                    <template #default="{ row }"> ￥{{ row.sell_price }} </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
// import { orderRemark, orderdetail } from '@/api/order'
import Popup from '@/components/popup/index.vue'
import { Search } from '@element-plus/icons-vue'
// import { getGoodLists, getGoodsCategory } from '@/api/goods'
import { usePaging } from '@/hooks/usePaging'
import { useDictOptions } from '@/hooks/useDictOptions'
const props = defineProps({
    selected: {
        type: Array
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const tableRef = shallowRef()
const popupRef = shallowRef<InstanceType<typeof Popup>>()

const queryParams = reactive({
    category_id: '',
    name: ''
})

const open = () => {
    popupRef.value?.open()
    getLists()
}
//勾选框处理
const ids = ref<any[]>([])

const handleSelectionChange = (val: any) => {
    ids.value = val.map((item: any) => {
        return item
    })
}

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getGoodLists,
    params: queryParams
})

const getDetail = async (row: Record<string, any>) => {
    // getLists()
}

const handleClose = () => {
    emit('close')
}
const { optionsData } = useDictOptions<{
    lists: any[]
}>({
    lists: {
        api: getGoodsCategory
    }
})

defineExpose({
    open,
    // setFormData,
    getDetail
})
//提交
const handleSubmit = async () => {
    // await orderRemark(formData)
    popupRef.value?.close()
    emit('success', ids.value)
}
</script>
