<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="testitemAdd" v-auth="'role.create'"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="query.search" placeholder="名称" clearable @keyup.enter="handleQuery"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="handleQuery"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id" hidePagination>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="名称" prop="name"></el-table-column>
				<el-table-column label="检验类型" prop="field_type">
					<template #default="scope">
						<span v-for="(item,index) in scope.row.tags" :key="item">{{ tags_[item] }} <span v-if="index < scope.row.tags.length - 1">、</span></span>
					</template>
				</el-table-column>
				<el-table-column label="类型" prop="field_type">
					<template #default="scope">{{ field_type_[scope.row.field_type] }}</template>
				</el-table-column>
				<el-table-column label="描述" prop="description"></el-table-column>
				<el-table-column label="物料系列标签" prop="mcate_tags" v-if="base_code !== 'bxerp'">
					<template #default="scope">
						<span v-for="item in scope.row.mcate_tags" :key="item">{{ item }}、</span>
					</template>
				</el-table-column>
				<el-table-column label="选项" prop="options">
					<template #default="scope">
						<div v-for="item in scope.row.options" :key="item">{{ item }}</div>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="140">
					<template #default="scope">
						<el-button link size="small" @click="testitemEdit(scope.row, scope.$index)" v-auth="'role.update'" type="primary">编辑</el-button>
						<el-divider direction="vertical"></el-divider>
						<el-popconfirm title="确定删除吗？" @confirm="testitemDel(scope.row, scope.$index)">
							<template #reference>
								<el-button link size="small" v-auth="'role.delete'" type="danger">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog
		v-if="saveVisible"
		ref="saveDialog"
		@success="handleSuccess"
		@closed="saveVisible = false"
	>
	</save-dialog>
</template>
<script>
	import saveDialog from "./testItem_form.vue";
	export default {
		name: 'testitem',
		components: { saveDialog },
		data() {
			return {
				base_code: this.$TOOL.data.get('BASE_INFO').base.base_code,
				apiObj: this.$API.qm.testitem.list,
				query: {
					search:''
				},
				saveVisible : false,
				type: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				field_type_: {
					"input-int":"整数",
					"input-number":"小数",
					"input-text":"文本",
					"select":"单选",
					"selects":"多选",
				},
				tags_:{
					"first":"首件检验",
					"prod":"成品检验",
					"performance":"性能检验"
				},
			}
		},
		mounted() {
		},
		methods: {
			//添加
			testitemAdd(){
				this.saveVisible = true;
				this.$nextTick(() => {
					this.$refs.saveDialog.open("add");
				});
			},
			//编辑
			testitemEdit(row){
				this.saveVisible = true;
				this.$nextTick(() => {
					this.$refs.saveDialog.open("edit").setData(row);
				});
			},
			//删除
			async testitemDel(row){
				var id = row.id;
				var res = await this.$API.qm.testitem.delete.req(id);
				if(res.err_msg){
					this.$message.error(res.err_msg)
				}else{
					this.$refs.table.refresh();
					this.$message.success("删除成功")
				}
			},
			//搜索
			handleQuery(){
				this.$refs.table.queryData(this.query)
			},
			//本地更新数据
			handleSuccess(data, type){
				this.$refs.table.refresh()
				this.$message.success("操作成功")
			}
		}
	}
</script>
<style scoped></style>
