<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="factoryCode">
				<el-input
					v-model="queryParams.factoryCode"
					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 label="订单数量" prop="quantity">
				<el-input
					v-model="queryParams.quantity"
					placeholder="请输入订单数量"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="已拆分数量" prop="quantitySplit">
				<el-input
					v-model="queryParams.quantitySplit"
					placeholder="请输入已拆分数量"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="单位" prop="unit">
				<el-input
					v-model="queryParams.unit"
					placeholder="请输入单位"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="任务清单" prop="workerOrder">
				<el-input
					v-model="queryParams.workerOrder"
					placeholder="请输入任务清单"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="计划生产日期" prop="planProDate">
				<el-date-picker clearable
					v-model="queryParams.planProDate"
					type="date"
					value-format="yyyy-MM-dd"
					placeholder="选择计划生产日期">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="计划完成日期" prop="planComplete">
				<el-date-picker clearable
					v-model="queryParams.planComplete"
					type="date"
					value-format="yyyy-MM-dd"
					placeholder="选择计划完成日期">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="" prop="atrr1">
				<el-input
					v-model="queryParams.atrr1"
					placeholder="请输入"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="" prop="atrr2">
				<el-input
					v-model="queryParams.atrr2"
					placeholder="请输入"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="" prop="atrr3">
				<el-input
					v-model="queryParams.atrr3"
					placeholder="请输入"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="0未拆分" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择0未拆分" clearable>
					<el-option
						v-for="dict in dict.type.order_status"
						:key="dict.value"
						:label="dict.label"
						:value="dict.value"
					/>
				</el-select>
			</el-form-item>
			<el-form-item label="上级工单" prop="parentOrder">
				<el-input
					v-model="queryParams.parentOrder"
					placeholder="请输入上级工单"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="产品类型" prop="prodType">
				<el-select v-model="queryParams.prodType" placeholder="请选择产品类型" clearable>
					<el-option
						v-for="dict in dict.type.product_type"
						:key="dict.value"
						:label="dict.label"
						:value="dict.value"
					/>
				</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="['plan:whiteOrder: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="whiteOrderList"
			row-key="orderCode"
			:default-expand-all="isExpandAll"
			:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
		>
			<el-table-column label="计划工厂编码" prop="factoryCode" />
			<el-table-column label="订单类型" align="center" prop="orderType" />
			<el-table-column label="订单号" align="center" prop="orderCode" />
			<el-table-column label="物料号" align="center" prop="prodCode" />
			<el-table-column label="物料名称" align="center" prop="prodDesc" />
			<el-table-column label="订单数量" align="center" prop="quantity" />
			<el-table-column label="已拆分数量" align="center" prop="quantitySplit" />
			<el-table-column label="单位" align="center" prop="unit" />
			<el-table-column label="任务清单" align="center" prop="workerOrder" />
			<el-table-column label="计划生产日期" align="center" prop="planProDate" width="180">
				<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="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.planComplete, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="" align="center" prop="atrr1" />
			<el-table-column label="" align="center" prop="atrr2" />
			<el-table-column label="" align="center" prop="atrr3" />
			<el-table-column label="0未拆分" align="center" prop="status">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.order_status" :value="scope.row.status"/>
				</template>
			</el-table-column>
			<el-table-column label="上级工单" align="center" prop="parentOrder" />
			<el-table-column label="产品类型" align="center" prop="prodType">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.product_type" :value="scope.row.prodType"/>
				</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="['plan:whiteOrder:edit']"
					>修改</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-plus"
						@click="handleAdd(scope.row)"
						v-hasPermi="['plan:whiteOrder:add']"
					>新增</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-delete"
						@click="handleDelete(scope.row)"
						v-hasPermi="['plan:whiteOrder: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="factoryCode">
					<el-input v-model="form.factoryCode" placeholder="请输入计划工厂编码" />
				</el-form-item>
				<el-form-item label="订单号" prop="orderCode">
					<el-input v-model="form.orderCode" placeholder="请输入订单号" />
				</el-form-item>
				<el-form-item label="物料号" prop="prodCode">
					<el-input v-model="form.prodCode" placeholder="请输入物料号" />
				</el-form-item>
				<el-form-item label="物料名称" prop="prodDesc">
					<el-input v-model="form.prodDesc" placeholder="请输入物料名称" />
				</el-form-item>
				<el-form-item label="订单数量" prop="quantity">
					<el-input v-model="form.quantity" placeholder="请输入订单数量" />
				</el-form-item>
				<el-form-item label="已拆分数量" prop="quantitySplit">
					<el-input v-model="form.quantitySplit" placeholder="请输入已拆分数量" />
				</el-form-item>
				<el-form-item label="单位" prop="unit">
					<el-input v-model="form.unit" placeholder="请输入单位" />
				</el-form-item>
				<el-form-item label="任务清单" prop="workerOrder">
					<el-input v-model="form.workerOrder" placeholder="请输入任务清单" />
				</el-form-item>
				<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-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-form-item label="" prop="atrr1">
					<el-input v-model="form.atrr1" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="" prop="atrr2">
					<el-input v-model="form.atrr2" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="" prop="atrr3">
					<el-input v-model="form.atrr3" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="0未拆分" prop="status">
					<el-radio-group v-model="form.status">
						<el-radio
							v-for="dict in dict.type.order_status"
							:key="dict.value"
							:label="dict.value"
						>{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="上级工单" prop="parentOrder">
					<treeselect v-model="form.parentOrder" :options="whiteOrderOptions" :normalizer="normalizer" placeholder="请选择上级工单" />
				</el-form-item>
				<el-form-item label="产品类型" prop="prodType">
					<el-select v-model="form.prodType" placeholder="请选择产品类型">
						<el-option
							v-for="dict in dict.type.product_type"
							:key="dict.value"
							:label="dict.label"
							:value="dict.value"
						></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 { listWhiteOrder, getWhiteOrder, delWhiteOrder, addWhiteOrder, updateWhiteOrder } from "@/api/plan/whiteOrder";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
	name: "WhiteOrder",
	dicts: ['product_type', 'order_status'],
	components: {
		Treeselect
	},
	data() {
		return {
			// 遮罩层
			loading: true,
			// 显示搜索条件
			showSearch: true,
			// 白坯订单管理表格数据
			whiteOrderList: [],
			// 白坯订单管理树选项
			whiteOrderOptions: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 是否展开，默认全部展开
			isExpandAll: true,
			// 重新渲染表格状态
			refreshTable: true,
			// 查询参数
			queryParams: {
				factoryCode: null,
				orderType: null,
				orderCode: null,
				prodCode: null,
				prodDesc: null,
				quantity: null,
				quantitySplit: 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;
			listWhiteOrder(this.queryParams).then(response => {
				this.whiteOrderList = 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.prodDesc,
				children: node.children
			};
		},
	/** 查询白坯订单管理下拉树结构 */
		getTreeselect() {
			listWhiteOrder().then(response => {
				this.whiteOrderOptions = [];
				const data = { orderCode: 0, prodDesc: '顶级节点', children: [] };
				data.children = this.handleTree(response.data, "orderCode", "parentOrder");
				this.whiteOrderOptions.push(data);
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				factoryCode: null,
				orderType: null,
				orderCode: null,
				prodCode: null,
				prodDesc: null,
				quantity: null,
				quantitySplit: 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;
			}
			getWhiteOrder(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) {
						updateWhiteOrder(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.open = false;
							this.getList();
						});
					} else {
						addWhiteOrder(this.form).then(response => {
							this.$modal.msgSuccess("新增成功");
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			this.$modal.confirm('是否确认删除白坯订单管理编号为"' + row.id + '"的数据项？').then(function() {
				return delWhiteOrder(row.id);
			}).then(() => {
				this.getList();
				this.$modal.msgSuccess("删除成功");
			}).catch(() => {});
		}
	}
};
</script>
