<template>
	<div class="NCC-common-layout">
		<div class="NCC-common-layout-center">
			<!-- 查询条件 -->
			<el-row class="NCC-common-search-box" :gutter="16">
				<el-form @submit.native.prevent>
					<el-col :span="6">
						<el-form-item label="门店名称">
							<el-select v-model="query.storeId" placeholder="请选择门店" clearable filterable>
								<el-option v-for="store in storeOptions" :key="store.id" :label="store.dm" :value="store.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="产品类型">
							<el-input v-model="query.productType" placeholder="产品类型" clearable />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="是否有效">
							<el-select v-model="query.isEffective" placeholder="是否有效" clearable>
								<el-option label="有效" :value="1" />
								<el-option label="无效" :value="0" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item>
							<el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
							<el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
			<!-- 列表 -->
			<div class="NCC-common-layout-main NCC-flex-main">
				<div class="NCC-common-head">
					<div>
						<el-button type="primary" icon="el-icon-plus" @click="add()">新增</el-button>
					</div>
					<div class="NCC-common-head-right">
						<el-tooltip effect="dark" content="刷新" placement="top">
							<el-link icon="icon-ym icon-ym-Refresh NCC-common-head-icon" :underline="false"
								@click="initData()" />
						</el-tooltip>
						<screenfull isContainer />
					</div>
				</div>
				<NCC-table v-loading="loading" :data="list"
					:header-cell-style="{ background: '#f5f7fa', color: '#606266' }">
					<el-table-column label="门店名称" align="center" >
						<template slot-scope="scope">
							<div class="store-info">
								<i class="el-icon-office-building store-icon"></i>
								<span class="text-nowrap">{{ scope.row.storeName || '无' }}</span>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="产品类型" align="center" >
						<template slot-scope="scope">
							<div class="product-type-info">
								<i class="el-icon-goods product-type-icon"></i>
								<span class="text-nowrap">{{ scope.row.productType || '无' }}</span>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="库存数量" align="center" >
						<template slot-scope="scope">
							<div class="quantity-info">
								<i class="el-icon-box quantity-icon"></i>
								<span class="text-nowrap">{{ scope.row.quantity || 0 }}</span>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="是否有效" align="center" >
						<template slot-scope="scope">
							<el-tag :type="scope.row.isEffective === 1 ? 'success' : 'info'" size="small">
								{{ scope.row.isEffective === 1 ? '有效' : '无效' }}
							</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="创建人" align="center" >
						<template slot-scope="scope">
							<div class="user-info">
								<i class="el-icon-user user-icon"></i>
								<span class="text-nowrap">{{ scope.row.createUserName || '无' }}</span>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="创建时间" align="center" width="180" prop="createTime">
						<template slot-scope="scope">
							<div class="time-info">
								<i class="el-icon-time time-icon"></i>
								<span class="text-nowrap">{{ formatDateTime(scope.row.createTime) || '无' }}</span>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="更新人" align="center" >
						<template slot-scope="scope">
							<div class="user-info">
								<i class="el-icon-user user-icon"></i>
								<span class="text-nowrap">{{ scope.row.updateUserName || '无' }}</span>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="更新时间" align="center" width="180" prop="updateTime">
						<template slot-scope="scope">
							<div class="time-info">
								<i class="el-icon-time time-icon"></i>
								<span class="text-nowrap">{{ formatDateTime(scope.row.updateTime) || '无' }}</span>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="操作" width="180" align="left" fixed="right">
						<template slot-scope="scope">
							<div class="action-buttons">
								<el-button type="text" icon="el-icon-edit" @click="edit(scope.row)" class="edit-btn">
									编辑
								</el-button>
								<el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
									class="delete-btn">
									删除
								</el-button>
							</div>
						</template>
					</el-table-column>
				</NCC-table>
				<pagination :total="total" :page.sync="query.currentPage"
					:limit.sync="query.pageSize" @pagination="initData" />
			</div>
		</div>
		<!-- 表单弹窗 -->
		<FormDialog v-if="formVisible" ref="FormDialog" @refresh="refresh" />
	</div>
</template>

<script>
import request from '@/utils/request'
import FormDialog from './form-dialog.vue'

