<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
			<el-form-item label="报工单编号" prop="reportCode">
				<el-input
					v-model="queryParams.reportCode"
					placeholder="请输入报工单编号"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="报工类型" prop="reportType">
				<el-input
					v-model="queryParams.reportType"
					placeholder="请输入报工类型"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="生产工单编码" prop="workorderCode">
				<el-input
					v-model="queryParams.workorderCode"
					placeholder="请输入生产工单编码"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
						<el-form-item label="产品编码" prop="productCode">
      				<el-input
      					v-model="queryParams.productCode"
      					placeholder="请输入产品编码"
      					clearable
      					@keyup.enter.native="handleQuery"
      				/>
      			</el-form-item>
			</el-form-item>
						<el-form-item label="产品名称" prop="productName">
      				<el-input
      					v-model="queryParams.productName"
      					placeholder="请输入产品名称"
      					clearable
      					@keyup.enter.native="handleQuery"
      				/>
      			</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="['mes:reportWork:add']"
				>新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="success"
					plain
					icon="el-icon-edit"
					size="mini"
					:disabled="single"
					@click="handleUpdate"
					v-hasPermi="['mes:reportWork:edit']"
				>修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="danger"
					plain
					icon="el-icon-delete"
					size="mini"
					:disabled="multiple"
					@click="handleDelete"
					v-hasPermi="['mes:reportWork:remove']"
				>删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="warning"
					plain
					icon="el-icon-download"
					size="mini"
					@click="handleExport"
					v-hasPermi="['mes:reportWork:export']"
				>导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="reportWorkList" @selection-change="handleSelectionChange">
			<el-table-column type="selection"  align="center" />
			<el-table-column label="报工单编号" align="center" prop="reportCode" width="100">
			  <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
          >{{scope.row.reportCode}}</el-button>
        </template>
      </el-table-column>
			<el-table-column label="报工类型" align="center" prop="reportType" width="150"/>
			<el-table-column label="生产工单编码" align="center" prop="workorderCode" width="200"/>
			<el-table-column label="产品编码" align="center" prop="productCode" width="200"/>
			<el-table-column label="产品名称" align="center" prop="productName" width="200"/>
			<el-table-column label="规格型号" align="center" prop="spec" />
			<el-table-column label="单位" align="center" prop="unit" />
			<el-table-column label="排产数量" align="center" prop="quantity" />
			<el-table-column label="本次报工数量" align="center" prop="quantityFeedback" width="100"/>
			<el-table-column label="合格数量" align="center" prop="quantityQualified" :show-overflow-tooltip="true">
			  <template slot-scope="scope">
          <div :style="{ color: scope.row.quantityQualified ? 'green' : 'green' }">
            {{ scope.row.quantityQualified }}
          </div>
        </template>
      </el-table-column>
			<el-table-column label="不合格数量" align="center" prop="quantityUnqualified" width="100">
				<template slot-scope="scope">
          <div :style="{ color: scope.row.quantityUnqualified ? 'red' : 'red' }">
            {{ scope.row.quantityUnqualified }}
          </div>
        </template>
      </el-table-column>
			<el-table-column label="报工人员" align="center" prop="userName" />
			<el-table-column label="人员名称" align="center" prop="nickName" />
			<el-table-column label="报工途径" align="center" prop="feedbackChannel" />
			<el-table-column label="报工时间" align="center" prop="feedbackTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.feedbackTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="录入人员" align="center" prop="recordUser" />
			<el-table-column label="备注" align="center" prop="remark" />
			<el-table-column label="工时" align="center" prop="workTime" />
			<el-table-column label="线体编码" align="center" prop="machineCode" />
			<el-table-column label="线体名称" align="center" prop="machineName" />
			<el-table-column label="班组编码" align="center" prop="teamCode" />
			<el-table-column label="班次编码" align="center" prop="shiftCode" />
			<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="['mes:reportWork:edit']"
					>修改</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-delete"
						@click="handleDelete(scope.row)"
						v-hasPermi="['mes:reportWork:remove']"
					>删除</el-button>
				</template>
			</el-table-column>
		</el-table>

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

		<!-- 添加或修改报工报表对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-row>
          			<el-col :span="6">
						<el-form-item label="报工单编号" prop="reportCode">
							<el-input v-model="form.reportCode" placeholder="请输入报工单编号" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="报工类型" prop="reportType">
        					<el-select v-model="form.reportType" placeholder="请选择报工类型"  @change="$forceUpdate()" clearable>
            					<el-option v-for="item in reportTypeOptions" :key="item.reportType" :label="item.label" :value="item.reportType"></el-option>
          					</el-select>
        				</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="生产工单编码" prop="workorderCode">
							<el-input v-model="form.workorderCode" placeholder="请输入生产工单编码" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="产品编码" prop="productCode">
							<el-input v-model="form.productCode" placeholder="请输入产品编码" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="6">
						<el-form-item label="产品名称" prop="productName">
							<el-input v-model="form.productName" placeholder="请输入产品名称" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="规格型号" prop="spec">
							<el-input v-model="form.spec" type="textarea" placeholder="请输入内容" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="单位" prop="unit">
							<el-input v-model="form.unit" placeholder="请输入单位" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="排产数量" prop="quantity">
							<el-input v-model="form.quantity" placeholder="请输入排产数量" />
						</el-form-item>
					</el-col>

				</el-row>
				<el-row>
					<el-col :span="6">
						<el-form-item label="本次报工数量" prop="quantityFeedback">
							<el-input v-model="form.quantityFeedback" placeholder="请输入本次报工数量" />
						</el-form-item>
					</el-col>
          			<el-col :span="6">
						<el-form-item label="合格数量" prop="quantityQualified">
							<el-input v-model="form.quantityQualified" placeholder="请输入合格数量" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="不合格数量" prop="quantityUnqualified">
							<el-input v-model="form.quantityUnqualified" placeholder="请输入不合格数量" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="报工人员" prop="userName">
							<el-input v-model="form.userName" placeholder="请输入报工人员" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="6">
						<el-form-item label="人员名称" prop="nickName">
							<el-input v-model="form.nickName" placeholder="请输入人员名称" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="报工途径" prop="feedbackChannel">
        					<el-select v-model="form.feedbackChannel" placeholder="请选择报工类型"  @change="$forceUpdate()" clearable>
            					<el-option v-for="item in feedbackChannelOptions" :key="item.feedbackChannel" :label="item.label" :value="item.feedbackChannel"></el-option>
          					</el-select>
        				</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="报工时间" prop="feedbackTime">
						<el-date-picker clearable
							v-model="form.feedbackTime"
							type="date"
							value-format="yyyy-MM-dd"
							placeholder="请选择报工时间">
						</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="录入人员" prop="recordUser">
							<el-input v-model="form.recordUser" placeholder="请输入录入人员" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="6">
						<el-form-item label="线体编码" prop="machineCode">
							<el-input v-model="form.machineCode" placeholder="请输入线体编码" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="线体名称" prop="machineName">
							<el-input v-model="form.machineName" placeholder="请输入线体名称" />
						</el-form-item>
					</el-col>
          			<el-col :span="6">
						<el-form-item label="班组编码" prop="teamCode">
							<el-input v-model="form.teamCode" placeholder="请输入班组编码" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="班次编码" prop="shiftCode">
							<el-input v-model="form.shiftCode" placeholder="请输入班次编码" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="24">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="form.remark"placeholder="请输入内容" />
						</el-form-item>
					</el-col>
					</el-row>
			</el-form>
			<el-divider content-position="center" v-if="form.reportCode !=null">物料消耗明细</el-divider>
      <ReportWorkConsume v-if="form.reportCode !=null" :optType="optType" :reportCode="form.reportCode"></ReportWorkConsume>
			<div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType =='view'">返回</el-button>
        <el-button type="primary" @click="submitForm" v-else>确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

