<template>
	<view>
		<view class="white-card">
			<picker @change="bindPickerChange" :value="index" :range="materielList">
				<view class="list-list">
					<view class="list-title">物料名称</view>
					<view class="list-right">
						<text>{{materielList[index]?materielList[index]:'请选择'}}</text>
						<image class="next" src="../../static/next.png"></image>
					</view>
				</view>
			</picker>
			<view class="list-list">
				<view class="list-title">物料编号</view>
				<view class="list-right">
					<input v-model="froms.number" />
				</view>
			</view>
			<picker @change="bindPickerChangeReson" :value="resonIds" :range="repairRecordReasonList">
				<view class="list-list">
					<view class="list-title">不良现象</view>
					<view class="list-right">
						<text>{{repairRecordReasonList[resonIds]?repairRecordReasonList[resonIds]:'请选择'}}</text>
						<image class="next" src="../../static/next.png"></image>
					</view>
				</view>
			</picker>
			<picker mode="date" @change="dateChange($event, index)">
				<view class="list-list">
					<view class="list-title">返修日期</view>
					<view class="list-right"><input v-model="froms.out_date" disabled="" placeholder="请选择" /></view>
				</view>
			</picker>
			<view class="list-list">
				<view class="list-title">返修合格数</view>
				<view class="list-right"><input v-model="froms.ok_quantity"  placeholder="请输入" /></view>
			</view>
			<view class="list-list">
				<view class="list-title">返修报废数</view>
				<view class="list-right"><input v-model="froms.scrap_quantity"  placeholder="请输入" /></view>
			</view>
			<view class="bzqk-card">
				<textarea value="" v-model="froms.remark" placeholder="备注情况说明" />
			</view>
		</view>
		<view class="submit-btn"><button :disabled="buttonState==false?true:false" @tap="submit"
				type="primary">提交</button></view>
	</view>
</template>

<script>
	import {
		repairRecordMateriel,
		repairRecordReason,
		AddrepairRecord
	} from "../../api/user.js"
	export default {
		data() {
			return {
				materielList: [],
				index: -1,
				repairRecordReasonList: [],
				resonIds: 0,
				repairRecordMateriel: [],
				froms: {
					number: '',
					name: '',
					out_date: '',
					no_reason: '',
					ok_quantity: '',
					scrap_quantity: '',
					remark: ''
				},
				buttonState: true,
			};
		},
		onLoad() {
			this.getMateriel();
			this.getrepairRecordReason()
		},
		methods: {
			getMateriel() {
				repairRecordMateriel().then(res => {
					this.repairRecordMateriel = res
					let newArr = []
					res.map(item => {
						newArr.push(item.name)
					})
					this.materielList = newArr

				})
			},
			getrepairRecordReason() {
				repairRecordReason().then(res => {
					this.repairRecordReasonList = res
					console.log("不良现象", res)
				})
			},
			bindPickerChange(e) {
				this.index = e.target.value
				this.froms.number = this.repairRecordMateriel[this.index].number
			},
			dateChange(e, index) {
				this.froms.out_date = e.detail.value;
			},
			bindPickerChangeReson(e) {
				this.resonIds = e.detail.value
			},
			submit() {
				this.froms.name = this.repairRecordMateriel[this.index].name
				this.froms.no_reason = this.repairRecordReasonList[this.resonIds]
				this.buttonState = false;
				this.$functions.confirm("是否进行此操作?").then(() => {
						AddrepairRecord(this.froms
						).then(res => {
						this.$functions.success('提交成功').then(() => {
							uni.navigateBack();
						})
					}).catch(res => {
						this.buttonState = true;
					});
				}).catch(res => {
				this.buttonState = true;
			});

		}
	}
	}
</script>

<style lang="scss">
	@import "./index.scss"
</style>
