<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<!-- <el-date-picker
                    v-model="query.year"
                    type="year"
                    value-format="YYYY"
                    format="YYYY"
                    placeholder="年"
					style="width: 120px;margin-right: 5px;"
                /> -->
				<el-date-picker
					v-model="query.date"
					type="month"
					value-format="YYYY-MM"
					format="YYYY-MM"
					placeholder="月"
					class="headerSearch"
				/>
				<el-select
					v-model="query.fee"
					placeholder="费用类型"
					clearable
					class="headerSearch"
				>
					<el-option
						v-for="item in feeOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
				<el-select
					v-model="query.mgroup"
					placeholder="工段"
					clearable
					class="headerSearch"
				>
					<el-option
						v-for="item in options"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></el-button>
			</div>
			<div class="right-panel">
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add"
					v-auth="'feeset.create'"
				></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id">
				<el-table-column
					label="#"
					type="index"
					width="50"
				></el-table-column>
				<el-table-column
					label="年"
					prop="year"
					min-width="100"
				></el-table-column>
				<el-table-column
					label="月"
					prop="month"
					min-width="100"
				></el-table-column>
				<el-table-column
					label="单位成本"
					prop="cost_unit"
					min-width="150"
				></el-table-column>
				<el-table-column
					label="关联工段"
					prop="mgroup_name"
					min-width="150"
				></el-table-column>
				<el-table-column label="关联费用" prop="fee" min-width="150">
					<template #default="scope">
						<span>{{ scope.row.fee_.name }}</span>
					</template>
				</el-table-column>
				<el-table-column
					label="操作"
					fixed="right"
					align="center"
					width="140"
				>
					<template #default="scope">
						<el-button
							link
							size="small"
							@click="table_edit(scope.row)"
							v-auth="'feeset.update'"
							type="primary"
							>编辑</el-button
						>
						<el-divider direction="vertical"></el-divider>
						<el-popconfirm
							title="确定删除吗？"
							@confirm="table_del(scope.row, scope.$index)"
						>
							<template #reference>
								<el-button
									link
									size="small"
									v-auth="'feeset.delete'"
									type="danger"
									>删除</el-button
								>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog
		v-if="dialog.save"
		ref="saveDialog"
		@success="handleSaveSuccess"
		@closed="dialog.save = false"
	></save-dialog>
</template>
<script>
import saveDialog from "./feeset_form.vue";
export default {
	name: "dept",
	components: {
		saveDialog,
	},
	data() {
		return {
			apiObj: this.$API.fim.feeset.list,
			query: {
				date: "",
				fee: "",
				mgroup: "",
			},
			dialog: {
				save: false,
			},
			selection: [],
			options: [],
			feeOptions: [],
		};
	},
	mounted() {
		this.getFee();
		this.getMgroup();
	},
	methods: {
		//获取集合列表
		getMgroup() {
			this.$API.mtm.mgroup.list.req({ page: 0 }).then((res) => {
				this.options = res;
			});
		},
		//获取类型列表
		getFee() {
			this.$API.fim.fee.req({ page: 0 }).then((res) => {
				this.feeOptions = res;
			});
		},
		//添加
		table_add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add");
			});
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
		//删除
		async table_del(row) {
			var id = row.id;
			var res = await this.$API.mtm.mgroup.delete.req(id);
			if (res.err_msg) {
				this.$message.error(res.err_msg);
			} else {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			}
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		handleQuery() {
			let year = this.query.date.split("-")[0];
			let month = this.query.date.split("-")[1];
			let query = {};
			query.year = Number(year);
			query.month = Number(month);
			query.fee = this.query.fee;
			query.mgroup = this.query.mgroup;
			this.$refs.table.queryData(query);
		},
		//本地更新数据
		//新增岗位后更新数据
		handleSaveSuccess(data, mode) {
			this.dialog.save = false;
			this.$refs.table.refresh();
		},
	},
};
</script>
<style scoped>
.treeMain {
	width: 100%;
	height: 280px;
	overflow: auto;
	border: 1px solid #dcdfe6;
	margin-bottom: 10px;
}
</style>
