<template>
	<div class="page">
		<el-form :inline="true" class="query-form m-b-10" v-if="searchVisible" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
			<!-- 搜索框-->
			<el-form-item label="产品名称" prop="name">
				<el-input v-model="searchForm.name" placeholder="请输入产品名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="创建时间" prop="dates">
				<el-date-picker
					style=""
					placement="bottom-start"
					value-format="YYYY-MM-DD HH:mm:ss"
					v-model="searchForm.dates"
					type="datetimerange"
					range-separator="至"
					start-placeholder="开始日期"
					end-placeholder="结束日期">
				</el-date-picker>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" @click="refreshList()" icon="el-icon-search">查询</el-button>
				<el-button @click="resetSearch()" icon="el-icon-refresh-right">重置</el-button>
			</el-form-item>
		</el-form>

		<div class="jp-table top" style="">
			<vxe-toolbar :refresh="{query: refreshList}" ref="toolbarRef" custom>
				<template #buttons>
					<el-button v-if="hasPermission('wdt:merchantProduct:add')" type="primary" icon="el-icon-plus" @click="add()">新建</el-button>
					<el-button v-if="hasPermission('wdt:merchantProduct:del')" type="danger"  icon="el-icon-delete" @click="del()" :disabled="$refs.infoTable && $refs.infoTable.getCheckboxRecords().length === 0" plain>删除</el-button>
				</template>
			</vxe-toolbar>
			<div style="height: calc(100% - 80px)">
				<vxe-table
					border="inner"
					auto-resize
					resizable
					height="auto"
					:loading="loading"
					ref="infoTable"
					show-header-overflow
					show-overflow
					highlight-hover-row
					:menu-config="{}"
					:sort-config="{remote:true}"
					:export-config="{
                    remote: true,
                    filename: `入驻平台数据${moment(new Date()).format('YYYY-MM-DD')}`,
                    sheetName: `入驻平台数据${moment(new Date()).format('YYYY-MM-DD')}`,
                    exportMethod: exportMethod,
                    types: ['xlsx'],
                    modes: ['current', 'selected', 'all']
                  }"
					:data="dataList"
					:checkbox-config="{}">
					<vxe-column type="seq" width="60" title="序号"></vxe-column>
					<vxe-column type="checkbox" width="60" ></vxe-column>
					<vxe-column min-width="130" title="产品名称" field="productName" align="center">
						<template #default="scope">
							<el-link  type="primary" :underline="false" v-if="hasPermission('wdt:merchantProduct:view')" @click="view(scope.row.id)">{{scope.row.name}}</el-link>
							<span v-else>{{scope.row.name}}</span>
						</template>
					</vxe-column>
					<vxe-column width="120" title="种类数量" field="salesVolume" align="center"></vxe-column>
					<vxe-column width="120" title="销售额(万元)" field="saleroom" align="center"></vxe-column>
					<vxe-column width="120" title="月销售额(万元)" field="monthlySalesVolume" align="center"></vxe-column>
					<vxe-column width="120" title="排序" field="sort" align="center"></vxe-column>
					<vxe-column width="180" title="创建时间" field="createTime" align="center"></vxe-column>

					<vxe-column title="操作" width="130px" fixed="right" align="center">
						<template  #default="scope">
							<el-button v-if="hasPermission('wdt:merchantProduct:edit')" text type="primary" @click="edit(scope.row)">修改</el-button>
							<el-button v-if="hasPermission('wdt:merchantProduct:del')" text type="primary"  @click="del(scope.row.id)">删除</el-button>
						</template>
					</vxe-column>
				</vxe-table>
				<vxe-pager
					background
					:current-page="tablePage.currentPage"
					:page-size="tablePage.pageSize"
					:total="tablePage.total"
					:page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
					:layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
					@page-change="currentChangeHandle">
				</vxe-pager>

				<MerchantProductForm  ref="merchantProductForm" @refreshDataList="refreshList"></MerchantProductForm>
			</div>
		</div>
	</div>
</template>

<script>
	import merchantProductService from '@/api/wdt/merchantProduct/MerchantProductService'
	import MerchantProductForm from './MerchantProductForm'
	export default {
		data () {
			return {
				searchVisible: true,
				num: 0,
				visable: false,
				gridData: [],
				searchForm: {
					name: '',
					dates: [],
					type: [],
				},
				dataList: [],
				tablePage: {
					total: 0,
					currentPage: 1,
					pageSize: 10,
					orders: []
				},
				loading: false,
				isAdmin: false
			}
		},
		created () {
		},
		components: {
			MerchantProductForm
		},
		mounted () {
			this.$nextTick(() => {
				//将表格和工具栏进行关联
				const $table = this.$refs.infoTable;
				const $toolbar = this.$refs.toolbarRef;
				$table.connect($toolbar);
			})
			this.refreshList()
		},
		activated () {
			this.refreshList()
		},
		computed: {
			userName () {
				return this.$store.state.user.name
			},
			user () {
				this.create = this.$store.state.user.id
				console.log('createId', this.create)
				return this.$store.state.user
			}
		},
		methods: {
			// 新增
			add () {
				this.$refs.merchantProductForm.init('add', '')
			},

			// 查看
			view (id) {
				this.$refs.merchantProductForm.init('view', id)
			},
			edit (row) {
				this.$refs.merchantProductForm.init('edit', row.id)
			},
			// 获取数据列表
			refreshList () {
				this.loading = true
				merchantProductService.list({
					'current': this.tablePage.currentPage,
					'size': this.tablePage.pageSize,
					'orders': this.tablePage.orders,
					...this.searchForm
				}).then((data) => {
					this.dataList = data.records
					this.tablePage.total = data.total
					this.loading = false
				})
			},
			// 删除
			del (id) {
				let ids = id || this.$refs.infoTable.getCheckboxRecords().map(item => {
					return item.id
				}).join(',')
				this.$confirm(`确定删除所选项吗?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.loading = true
					merchantProductService.delete(ids).then((data) => {
						this.$message.success(data)
						this.refreshList()
						this.loading = false
					})
				})
			},
			resetSearch () {
				this.$refs.searchForm.resetFields()
				this.refreshList()
			},
			// 下载文档
			exportFile () {
				this.loading = true
				merchantProductService.exportFile({
					'itemType': '1',
					...this.searchForm
				}).then((res) => {
					// 将二进制流文件写入excel表，以下为重要步骤
					this.$utils.downloadExcel(res, '产品信息表')
					this.loading = false
				}).catch(function (err) {
					this.loading = false
					if (err.response) {
						console.log(err.response)
					}
				})
			},
			// 当前页
			currentChangeHandle ({ currentPage, pageSize }) {
				this.tablePage.currentPage = currentPage
				this.tablePage.pageSize = pageSize
				this.refreshList()
			},
			// 自定义服务端导出
			exportMethod ({ options }) {
				// 传给服务端的参数
				const params = {
					'current': this.tablePage.currentPage,
					'size': this.tablePage.pageSize,
					'orders': this.tablePage.orders,
					...this.searchForm,
					filename: options.filename,
					sheetName: options.sheetName,
					isHeader: options.isHeader,
					original: options.original,
					mode: options.mode,
					selectIds: options.mode === 'selected' ? options.map(item => item.id) : [],
					exportFields: options.columns.map(column => column.property)
				}
				return merchantProductService.exportFile(params).then((res) => {
					// 将二进制流文件写入excel表，以下为重要步骤
					this.$utils.downloadExcel(res, options.filename)
				}).catch(function (err) {
					if (err.response) {
						console.log(err.response)
					}
				})
			},
		}
	}
</script>
