<template>
	<div class="app-container">
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button
					type="primary"
					plain
					icon="el-icon-plus"
					size="mini"
					@click="handleSelectSparePartsLedger"
					v-hasPermi="['device:sparePartsApplicationRecord:add']"
				>新增</el-button>
				<ItemSelectSparePartsLedger ref="itemSelectSparePartsLedger" @onSelected="selectSparePartsLedger"></ItemSelectSparePartsLedger>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="danger"
					plain
					icon="el-icon-delete"
					size="mini"
					:disabled="multiple"
					@click="handleDelete"
					v-hasPermi="['device:sparePartsApplicationRecord:remove']"
				>删除</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="sparePartsApplicationRecordList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<!-- 序号 -->
      <el-table-column type="index" width="90" align="center" :index="indexMethod" label="序号" fixed/>
      <el-table-column label="维修单号" align="center" prop="workCode" width="200" fixed/>
      <el-table-column label="使用设备" align="center" prop="spareUseEquipment" />
			<el-table-column label="备品备件编码" align="center" prop="spareCode" width="120"/>
			<el-table-column label="备品备件名称" align="center" prop="spareName" width="120"/>
			<el-table-column label="规格型号" align="center" prop="spareModel" width="150"/>
			<el-table-column label="数量" align="center" prop="spareQuantity" />
			<el-table-column label="领用时间" align="center" prop="applyTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.applyTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="申领人" align="center" prop="applyPeople" width="80"/>
			<el-table-column label="批准人" align="center" prop="applyApprovePeople" width="80"/>
		</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="1000px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
			</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 { listSparePartsApplicationRecord, getSparePartsApplicationRecord, delSparePartsApplicationRecord, addSparePartsApplicationRecord, updateSparePartsApplicationRecord , listRepairSparePartsRecord } from "@/api/device/sparePartsApplicationRecord";
import ItemSelectSparePartsLedger from "./selectSparePartsLedger.vue";

export default {
	name: "SelectSpareParts",
	components: { ItemSelectSparePartsLedger },
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 申领记录表格数据
			sparePartsApplicationRecordList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				workCode: this.workCode,
			  equipmentCode: this.equipmentCode,
				applyType: null,
				applyCode: null,
				spareCode: null,
				spareName: null,
				spareModel: null,
				spareQuantity: null,
				spareGroupLine: null,
				spareUseEquipment: null,
				applyTime: null,
				applyPeople: null,
				applyApprovePeople: null,
				attr1: null,
				attr2: null,
				attr3: null,
				factoryCode: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
			},
			componentKey:0
		};
	},
  props: {
    equipmentCode: undefined,
    workCode: undefined,
    optType: undefined,
  },
	created() {
		this.getList();
	},
	methods: {
	    //设备选择弹出框
      handleSelectSparePartsLedger() {
        this.$forceUpdate();
        this.$refs.itemSelectSparePartsLedger.showFlag = true;
      },
		// 生成表头序号
    indexMethod(index) {
      return index + 1;
    },
		/** 查询申领记录列表 */
		getList() {
			this.loading = true;
			listRepairSparePartsRecord(this.queryParams).then(response => {
				this.sparePartsApplicationRecordList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				workCode: this.workCode,
				equipmentCode: this.equipmentCode,
				applyType: null,
				applyId: null,
				applyCode: null,
				spareCode: null,
				spareName: null,
				spareModel: null,
				spareQuantity: null,
				spareGroupLine: null,
				spareUseEquipment: null,
				applyTime: null,
				applyPeople: null,
				applyApprovePeople: null,
				attr1: null,
				attr2: null,
				attr3: null,
				delFlag: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null,
				factoryCode: null,
				spareApplyLists : []
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.applyId)
			this.single = selection.length!==1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加申领记录";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const applyId = row.applyId || this.ids
			getSparePartsApplicationRecord(applyId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改申领记录";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.applyId != null) {
						updateSparePartsApplicationRecord(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.open = false;
							this.getList();
						});
					} else {
						addSparePartsApplicationRecord(this.form).then(response => {
							this.$modal.msgSuccess("新增成功");
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
    //接收参数
    selectSparePartsLedger(obj){
      console.log("接收参数",obj);
      //备件编号、备件名称、备件规格型号、备件使用设备、备件申请数量、备件关联工单号
      var spareApplyLists = [];
      obj.forEach(item => {
        var spareApplyList = {};
          spareApplyList.spareCode = item.materialCode;
          spareApplyList.spareName = item.materialDesc;
          spareApplyList.spareModel = item.spareMode;
          spareApplyList.spareUseEquipment = this.queryParams.equipmentCode;
          spareApplyList.spareQuantity = item.spareQuantity;
          spareApplyList.workCode = this.queryParams.workCode;
          spareApplyList.storageId = item.storageId;
          spareApplyList.amount = item.amount;
          spareApplyLists.push(spareApplyList);
      });
      this.form.spareApplyLists = spareApplyLists;
      addSparePartsApplicationRecord(this.form).then(response => {
      	this.open = false;
      	this.getList();
      });
    },
		/** 删除按钮操作 */
		handleDelete(row) {
			const applyIds = row.applyId || this.ids;
			this.$modal.confirm('是否确认删除申领记录编号为"' + applyIds + '"的数据项？').then(function() {
				return delSparePartsApplicationRecord(applyIds);
			}).then(() => {
				this.getList();
				this.$modal.msgSuccess("删除成功");
			}).catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download('device/sparePartsApplicationRecord/export', {
				...this.queryParams
			}, `sparePartsApplicationRecord_${new Date().getTime()}.xlsx`)
		}
	}
};
</script>