<!-- 添加或修改生产报工物料消耗对话框 -->
		<el-dialog :title="title" :visible.sync="reportWorkConsunmOpen" width="1200px" append-to-body>
					<el-table v-loading="loading" :data="reportWorkConsumeList">
          			<el-table-column type="selection" width="55" align="center" />
          			<el-table-column label="报工编码" align="center" prop="reportCode" />
          			<el-table-column label="工单编码" align="center" prop="workorderCode" />
          			<el-table-column label="物料编号" align="center" prop="materialCode" />
          			<el-table-column label="物料名称" align="center" prop="materialName" />
          			<el-table-column label="物料规格型号" align="center" prop="materialSpc" />
          			<el-table-column label="数量" align="center" prop="quantity" />
          			<el-table-column label="单位" align="center" prop="unit" />
          			<el-table-column label="备注" align="center" prop="remark" />
          			<el-table-column label="预留字段1" align="center" prop="attr1" />
          			<el-table-column label="预留字段2" align="center" prop="attr2" />
          			<el-table-column label="预留字段3" align="center" prop="attr3" />
          			<el-table-column label="预留字段4" align="center" prop="attr4" />
          			<el-table-column label="工厂编码" align="center" prop="factoryCode" />
          		</el-table>
      		<pagination
      			v-show="consumeTotal>0"
      			:total="consumeTotal"
      			:page.sync="consumeQueryParams.consumePageNum"
      			:limit.sync="consumeQueryParams.consumePageSize"
      			@pagination="getReportWorkConsumeList"
      		/>
		</el-dialog>

	</div>