export default {
	components: { FormDialog },
	data() {
		return {
			list: [],
			loading: false,
			total: 0,
			storeOptions: [],
			query: {
				currentPage: 1,
				pageSize: 20,
				storeId: undefined,
				productType: undefined,
				isEffective: undefined
			},
			formVisible: false
		}
	},
	created() {
		this.initData()
		this.loadStoreOptions()
	},
	methods: {
		// 初始化数据
		initData() {
			this.loading = true
			let query = { ...this.query }
			// 移除空值
			Object.keys(query).forEach(key => {
				if (query[key] === undefined || query[key] === null || query[key] === '') {
					delete query[key]
				}
			})
			request({
				url: '/api/Extend/LqStoreConsumableInventory/GetList',
				method: 'GET',
				data: query
			}).then(res => {
				if (res.code == 200 && res.data) {
					this.list = res.data.list || []
					this.total = res.data.pagination ? res.data.pagination.total : 0
				} else {
					this.list = []
					this.total = 0
				}
				this.loading = false
			}).catch(() => {
				this.loading = false
				this.list = []
				this.total = 0
			})
		},
		// 加载门店选项
		loadStoreOptions() {
			request({
				url: '/api/Extend/LqMdxx',
				method: 'GET',
				data: {
					currentPage: 1,
					pageSize: 1000
				}
			}).then(res => {
				if (res.code == 200 && res.data && res.data.list) {
					this.storeOptions = res.data.list
				} else {
					this.storeOptions = []
				}
			}).catch(() => {
				this.storeOptions = []
			})
		},
		// 查询
		search() {
			this.query.currentPage = 1
			this.initData()
		},
		// 重置
		reset() {
			this.query = {
				currentPage: 1,
				pageSize: 20,
				storeId: undefined,
				productType: undefined,
				isEffective: undefined
			}
			this.initData()
		},
		// 新增
		add() {
			this.formVisible = true
			this.$nextTick(() => {
				this.$refs.FormDialog.init()
			})
		},
		// 编辑
		edit(row) {
			this.formVisible = true
			this.$nextTick(() => {
				this.$refs.FormDialog.init(row.id)
			})
		},
		// 删除
		handleDelete(row) {
			this.$confirm(`确定要删除该库存记录吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				request({
					url: `/api/Extend/LqStoreConsumableInventory/${row.id}`,
					method: 'DELETE'
				}).then(res => {
					this.$message({
						type: 'success',
						message: res.msg || '删除成功',
						onClose: () => {
							this.initData()
						}
					})
				})
			}).catch(() => { })
		},
		// 刷新
		refresh() {
			this.formVisible = false
			this.initData()
		},
		// 格式化日期时间
		formatDateTime(dateTime) {
			if (!dateTime) return ''
			const date = new Date(dateTime)
			const year = date.getFullYear()
			const month = String(date.getMonth() + 1).padStart(2, '0')
			const day = String(date.getDate()).padStart(2, '0')
			const hours = String(date.getHours()).padStart(2, '0')
			const minutes = String(date.getMinutes()).padStart(2, '0')
			const seconds = String(date.getSeconds()).padStart(2, '0')
			return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
		}
	}
}
</script>

<style lang="scss" scoped>
// 通用文本不换行样式
.text-nowrap {
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	max-width: 100%;
}

// 信息显示样式
.store-info,
.product-type-info,
.quantity-info,
.user-info,
.time-info {
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 6px;
}

.store-icon {
	color: #409EFF;
	font-size: 16px;
}

.product-type-icon {
	color: #67C23A;
	font-size: 16px;
}

.quantity-icon {
	color: #E6A23C;
	font-size: 16px;
}

.user-icon {
	color: #909399;
	font-size: 16px;
}

.time-icon {
	color: #909399;
	font-size: 16px;
}

// 操作按钮样式
.action-buttons {
	display: flex;
	align-items: center;
	gap: 8px;
}

.edit-btn {
	color: #409EFF;

	&:hover {
		color: #66b1ff;
	}
}

.delete-btn {
	color: #F56C6C !important;

	&:hover {
		color: #f56c6c !important;
		opacity: 0.8;
	}
}

// 表格行悬停效果
::v-deep .el-table__row:hover {
	background-color: #f5f7fa;
}

// 表格头部样式
::v-deep .el-table__header-wrapper {
	.el-table__header {
		th {
			background-color: #f5f7fa;
			color: #606266;
			font-weight: 600;
		}
	}
}
</style>

