<template>
  <el-form ref="form" :rules="rules" label-width="110px" :model="form">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="产线" prop="proLineId">
          <el-select
            v-model="form.proLineId"
            placeholder="产线"
            style="width: 100%"
            disabled
          >
            <el-option
              v-for="item in productionLineList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="备件类别" prop="sparePartType">
          <el-select
            v-model="form.sparePartType"
            placeholder="备件类别"
            style="width: 100%"
            disabled
          >
            <el-option
              v-for="(item, i) in sparePartsTypeList"
              :key="i"
              :label="item.dataName"
              :value="item.dataName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="备件名称" prop="sparePartName">
          <el-select
            v-model="form.sparePartName"
            placeholder="备件名称"
            @change="selectModel"
            style="width: 100%"
            disabled
          >
            <el-option
              v-for="(item, i) in sparePartsList"
              :key="i"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="备件型号" prop="model">
          <el-select
            v-model="form.model"
            placeholder="备件型号"
            style="width: 100%"
            disabled
          >
            <el-option
              v-for="(item, i) in sparePartsSpecList"
              :key="i"
              :label="item.model"
              :value="item.model"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="批次编码" prop="batchCode">
          <el-select
            v-model="form.batchCode"
            placeholder="批次编码"
            style="width: 100%"
            @change="batchChange"
          >
            <el-option
              v-for="(item, i) in batchCodeList"
              :key="i"
              :label="item.batchCode"
              :value="item.batchCode"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="供应商" prop="supplier">
          <el-input
            v-model="form.supplier"
            placeholder="供应商"
            disabled
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="出库数量" prop="outNum">
          <el-input-number
            v-model="form.outNum"
            :min="0"
            :max="999999999"
            style="width: 100%"
          ></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="领用人" prop="recipient">
          <el-input v-model="form.recipient" placeholder="领用人"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="24">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="备注"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { getModelList, getNameList } from '@/api/basicConfig'
import {
  sparePartStockOutStock,
  getBatchList,
  getSparePartStock
} from '@/api/deviceManagement'
export default {
  name: 'SparePartsOut',
  data() {
    return {
      form: {
        id: '',
        proLineId: '',
        sparePartType: '',
        sparePartName: '',
        model: '',
        batchCode: '',
        supplier: '',
        outNum: '',
        recipient: '',
        remark: ''
      },
      rules: {
        proLineId: [
          { required: true, message: '请选择产线', trigger: 'change' }
        ],
        sparePartType: [
          { required: true, message: '请选择备件类型', trigger: 'change' }
        ],
        sparePartName: [
          { required: true, message: '请选择备件名称', trigger: 'change' }
        ],
        model: [
          { required: true, message: '请选择备件型号', trigger: 'change' }
        ],
        batchCode: [
          { required: true, message: '请选择批次编码', trigger: 'change' }
        ],
        outNum: [
          { required: true, message: '请输入出库数量', trigger: 'blur' }
        ],
        recipient: [
          { required: true, message: '请输入领用人', trigger: 'blur' }
        ]
      },
      productionLineList: JSON.parse(localStorage.getItem('publicList'))
        .proLineVoList,
      sparePartsTypeList: JSON.parse(localStorage.getItem('publicList'))
        .sparePartsTypeList,
      sparePartsList: [],
      sparePartsSpecList: [],
      batchCodeList: []
    }
  },
  methods: {
    init(id) {
      if (id) {
        this.form.id = id
        this.getBatchList(id)
        getSparePartStock({ id }).then((res) => {
          this.form.proLineId = res.data.proLineId
          this.form.sparePartType = res.data.sparePartType
          this.form.sparePartName = res.data.sparePartName
          this.form.model = res.data.model
          this.form.maxNum = res.data.maxNum
          this.form.minNum = res.data.minNum
          this.form.stockPosition = res.data.stockPosition
          this.form.remark = res.data.remark
        })
      }
      this.form.recipient = this.$store.getters.username
        ? this.$store.getters.username
        : ''
    },
    getBatchList(id) {
      getBatchList({ id }).then((res) => {
        this.batchCodeList = res.data
      })
    },
    getNameList() {
      getNameList().then((res) => {
        this.sparePartsList = res.data
        this.sparePartsSpecList = []
      })
    },
    selectModel(val) {
      getModelList({
        name: val
      }).then((res) => {
        this.sparePartsSpecList = res.data
        this.form.model = ''
      })
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          sparePartStockOutStock({
            id: this.form.id,
            batchCode: this.form.batchCode,
            outNum: this.form.outNum,
            recipient: this.form.recipient,
            remark: this.form.remark
          }).then((res) => {
            if (res.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.$emit('successSubmit')
            }
          })
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
      this.sparePartsList = []
      this.sparePartsSpecList = []
      this.batchCodeList = []
    },
    batchChange(val) {
      this.batchCodeList &&
        this.batchCodeList.map((item) => {
          if (item.batchCode === val) {
            this.form.supplier = item.supplier
          }
        })
    }
  }
}
</script>
