<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="组织名称" prop="factoryName">
				<el-input
					v-model="queryParams.factoryName"
					placeholder="请输入组织名称"
					clearable
					@keyup.enter.native="handleQuery"
				/>
      </el-form-item>
      	<el-form-item label="组织编码" prop="factoryCode">
      		<el-input
      			v-model="queryParams.factoryCode"
      			placeholder="请输入组织编码"
      			clearable
      			@keyup.enter.native="handleQuery"
      		/>
        </el-form-item>
			<el-form-item label="启用状态" prop="status">
				<el-input
					v-model="queryParams.status"
					placeholder="请输入启用状态(1/0)"
					clearable
					@keyup.enter.native="handleQuery"
				/>
          <!-- <el-select v-model="form.status" placeholder="请选择是否启用"  @change="$forceUpdate()" clearable>
            <el-option v-for="item in options" :key="item.status" :label="item.label" :value="item.status"></el-option>
          </el-select> -->
      </el-form-item>
			<el-form-item>
			<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button
					type="primary"
					plain
					icon="el-icon-plus"
					size="mini"
					@click="handleAdd"
					v-hasPermi="['wms:factory:add']"
				>新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="info"
					plain
					icon="el-icon-sort"
					size="mini"
					@click="toggleExpandAll"
				>展开/折叠</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table
			v-if="refreshTable"
			v-loading="loading"
			:data="factoryList"
			row-key="factoryId"
			:default-expand-all="isExpandAll"
			:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
		>
			<el-table-column label="组织名称" align="center" prop="factoryName" width="350"/>
			<el-table-column label="组织编码" align="center" prop="factoryCode" />
			<el-table-column label="创建人" align="center" prop="createBy" />
      		<el-table-column label="创建时间" align="center" prop="createTime" />
      		<el-table-column prop="status" label="是否启用" align="center" >
         		<template slot-scope="scope">
            		{{ scope.row.status == 1 ? "是" : "否" }}
         		</template>
      		</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button
						size="mini"
						type="text"
						icon="el-icon-edit"
						@click="handleUpdate(scope.row)"
						v-hasPermi="['wms:factory:edit']"
					>修改</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-plus"
						@click="handleAdd(scope.row)"
						v-hasPermi="['wms:factory:add']"
					>新增</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-delete"
						@click="handleDelete(scope.row)"
						v-hasPermi="['wms:factory:remove']"
					>删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 添加或修改工厂模型对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="父组织名" prop="parentId" style="width:430px">
					<treeselect v-model="form.parentId" :options="factoryOptions" :normalizer="normalizer" placeholder="请选择父组织id" />
				</el-form-item>
				<el-form-item label="组织名称" prop="factoryName" style="width:430px">
					<el-input v-model="form.factoryName" placeholder="请输入组织名称" />
				</el-form-item>
				<el-form-item label="组织编码" prop="factoryCode" style="width:430px">
        			<el-input v-model="form.factoryCode" placeholder="请输入组织编码" />
   				</el-form-item>
				<el-form-item label="负责人" prop="leader" style="width:430px">
					<el-input v-model="form.leader" placeholder="请输入负责人" />
				</el-form-item>
				<el-form-item label="联系电话" prop="phone" style="width:430px">
					<el-input v-model="form.phone" placeholder="请输入联系电话" />
				</el-form-item>
				<el-form-item label="邮箱" prop="email" style="width:430px">
					<el-input v-model="form.email" placeholder="请输入邮箱" />
				</el-form-item>
				<el-form-item label="是否启用" prop="status" >
          			<el-select v-model="form.status" placeholder="请选择是否启用"  @change="$forceUpdate()" clearable style="width:350px">
            			<el-option v-for="item in options" :key="item.status" :label="item.label" :value="item.status"></el-option>
          			</el-select>
        		</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listFactory, getFactory, delFactory, addFactory, updateFactory } from "@/api/wms/factory";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
	name: "Factory",
	components: {
		Treeselect
	},
	data() {
		return {
			// 遮罩层
			loading: true,
			// 显示搜索条件
			showSearch: true,
			// 工厂模型表格数据
			factoryList: [],
			// 工厂模型树选项
			factoryOptions: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 是否展开，默认全部展开
			isExpandAll: true,
			// 重新渲染表格状态
			refreshTable: true,
			// 查询参数
			queryParams: {
				parentId: null,
				ancestors: null,
				factoryName: null,
				orderNum: null,
				leader: null,
				phone: null,
				email: null,
				status: null,
			},
			 options: [{
                status: "1",
                label: '是'
              }, {
                status: "0",
                label: '否'
              }],
			// 表单参数
			form: {},
			// 表单校验
						// 表单校验
      			rules: {
      			  factoryCode: [
            	  { required: true, message: "组织编码不能为空", trigger: "blur" }
            	],
            	factoryName: [
            		{ required: true, message: "组织名称不能为空", trigger: "blur" }
            	],
      			  status: [
              	{ required: true, message: "组织状态不能为空", trigger: "blur" }
              ]
      			}
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 查询工厂模型列表 */
		getList() {
			this.loading = true;
			listFactory(this.queryParams).then(response => {
				this.factoryList = this.handleTree(response.data, "factoryId", "parentId");
				this.loading = false;
			});
		},
		/** 转换工厂模型数据结构 */
		normalizer(node) {
			if (node.children && !node.children.length) {
				delete node.children;
			}
			return {
				id: node.factoryId,
				label: node.factoryName,
				children: node.children
			};
		},
	/** 查询工厂模型下拉树结构 */
		getTreeselect() {
			listFactory().then(response => {
				this.factoryOptions = [];
				const data = { factoryId: 0, factoryName: '顶级节点', children: [] };
				data.children = this.handleTree(response.data, "factoryId", "parentId");
				this.factoryOptions.push(data);
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				factoryId: null,
				parentId: null,
				ancestors: null,
				factoryName: null,
				orderNum: null,
				leader: null,
				phone: null,
				email: null,
				status: null,
				delFlag: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		/** 新增按钮操作 */
		handleAdd(row) {
			this.reset();
			this.getTreeselect();
			if (row != null && row.factoryId) {
				this.form.parentId = row.factoryId;
			} else {
				this.form.parentId = 0;
			}
			this.open = true;
			this.title = "添加工厂模型";
		},
		/** 展开/折叠操作 */
		toggleExpandAll() {
			this.refreshTable = false;
			this.isExpandAll = !this.isExpandAll;
			this.$nextTick(() => {
				this.refreshTable = true;
			});
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			this.getTreeselect();
			if (row != null) {
				this.form.parentId = row.factoryId;
			}
			getFactory(row.factoryId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改工厂模型";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.factoryId != null) {
						updateFactory(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.open = false;
							this.getList();
						});
					} else {
						addFactory(this.form).then(response => {
							this.$modal.msgSuccess("新增成功");
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			this.$modal.confirm('是否确认删除工厂模型编号为"' + row.factoryId + '"的数据项？').then(function() {
				return delFactory(row.factoryId);
			}).then(() => {
				this.getList();
				this.$modal.msgSuccess("删除成功");
			}).catch(() => {});
		}
	}
};
</script>
