var searchbycodecomponent = {
	template: `
		<div>
			
			<div style="display: flex;">
				<el-upload
				 class="upload-demo"
				 action=""
				 :on-change="handleChange"
				 :file-list="fileListUpload"
				 :show-file-list="false"
				  accept=".csv"
				  :auto-upload="false">
				  <el-button id ="bt_upload" size="small" plain type="primary">点击上传（csv文件）</el-button>
				</el-upload>
				 <el-button id ="bt_import" size="small" plain type="primary" @click="importData" style="margin-left: 10px;">确认导入</el-button>
			<el-button id ="bt_download" size="small" plain type="primary" @click="download_template" style="margin-left: 10px;">下载导入模板文件</el-button>
			<el-button id ="bt_export_1" plain type="primary" icon="el-icon-document" @click="export_plan_1()" style="margin-left: 0px;padding-left: 10px;padding-right: 10px;">导出</el-button>
			
			</div>
			<div>
				<el-progress v-if="if_show_process" :percentage="percent_len" :stroke-width="2"></el-progress>
			</div>
			
			<div class="span">
				<el-table
					:data="importTableData"
					style="font-size:14px;margin-top: 10px;"
					:row-class-name="tableRowClassName"
					stripe border>
					  <el-table-column
						prop="code"
						label="六合编号"
						:width="flexColumnWidth('code',importTableData)">
						:show-overflow-tooltip="true">
					  </el-table-column>
					  <el-table-column
						prop="order_date"
						label="日期"
						:width="flexColumnWidth('order_date',importTableData)"
						:show-overflow-tooltip="true">
					  </el-table-column>
					  			
					  <el-table-column
						prop="price"
						label="单价"
						:width="flexColumnWidth('price',importTableData,2)">
					  </el-table-column>
					  <el-table-column
						prop="supplier"
						label="供应商"
						:width="flexColumnWidth('supplier',importTableData,3)"
						:show-overflow-tooltip="true">
					  </el-table-column>
					  <el-table-column
						prop="rdm"
						label="需求任务"
						:width="flexColumnWidth('rdm',importTableData)"
						:show-overflow-tooltip="true">
					  </el-table-column>
					
				  </el-table>
			</div>	
		</div>
		`,
	data() {
		// 获取当前屏幕的分辨率
		let screenW = window.screen.width;
		let screenH = window.innerHeight;
		console.log("screen width:", screenW);
		console.log("screen height:", screenH);
		return {
			tHeight: screenH - 40 - 232,
			tableData: [],
			tableData1: [],
			currentPage: 1,
			pagesize: 50,
			recordTotal: 0,
			applicant_v: '',

			dialogVisible: false,
			multipleSelection: [],
			disabled: true,

			if_show_process: false,
			percent_len: 0,
			interval: '',
			if_show_process1: false,

			update_visible: false,
			form: {
				id: '',
				p_id: '',
				process_name: '',
				process_t: '',
			},

			id: 0,
			state: '',
			fileListUpload: [],
			importTableData: [],
			codes: []
		}
	},

	methods: {
		celldblclick_1(row, column, cell, event) {
			this.$data.tableData1 = JSON.parse(row.data);
			this.$data.id = row.id,
				this.$data.state = row.state,
				this.$data.update_visible = true;
		},
		tableRowClassName({ row, rowIndex }) {
			if (row.cti != undefined && row.cti.length > 0) {
				return 'success-row';
			} else {
				/*var n = rowIndex%2;
				if (n == 1) {
					return 'success-row';
				}*/
			}
			return '';
		},
		download_template() {
			document.getElementById("bt_download").blur();

			var downUrl = '/pp/download_file' + "?id=" + "根据编号物料匹配.csv";
			downloadUrl("根据编号物料匹配.csv", downUrl);
		},
		handleChange(file, fileList) {
			document.getElementById("bt_upload").blur();

			this.fileTemp = file.raw
			console.log("filetype:", this.fileTemp);
			if (this.fileTemp) {
				console.log("begin ...")
				console.log(this.fileTemp.type);
				if ((this.fileTemp.type == 'text/csv') || (this.fileTemp.type == '.csv') || (this.fileTemp.type == 'application/vnd.ms-excel')) {
					let _this = this//如果需要点击事件结束之后对DOM进行操作使用)_this.xx=xx进行操作
					Papa.parse(_this.fileTemp, {
						encoding: 'gb2312',
						complete(results) {
							console.log(results)//这个是csv文件的数据
							let data = []
							let codes = []
							//遍历csv文件中的数据，存放到data中 方法不唯一，可自己更改
							var cnt = 1
							for (let i = 0; i < results.data.length; ++i) {
								if (i == 0) {
									continue;
								}
								let obj = {}

								obj.code = results.data[i][0]

								if (obj.code != '') {
									cnt += 1
									data.push(obj)
									codes.push(obj.code)
								}
							}
							//data.splice(0, 1)//将数组第一位的表格名称去除
							let num = 0
							_this.$data.importTableData = data;
							_this.$data.codes = codes;
							console.log("_this.$data.codes", _this.$data.codes)
						}
					})

				} else {
					this.$data.fileListUpload = [];
					this.$message({
						type: 'warning',
						message: '附件格式错误，请删除后重新上传！'
					})
				}
			} else {
				this.$message({
					type: 'warning',
					message: '请上传附件！'
				})
			}
		},
		//导入CSV
		importData() {
			console.log("this.$data.importTableData", this.$data.importTableData);
			document.getElementById("bt_import").blur();
			if (this.$data.importTableData.length == 0) {
				this.$message({
					message: "请选择导入文件",
					type: "warning",
					duration: 1500
				});
				return;
			}

			this.$data.percent_len = 0;
			this.$data.if_show_process = true;
			this.$data.interval = setInterval(() => {
				if (this.$data.percent_len >= 99) {
					clearInterval(this.$data.interval);
					return;
				}
				this.$data.percent_len += 1
			}, 20)
			var _this = this;

			axios.post('/pp/search_plan_by_code', {
				opuser: localStorage.getItem("online_user"),
				opuser_uuid: localStorage.getItem("uuid"),
				codes: this.$data.codes,
			})
				.then(function (response) {

					var data = response.data.data;
					console.log("r:", data);
					let s = ""
					let errType = ""

					_this.$data.importTableData = data;



					_this.$data.importdialogVisible = false;
					//更新页面
					//	_this.get_ma_plan();

					// 操作日志
					_this.add_operation_log("根据编码匹配物料");

				})
				.catch(function (error) {
					console.log(error);
				});
		},
		export_plan_1() {

			var _this = this;
			var objs;
			axios.post('/pp/export_plan_by_code', {
				opuser: localStorage.getItem("online_user"),
				opuser_uuid: localStorage.getItem("uuid"),
				codes: this.$data.codes,
			})
				.then(function (response) {

					var date = new Date();
					//年 getFullYear()：四位数字返回年份
					var year = date.getFullYear() % 2000; //getFullYear()代替getYear()
					console.log(year.toString());
					//月 getMonth()：0 ~ 11
					var month = date.getMonth() + 1;
					if (month < 10) {
						month = '0' + month
					}
					//日 getDate()：(1 ~ 31)
					var day = date.getDate();
					if (day < 10) {
						day = '0' + day;
					}
					//时 getHours()：(0 ~ 23)
					var hour = date.getHours();
					if (hour < 10) {
						hour = '0' + hour;
					}
					//分 getMinutes()： (0 ~ 59)
					var minute = date.getMinutes();
					if (minute < 10) {
						minute = '0' + minute;
					}
					//秒 getSeconds()：(0 ~ 59)
					var second = date.getSeconds();
					if (second < 10) {
						second = '0' + second;
					}

					var filename = year.toString() + month.toString() + day.toString() + " " + hour.toString() + minute.toString() + second.toString();
					filename = "物料匹配 " + filename + ".csv"
					console.log(response.data);
					var fileid = response.data.fileId;

					// 操作日志
					_this.add_operation_log("根据编码匹配物料导出");

					var downUrl = '/pp/download_file' + "?id=" + fileid;
					downloadUrl(filename, downUrl);
				})
				.catch(function (error) {
					console.log(error);
				});
		},

		post_process() {
			this.$data.update_visible = false;

			var _this = this;

			console.log(_this.$data.form);

			var objs;
			axios.post('/pp/approve_ok', {
				opuser: localStorage.getItem("online_user"),
				opuser_uuid: localStorage.getItem("uuid"),
				id: _this.$data.id,
				data: _this.$data.tableData1,
			})
				.then(function (response) {

					let r = response.data.r;
					let s = ""
					let errType = ""
					if (r == 0) {
						errType = "success"
					} else {
						errType = "error"
					}

					if (r == 0) {
						s = "入库成功！";
					} else {
						s = "入库失败！";
					}

					_this.$message({
						message: s,
						type: errType,
						duration: 1500
					});

					//更新页面
					_this.get_approve();
				})
				.catch(function (error) {
					console.log(error);
				});
		},
		update_process(row) {
			this.$data.form = {
				id: row.id,
				p_id: row.p_id,
				process_name: row.process_name,
				process_t: row.process_t
			};
			this.$nextTick(() => {
				this.$refs['form_p'].clearValidate()
			});
			this.$data.update_visible = true;
		},
		input_change(value) {
			console.log(value);
			this.get_approve();
		},

		importDialog_close(done) {
			done();
		},
		handleSelectionChange(val) {
			console.log("已选择");
			console.log(val);
			this.multipleSelection = val;
		},

		del_approve() {
			var _this = this;

			axios.post('/pp/del_approve', {
				opuser: localStorage.getItem("online_user"),
				opuser_uuid: localStorage.getItem("uuid"),
				ids: _this.$data.multipleSelection,
			})
				.then(function (response) {

					let r = response.data.r;
					let s = ""
					let errType = ""
					if (r == 0) {
						s = "删除成功！";
						errType = "success"
					} else {
						s = "删除失败！";
						errType = "error"
					}

					_this.$message({
						message: s,
						type: errType,
						duration: 1500
					});

					if (r == 0) {
						_this.get_approve();//更新页面
					}

				})
				.catch(function (error) {
					console.log(error);
				});
		},

		get_approve() {
			document.getElementById("bt_search").blur();

			var _this = this;

			var objs;
			axios.post('/pp/get_plan_by_code', {
				opuser: localStorage.getItem("online_user"),
				opuser_uuid: localStorage.getItem("uuid"),
				index: this.$data.currentPage,
				count: this.$data.pagesize,
				applicant: this.$data.applicant_v,
			})
				.then(function (response) {
					console.log(response.data);
					objs = response.data.data;
					rcnt = response.data.total;
					_this.$data.tableData = objs;
					_this.$data.recordTotal = rcnt;
				})
				.catch(function (error) {
					console.log(error);
				});

		},

		handleSizeChange(val) {
			this.$data.pagesize = val;

			this.get_approve();
		},
		handleCurrentChange(val) {
			this.$data.currentPage = val;

			this.get_approve();
		},
		// 根据内容设置列表宽度
		flexColumnWidth(str, arr1, fontNum = 4, flag = 'max') {
			return window.flexColumnWidth(str, arr1, fontNum, flag);
		},
		// 操作日志
		add_operation_log(operation) {
			axios.post('/pp/add_operation_log', {
				opuser: localStorage.getItem("online_user"),
				opuser_uuid: localStorage.getItem("uuid"),
				operation: operation,
			}).then(function (response) {
				console.log(response);
			}).catch(function (error) {
				console.log(error);
			})

		}
	}
}
