<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="商品名称" prop="name">
                    <el-input class="w-[280px]" v-model="queryParams.name" clearable placeholder="请输入商品名称" />
                </el-form-item>
                <!-- <el-form-item label="商品品牌" prop="brand">
                    <el-input class="w-[280px]" v-model="queryParams.brand" clearable placeholder="请输入商品品牌" />
                </el-form-item> -->
                <el-form-item label="商品单位" prop="unit">
                    <el-input class="w-[280px]" v-model="queryParams.unit" clearable placeholder="请输入商品单位" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="ID" prop="id" width="55" />
                    <el-table-column label="商品图片" prop="imgs">
                        <template #default="{ row }">
                            <el-image style="width:50px;height:50px;" :src="row.imgs" />
                        </template>
                    </el-table-column>
                    <el-table-column label="商品名称" prop="name" show-overflow-tooltip />
                    <el-table-column label="规格型号" prop="spec" show-overflow-tooltip />
                    <el-table-column label="商品分类" prop="class_name" show-overflow-tooltip />
                    <!-- <el-table-column label="商品品牌" prop="brand_name" show-overflow-tooltip /> -->
                    <el-table-column label="商品单位" prop="unit_name" show-overflow-tooltip />
                    <!-- <el-table-column label="购货价格" prop="buy" show-overflow-tooltip />
                    <el-table-column label="销货价格" prop="sell" show-overflow-tooltip />
                    <el-table-column label="零售价格" prop="retail" show-overflow-tooltip />
                    <el-table-column label="标签" prop="sys_labels_text" show-overflow-tooltip />
                    <el-table-column label="数量" prop="num" width="170">
                        <template #default="{ row }">
                            <el-input type="number" v-model="row.num" clearable placeholder="请输入数量" />
                        </template>
                    </el-table-column> -->
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
            <div class="flex mt-4 justify-end">
                <el-button type="primary" @click="bindFn">确认绑定</el-button>
                <el-button @click="canceFn">取消</el-button>
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="supplierGoodsPop">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiGoodsLists, apiBindGodds } from '@/api/goods'
import { apiSupplierBindGoodsAdd } from '@/api/supplier'
import { defineProps } from "vue"

let props = defineProps({
    sid: {
        type: [Number, String],
        default: 0
    }
})

// 是否显示编辑框
const showEdit = ref(false)
const showDetail = ref(false)

const emits = defineEmits(["offPop"]);

// 查询条件
const queryParams = reactive({
    name: '',
    class: '',
    brand: '',
    unit: '',
    code: '',
    warehouse: '',
    retail_name: '',
    sort: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiGoodsLists,
    params: queryParams
})

const bindFn = async () => {
    let list = pager.lists.filter(item => selectData.value.includes(item.id))
    let data: any = [];

    list.forEach(item => {
        data.push({
            goods_id: item.id,
            price: item.sell,
            goods_name: item.name,
            supplier_id: props.sid,
        })
    })
    await apiSupplierBindGoodsAdd({ 
        data: data
    })
    emits("offPop");
}

const canceFn = () => {
    emits("offPop");
}

getLists()
</script>

