<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px" class="edit-el-form">
			<el-form-item label="订单编码" prop="orderNo">
				<el-input
					v-model="queryParams.orderNo"
					placeholder="请输入订单编码"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="产品编号" prop="materialCode">
				<el-input
					v-model="queryParams.materialCode"
					placeholder="请输入产品编号"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<!--
			<el-form-item label="工单生产日期" prop="incomeTime">
				<el-date-picker clearable
					v-model="queryParams.incomeTime"
					type="date"
					value-format="yyyy-MM-dd"
					placeholder="请选择工单生产日期"
					style="width:205px"
					>
				</el-date-picker>
			</el-form-item>-->

			<el-form-item label="任务时间" prop="incomeTimeArray">
                <el-date-picker
				v-model="queryParams.incomeTimeArray"
				format="yyyy-MM-dd HH:mm:ss"
				type="datetimerange"
				range-separator="至"
				start-placeholder="开始日期"
				end-placeholder="结束日期">
				</el-date-picker>
			</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:prepare:export']"
				>导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="prepareList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="订单编码" align="center" prop="orderNo" width="180">
				 <template slot-scope="scope">
					<el-button
						type="text"
						@click="handleDetail(scope.row)"
					>{{scope.row.orderNo}}</el-button>
				</template>
			</el-table-column>
			<el-table-column prop="incomeBatchNo" label="批次" width="300" align="center"/>
			<el-table-column prop="incomeTimeStr" label="生产日期" width="100" align="center"/>
			<el-table-column prop="materialName" label="产品名称"align="center"/>
			<el-table-column prop="materialCode" label="产品编码" width="150" align="center"
			:formatter="productCodeFormate"/>
			<el-table-column prop="quality" label="数量" width="80" align="center"/>
			<el-table-column prop="unit" label="单位" width="80" align="center"/>
		</el-table>

		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>

		<!-- 报工详情 -->
		<el-dialog :title="titleReport" :visible.sync="openReport" width="1400px">
			<el-table v-loading="loading" :data="pOrderTableList" ref="myPOrderTable"
			>
				<el-table-column label="id" align="center" prop="recordId" v-if="false"/>
				<el-table-column label="序号" type="index" align="center" :index="indexMethod"/>
				<el-table-column label="项目名称" width="160" align="left" prop="ruleName" :show-overflow-tooltip="true" />
				<el-table-column label="上午" align="center" >
					<el-table-column prop="column080090" label="8:00-9:00" width="80" align="center"/>
					<el-table-column prop="column090100" label="9:00-10:00" width="90" align="center"/>
					<el-table-column prop="column100110" label="10:00-11:00" width="100" align="center"/>
					<el-table-column prop="column110120" label="11:00-12:00" width="100" align="center"/>
				</el-table-column>
				<el-table-column label="下午" align="center" >
					<el-table-column prop="column133143" label="13:30-14:30" width="100" align="center"/>
					<el-table-column prop="column13column1431533143" label="14:30-15:30" width="100" align="center"/>
					<el-table-column prop="column143153" label="15:30-16:30" width="100" align="center"/>
					<el-table-column prop="column163173" label="16:30-17:30" width="100" align="center"/>
				</el-table-column>
				<el-table-column label="加班" align="center" >
					<el-table-column prop="column123133" label="12:30-13:30" width="100" align="center"/>
					<el-table-column prop="column180190" label="18:00-19:00" width="100" align="center"/>
					<el-table-column prop="column190200" label="19:00-20:00" width="100" align="center"/>
					<el-table-column prop="column200210" label="20:00-21:00" width="100" align="center"/>
				</el-table-column>
			</el-table>
			<div slot="footer" class="dialog-footer">
        		<el-button @click="cancel">关闭</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { getXJCheckTableList,getXJCheckTableDetail } from "@/api/quality/qcTable";
