<template>
	<div class="mod-config">
		<el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
			<el-form-item>
				<el-input v-model="dataForm.key" placeholder="设备类型" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="getDataList()">查询</el-button>
				<el-button v-if="$hasPermission('monitoring:equipmenttype:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
			</el-form-item>
		</el-form>

		<base-table :data="dataList" :table-head-configs="tableConfigs" :max-height="500" @operate-event="handleOperations" @refreshDataList="getDataList" />
		<el-pagination
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
			:current-page="pageIndex"
			:page-sizes="[10, 20, 50, 100]"
			:page-size="pageSize"
			:total="totalPage"
			layout="total, sizes, prev, pager, next, jumper"
		>
		</el-pagination>
		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" :configs="addOrUpdateConfigs" @refreshDataList="getDataList" @destory-dialog="addOrUpdateVisible = false" />
	</div>
</template>

<script>
import { calcMaxHeight } from '@/utils'
import AddOrUpdate from '@/components/base-dialog/addOrUpdate'
// import AddOrUpdate from './equipmentType-add-or-update'
import BaseTable from '@/components/base-table'
import TableOperateComponent from '@/components/base-table/components/operationComponent'
// import TableTextComponent from '@/components/base-table/components/detailComponent'

const tableConfigs = [
	{ prop: 'createTime', name: '添加时间' },
	{ prop: 'name', name: '类型名称' },
	{ prop: 'code', name: '类型编码' },
	{ prop: 'remark', name: '备注' },
	{ prop: 'operations', name: '操作', fixed: 'right', width: 180, subcomponent: TableOperateComponent, options: ['edit', 'delete'] }
]

const addOrUpdateConfigs = {
	type: 'dialog',
	infoUrl: '/monitoring/equipmentType',
	fields: [
		{ name: 'name', label: '设备类型名称' },
		{ name: 'code', label: '设备类型编码', api: '/monitoring/equipmentType/getCode' },
		{ name: 'parentId', label: '父类', type: 'cascader', props: { label: 'name', value: 'id', checkStrictly: true, emitPath: false }, options: [] },
		'remark'
	],
	operations: [
		{ name: 'cancel', showAlways: true },
		{ name: 'save', url: '/monitoring/equipmentType', permission: '', showOnEdit: false },
		{ name: 'update', url: '/monitoring/equipmentType', permission: '', showOnEdit: true }
	],
	extraComponents: [
		{
			name: 'files',
			fieldType: 'array',
			label: '上传资料',
			component: () => import('@/components/base-upload'),
			props: {
				// 上传组件需要的 props
				url: '/monitoring/attachment/uploadFileFormData',
				extraParams: { typeCode: 'EquipmentTypeFile' },
				buttonContent: '点击上传',
				tip: '上传文件大小不要超过 2mb (2048kb)'
			}
		}
	]
}

export default {
	data() {
		return {
			addOrUpdateConfigs,
			calcMaxHeight,
			tableConfigs,
			dataForm: {
				key: ''
			},
			dataList: [],
			pageIndex: 1,
			pageSize: 10,
			totalPage: 0,
			dataListLoading: false,
			dataListSelections: [],
			addOrUpdateVisible: false
		}
	},
	components: {
		AddOrUpdate,
		BaseTable
	},
	activated() {
		this.getTreeEquipmentType()
		this.getDataList()
	},
	methods: {
		// 获取设备类型树形数据
		getTreeEquipmentType() {
			this.$http({
				url: this.$http.adornUrl('/monitoring/equipmentType/getTree'),
				method: 'post'
			}).then(({ data: res }) => {
				if (res && res.code === 0 && res.data.length) {
					this.addOrUpdateConfigs.fields.find(item => item.name === 'parentId').options = res.data
				} else {
					this.addOrUpdateConfigs.fields.find(item => item.name === 'parentId').options.splice(0)
				}
			})
		},
		// 获取数据列表
		getDataList() {
			this.addOrUpdateVisible = false
			this.dataListLoading = true
			this.$http({
				url: this.$http.adornUrl('/monitoring/equipmentType/page'),
				method: 'get',
				params: this.$http.adornParams({
					page: this.pageIndex,
					limit: this.pageSize,
					key: this.dataForm.key
				})
			}).then(({ data }) => {
				if (data && data.code === 0) {
					this.dataList = data.data.list
					this.totalPage = data.data.total
				} else {
					this.dataList = []
					this.totalPage = 0
				}
				this.dataListLoading = false
			})
		},
		// 每页数
		sizeChangeHandle(val) {
			this.pageSize = val
			this.pageIndex = 1
			this.getDataList()
		},
		// 当前页
		currentChangeHandle(val) {
			this.pageIndex = val
			this.getDataList()
		},
		// 多选
		selectionChangeHandle(val) {
			this.dataListSelections = val
		},
		handleOperations({ type, data: id }) {
			switch (type) {
				case 'edit':
					return this.addOrUpdateHandle(id)
				case 'delete':
					return this.deleteHandle(id)
			}
		},
		// 新增 / 修改
		addOrUpdateHandle(id) {
			// 更新树形结构
			this.getTreeEquipmentType()
			this.addOrUpdateVisible = true
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init(id)
			})
		},
		// 删除
		deleteHandle(id) {
			var ids = id
				? [id]
				: this.dataListSelections.map(item => {
						return item.id
				  })
			this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http({
					url: this.$http.adornUrl('/monitoring/equipmentType'),
					method: 'delete',
					data: this.$http.adornData(ids, false, 'raw')
				}).then(({ data }) => {
					if (data && data.code === 0) {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.getDataList()
							}
						})
					} else {
						this.$message.error(data.msg)
					}
				})
			})
		}
	}
}
</script>
