<!--
 * @Author: Do not edit
 * @Date: 2024-01-09 13:49:03
 * @LastEditTime: 2024-01-09 20:39:57
 * @LastEditors: DY
 * @Description:
-->
<template>
  <div class="scrap">
    <div style="float: right" type="primary" size="small" plain @click="goback()">
      <img class="homeIcon" src="./../assets/home.png" alt="">
    </div>
    <h2>报废管理</h2>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="工单" prop="workOrderId">
        <el-select v-model="ruleForm.workOrderId" filterable clearable placeholder="请选择工单" style="width: 100%; display: block">
          <el-option v-for="(item, index) in workOrderList" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报废数量" prop="num">
        <el-input-Number v-model="ruleForm.num" style="width: 100%; display: block"></el-input-Number>
      </el-form-item>
      <el-form-item label="报废原因" prop="detName">
        <el-checkbox-group v-model="ruleForm.detName" style="width: 100%; display: block; text-align: left">
          <el-checkbox v-for="(item, index) in scrapList" :key="index" :label="item.content" :name="item.id"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="产线" prop="lineId">
        <el-select v-model="ruleForm.lineId" filterable clearable placeholder="请选择产线" style="width: 100%; display: block">
          <el-option v-for="(item, index) in lineArray" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="logTime">
        <el-date-picker type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" v-model="ruleForm.logTime" style="width: 100%;"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm('ruleForm')">取消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { workOrderList, scrapDetList, lineList, createScrap } from '@/api/quality'

export default {
  data () {
    return {
      ruleForm: {
        workOrderId: '',
        num: 0,
        detId: '',
        detName: [],
        lineId: undefined,
        logTime: new Date()
      },
      workOrderList: [],
      lineArray: [],
      scrapList: [],
      rules: {
        workOrderId: [
          { required: true, message: '请选择工单', trigger: 'change' }
        ],
        num: [
          { required: true, message: '请输入报废数量', trigger: 'blur' }
        ],
        detName: [
          { required: true, message: '请选择报废原因', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.getDict()
    this.ruleForm = {
      workOrderId: '',
      num: 0,
      detId: '',
      detName: [],
      lineId: undefined,
      logTime: new Date()
    }
  },
  methods: {
    goback () {
      this.$router.go(-1)
    },
    async getDict () {
      const workRes = await workOrderList()
      this.workOrderList = workRes.data.data
      await lineList().then(res => {
        this.lineArray = res.data.data.map(item => {
          return {
            name: item.name,
            id: item.id
          }
        })
      })
      const scrapRes = await scrapDetList()
      this.scrapList = scrapRes.data.data
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.detId = this.ruleForm.detName.join(',')
          createScrap(this.ruleForm).then(res => {
            console.log(res.data)
            if (res.data.code === 0) {
              this.$message({
                message: '提交成功！',
                type: 'success'
              })
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.scrap {
  padding: 30px;
  /* font-size: 20px; */
}
</style>