import moment from "moment/moment";
export default {
	name: "Prepare",
	// components: {Detail},
	data() {
		return {
			newWorkerLoading: false,
			refreshNewWorkerTable:true,
			// 选择领料单
			selectPrepare:[],
			// 打印
			formRef: 'form',
			validateRules: [],
			printData: {
				printable: 'printFrom',
				ignore: ['no-print'],
				workCenter: "暂无数据",
				SAPNo: null,
				manufacture: null,
				auditor: null,
				printDate: null,
				factory: null,
				incomeTime: null,
				workTable: [],
			},
			printDialogVisible: false,
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 备料单表格数据
				prepareList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					incomeTimeArray: [],
					pageNum: 1,
					pageSize: 10,
					checkNo: null,
					workorderName: null,
					parentOrder: null,
					orderId: null,
					orderCode: null,
					productId: null,
					productCode: null,
					prodType: null,
					productName: null,
					productSpc: null,
					wetDetailPlanId: null,
					incomeTime: null,
					incomeBatchNo:null,
					status: null,
					factoryCode: null
				},
				options: [{
				status: "L0",
				label: '待确认'
				}, {
				status: "L1",
				label: '已确认'
				}],
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					checkNo: [
						{ required: true, message: "工单编码不能为空", trigger: "blur" }
					],
					parentOrder: [
						{ required: true, message: "父工单不能为空", trigger: "blur" }
					],
					orderId: [
						{ required: true, message: "订单id不能为空", trigger: "blur" }
					],
					productCode: [
						{ required: true, message: "产品编号不能为空", trigger: "blur" }
					],
					productName: [
						{ required: true, message: "产品名称不能为空", trigger: "blur" }
					],
				},
                openReport : false,
				titleReport : "工单报工详情"
		};
	},
	created() {
		this.getDate();
		this.getList();
	},
	methods: {
       	/**获取默认查询时间段**/
		getDate() {
			let start = this.Fungetdate (0)
			let end = this.Fungetdate (1)
			this.queryParams.incomeTimeArray.push(start,end)
			//this.queryParams.checkTimeArray.push(start,end)
		},
		Fungetdate (num) {
			var dd = new Date();
			dd.setDate(dd.getDate() + num);
			var y = dd.getFullYear();
			var m = dd.getMonth() + 1;//获取当前月份的日期
			var d = dd.getDate();
			return y + "-" + m +"-"+d;
		},

		indexMethod(index){
			return index+1 ;
		},
		/** 查询备料单列表 */
		getList() {
			this.loading = true;


			if(this.queryParams.incomeTimeArray != null &&this.queryParams.incomeTimeArray.length>0){
				this.queryParams.incomeTimeStart = moment(this.queryParams.incomeTimeArray[0]).format('YYYY-MM-DD HH:mm:ss');
				this.queryParams.incomeTimeEnd = moment(this.queryParams.incomeTimeArray[1]).format('YYYY-MM-DD HH:mm:ss');
			}else{
				this.queryParams.incomeTimeStart = null;
				this.queryParams.incomeTimeEnd = null;
			}

			getXJCheckTableList(this.queryParams).then(response => {
				this.prepareList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.openReport = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				prepareId: null,
				checkNo: null,
				workorderName: null,
				parentOrder: null,
				orderId: null,
				orderCode: null,
				productId: null,
				productCode: null,
				prodType: null,
				productName: null,
				productSpc: null,
				wetDetailPlanId: null,
				incomeTime: null,
				incomeBatchNo:null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null,
				factoryCode: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.getDate();
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.prepareId)
			this.single = selection.length!==1
			this.multiple = !selection.length
      this.selectPrepare = selection
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加备料单";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const prepareId = row.prepareId || this.ids
			getPrepare(prepareId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改备料单";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.prepareId != null) {
						updatePrepare(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.open = false;
							this.getList();
						});
					} else {
						addPrepare(this.form).then(response => {
							this.$modal.msgSuccess("新增成功");
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		//产品编码格式化
		productCodeFormate(row, column, cellValue){
			return cellValue.slice(7,18);  //返回值
		},
		/** 报工详情按钮操作 */
		handleDetail(row) {
			this.reset();
			let sr = row;

			this.queryParams.orderNo = sr.orderNo;
			this.queryParams.shiftId = sr.shiftId;
			getXJCheckTableDetail(this.queryParams).then(response => {
				this.pOrderTableList = response;
				this.openReport = true;
				this.titleReport = "过程巡检详情";
			});

		},
		/** 导出按钮操作 */
		handleExport() {
			this.download('mes/prepare/export', {
				...this.queryParams
			}, `prepare_${new Date().getTime()}.xlsx`)
		}
	}
};
</script>
<style>
.my-print-head {
  margin-top: 20px;
  font-weight: bold;
}
.my-print-nav {
  margin-top: 15px;
  margin-bottom: 15px;
}
.my-print-table {

}
.my-print-foot {
  margin-top: 20px;
}
</style>
