<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="planFactoryCode" label-width="100px">
				<el-input
					v-model="queryParams.planFactoryCode"
					placeholder="请输入计划工厂编码"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="订单号" prop="orderCode">
				<el-input
					v-model="queryParams.orderCode"
					placeholder="请输入订单号"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="物料号" prop="prodCode">
				<el-input
					v-model="queryParams.prodCode"
					placeholder="请输入物料号"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="物料名称" prop="prodDesc">
				<el-input
					v-model="queryParams.prodDesc"
					placeholder="请输入物料名称"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</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="warning"
				plain
				icon="el-icon-download"
				size="mini"
				@click="handleExport"
				v-hasPermi="['mes:pro:order:add']"
				>SAP同步</el-button>
			</el-col>
			<!--
				<el-button
					type="primary"
					plain
					icon="el-icon-plus"
					size="mini"
					@click="handleAdd"
					v-hasPermi="['order:order:add']"
				>新增</el-button>
				-->
			<el-col :span="1.5">
				<el-button
				type="warning"
				plain
				icon="el-icon-download"
				size="mini"
				@click="handleExport"
				v-hasPermi="['mes:pro:order:export']"
				>导出</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="orderList"
			row-key="orderCode"
			:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
		>
			
			<el-table-column label="订单号" align="center" prop="orderCode" width="100"/>
			<el-table-column label="物料号" align="center" prop="prodCode" width="100"/>
			<el-table-column label="物料名称" align="center" prop="prodDesc" width="100"/>
			<el-table-column label="订单数量" align="center" prop="quantity" />
			<el-table-column label="单位" align="center" prop="unit" />
			<el-table-column label="任务清单" align="center" prop="workerOrder" width="100"/>
			<el-table-column label="计划生产日期" align="center" prop="planProDate" width="100">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.planProDate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="计划完成日期" align="center" prop="planComplete" width="100">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.planComplete, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="订单状态" align="center" prop="status" />
			<el-table-column label="计划工厂编码" prop="planFactoryCode" width="100px"/>
			<el-table-column label="订单类型" align="center" prop="orderType" />
			<el-table-column label="产品类型" align="center" prop="prodType" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
				<template slot-scope="scope">
					<el-button
						size="mini"
						type="text"
						icon="el-icon-plus"
						@click="handleAdd(scope.row)"
						v-hasPermi="['mes:pro:order:add']"
						v-if="scope.row.parentOrder == 0"
					>拆分</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 添加或修改订单对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="100px">
			<el-row>
                <el-col :span="12">
					<el-form-item label="计划工厂编码" prop="planFactoryCode">
						<el-input v-model="form.planFactoryCode" placeholder="请输入计划工厂编码" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
				    <el-form-item label="订单号" prop="orderCode">
						<el-input v-model="form.orderCode" placeholder="请输入订单号" />
					</el-form-item>
				</el-col>	
			</el-row>		
			<el-row>
                <el-col :span="12">	
					<el-form-item label="物料号" prop="prodCode">
						<el-input v-model="form.prodCode" placeholder="请输入物料号" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="物料名称" prop="prodDesc">
						<el-input v-model="form.prodDesc" placeholder="请输入物料名称" />
					</el-form-item>
				</el-col>	
			</el-row>
			<el-row>
                <el-col :span="12">				
					<el-form-item label="订单数量" prop="quantity">
						<el-input v-model="form.quantity" placeholder="请输入订单数量" />
					</el-form-item>
				</el-col>
				<el-col :span="12">	
					<el-form-item label="单位" prop="unit">
						<el-input v-model="form.unit" placeholder="请输入单位" />
					</el-form-item>
				</el-col>	
			</el-row>
			<el-row>
                <el-col :span="12">		
					<el-form-item label="任务清单" prop="workerOrder">
						<el-input v-model="form.workerOrder" placeholder="请输入任务清单" />
					</el-form-item>
                </el-col>
				<el-col :span="12">	
					<el-form-item label="上级工单" prop="parentOrder">
						<treeselect v-model="form.parentOrder" :options="orderOptions" :normalizer="normalizer" placeholder="请选择上级工单" />
					</el-form-item>
				</el-col>	
			</el-row>
			<el-row>
                <el-col :span="12">	
					<el-form-item label="计划生产日期" prop="planProDate">
						<el-date-picker clearable
							v-model="form.planProDate"
							type="date"
							value-format="yyyy-MM-dd"
							placeholder="选择计划生产日期">
						</el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="12">		
					<el-form-item label="计划完成日期" prop="planComplete">
						<el-date-picker clearable
							v-model="form.planComplete"
							type="date"
							value-format="yyyy-MM-dd"
							placeholder="选择计划完成日期">
						</el-date-picker>
					</el-form-item>
				</el-col>	
			</el-row>
			</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 { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/mes/pro/order";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
	name: "Order",
	components: {
		Treeselect
	},
	data() {
		return {
			// 遮罩层
			loading: true,
			// 显示搜索条件
			showSearch: true,
			// 订单表格数据
			orderList: [],
			// 订单树选项
			orderOptions: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 是否展开，默认全部展开
			isExpandAll: true,
			// 重新渲染表格状态
			refreshTable: true,
			// 查询参数
			queryParams: {
				planFactoryCode: null,
				orderType: null,
				orderCode: null,
				prodCode: null,
				prodDesc: null,
				quantity: null,
				unit: null,
				workerOrder: null,
				planProDate: null,
				planComplete: null,
				atrr1: null,
				atrr2: null,
				atrr3: null,
				status: null,
				parentOrder: null,
				prodType: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
			}
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 查询订单列表 */
		getList() {
			this.loading = true;
			listOrder(this.queryParams).then(response => {
				this.orderList = this.handleTree(response.data, "orderCode", "parentOrder");
				this.loading = false;
			});
		},
		/** 转换订单数据结构 */
		normalizer(node) {
			if (node.children && !node.children.length) {
				delete node.children;
			}
			return {
				id: node.orderCode,
				label: node.orderCode,
				children: node.children
			};
		},
	/** 查询订单下拉树结构 */
		getTreeselect() {
			listOrder().then(response => {
				this.orderOptions = [];
				const data = { orderCode: 0, orderCode: '顶级节点', children: [] };
				data.children = this.handleTree(response.data, "orderCode", "parentOrder");
				this.orderOptions.push(data);
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				planFactoryCode: null,
				orderType: null,
				orderCode: null,
				prodCode: null,
				prodDesc: null,
				quantity: null,
				unit: null,
				workerOrder: null,
				planProDate: null,
				planComplete: null,
				atrr1: null,
				atrr2: null,
				atrr3: null,
				status: null,
				parentOrder: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null,
				prodType: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		/** 新增按钮操作 */
		handleAdd(row) {
			this.reset();
			this.getTreeselect();
			if (row != null && row.orderCode) {
				this.form.parentOrder = row.orderCode;
			} else {
				this.form.parentOrder = 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.parentOrder = row.orderCode;
			}
			getOrder(row.id).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改订单";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.id != null) {
						updateOrder(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.open = false;
							this.getList();
						});
					} else {
						addOrder(this.form).then(response => {
							this.$modal.msgSuccess("新增成功");
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			this.$modal.confirm('是否确认删除订单编号为"' + row.id + '"的数据项？').then(function() {
				return delOrder(row.id);
			}).then(() => {
				this.getList();
				this.$modal.msgSuccess("删除成功");
			}).catch(() => {});
		}
	}
};
</script>
