<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="商品" prop="store_name">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.store_name"
                        clearable
                        placeholder="请输入商品"
                    />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select
                        v-model="queryParams.status"
                        placeholder="请选择状态"
                        style="width: 240px"
                    >
                        <el-option :value="0" label="未设置"></el-option>
                        <el-option :value="1" label="已设置"></el-option>
                    </el-select>
                </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" show-overflow-tooltip />
                    <el-table-column label="预订单id" prop="bhoid" show-overflow-tooltip />
                    <el-table-column label="采购源id" prop="offer_id" show-overflow-tooltip />
                    <el-table-column label="商品名称" prop="store_name" show-overflow-tooltip />
                    <el-table-column label="商品图片" prop="image" width="120">
                        <template #default="{ row }">
                            <el-image
                                style="width: 50px; height: 50px"
                                :src="row.image"
                                :preview-teleported="true"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column label="采购价" prop="purchase_price" show-overflow-tooltip />
                    <el-table-column label="比率" prop="purchase_lv" show-overflow-tooltip />
                    <el-table-column label="供货价" prop="purchase" show-overflow-tooltip />
                    <el-table-column label="比率" prop="cost_lv" show-overflow-tooltip />
                    <el-table-column label="商户价" prop="cost" show-overflow-tooltip />
                    <el-table-column label="比率" prop="price_lv" show-overflow-tooltip />
                    <el-table-column label="零售价" prop="price" show-overflow-tooltip />
                    <el-table-column label="状态" prop="status_name" show-overflow-tooltip />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['store_product_price.store_product_price/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                                v-if="row.status == 0"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-perms="['store_product_price.store_product_price/delete']"
                                type="danger"
                                link
                                @click="handleEnter(row)"
                                v-if="row.status == 0"
                            >
                                确认
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>

<script lang="ts" setup name="storeProductPriceLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import {
    apiStoreProductPriceLists,
    apiStoreProductPriceDelete,
    apiStoreProductPriceEnterPrice
} from '@/api/store_product_price'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

// 查询条件
const queryParams = reactive({
    store_name: '',
    status: ''
})

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

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

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

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

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 确认
const handleEnter = async (data: any) => {
    await feedback.confirm('是否改价:' + data.store_name)
    await apiStoreProductPriceEnterPrice({ id: data.id })
    getLists()
}

getLists()
</script>