</template>

<script>
import { listReportWork, getReportWork, delReportWork, addReportWork, updateReportWork } from "@/api/mes/reportWork";
import { listReportWorkConsume, getReportWorkConsume, delReportWorkConsume, addReportWorkConsume, updateReportWorkConsume} from "@/api/mes/reportWorkConsume";
import ReportWorkConsume from "./reportWorkConsume.vue";

export default {
	name: "ReportWork",
	components: {ReportWorkConsume},
	data() {
		return {
		  reportTypeOptions: [{
              reportType: 'SELF自行报工',
              label: 'SELF自行报工'
            }, {
              reportType: 'UNI统一报工',
              label: 'UNI统一报工'
            }],
      feedbackChannelOptions: [{
              feedbackChannel: 'PAD',
              label: 'PAD'
            }, {
              feedbackChannel: 'MOBILE',
              label: 'MOBILE'
            }, {
              feedbackChannel: 'PC',
              label: 'PC'
            }],
      optType: undefined,
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			consumeTotal:0,
			// 报工报表表格数据
			reportWorkList: [],
			reportWorkConsumeList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			reportWorkConsunmOpen:false,
      consumeQueryParams:{
      	consumePageNum:1,
        consumePageSize:10,
        reportCode:null,
      },
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				reportType: null,
				reportCode: null,
				workorderCode: null,
				productCode: null,
				productName: null,
				spec: null,
				unit: null,
				quantity: null,
				quantityFeedback: null,
				quantityQualified: null,
				quantityUnqualified: null,
				userName: null,
				nickName: null,
				feedbackChannel: null,
				feedbackTime: null,
				recordUser: null,
				status: null,
				workTime: null,
				machineCode: null,
				machineName: null,
				teamCode: null,
				shiftCode: null,
				attr1: null,
				attr2: null,
				attr3: null,
				attr4: null,
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				reportType: [
					{ required: true, message: "报工类型，报工类型：SELF自行报工、UNI统一报工不能为空", trigger: "change" }
				],
				quantityFeedback: [
					{ required: true, message: "本次报工数量不能为空", trigger: "blur" }
				],
				quantityQualified: [
					{ required: true, message: "合格数量不能为空", trigger: "blur" }
				],
				quantityUnqualified: [
					{ required: true, message: "不合格数量不能为空", trigger: "blur" }
				],
			}
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 查询报工报表列表 */
		getList() {
			this.loading = true;
			listReportWork(this.queryParams).then(response => {
				this.reportWorkList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 查询明细按钮操作
      handleView(row){
          this.reset();
          const id = row.id || this.ids;
          getReportWork(id).then(response => {
            this.form = response.data;
            this.open = true;
            this.title = "查看报工信息";
            this.optType = "view";
          });
        },
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				reportType: null,
				reportCode: null,
				workorderCode: null,
				productCode: null,
				productName: null,
				spec: null,
				unit: null,
				quantity: null,
				quantityFeedback: null,
				quantityQualified: null,
				quantityUnqualified: null,
				userName: null,
				nickName: null,
				feedbackChannel: null,
				feedbackTime: null,
				recordUser: null,
				status: null,
				remark: null,
				workTime: null,
				machineCode: null,
				machineName: null,
				teamCode: null,
				shiftCode: null,
				attr1: null,
				attr2: null,
				attr3: null,
				attr4: null,
				createBy: null,
				createTime: null,
				updateTime: null,
				updateBy: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length!==1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加报工报表";
			this.optType = "add";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const id = row.id || this.ids
			getReportWork(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) {
						updateReportWork(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.open = false;
							this.getList();
						});
					} else {
						addReportWork(this.form).then(response => {
							this.$modal.msgSuccess("新增成功");
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids;
			this.$modal.confirm('是否确认删除报工报表编号为"' + ids + '"的数据项？').then(function() {
				return delReportWork(ids);
			}).then(() => {
				this.getList();
				this.$modal.msgSuccess("删除成功");
			}).catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download('mes/reportWork/export', {
				...this.queryParams
			}, `reportWork_${new Date().getTime()}.xlsx`)
		}
	}
};
</script>
