<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2021-03-25 16:23:37
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter"
    :visible.sync="visible"
  >
    <el-row :gutter="15">
      <el-form
        ref="dataForm"
        :model="dataForm"
        :rules="rules"
        size="medium"
        label-width="110px"
        label-position="left"
      >
        <el-col :span="8">
          <el-form-item :label="$t('module.basicData.ScrapInfo.WasteName')" prop="name">
            <el-input v-model="dataForm.name" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.WasteName')])" clearable :style="{width: '100%'}" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('module.basicData.ScrapInfo.WasteCode')" prop="code">
            <el-input v-model="dataForm.code" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.WasteCode')])" clearable :style="{width: '100%'}" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item v-if="false" :label="$t('module.basicData.ScrapInfo.PlateId')" prop="basalId">
            <el-input v-model="dataForm.basalId" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.PlateId')])" clearable :style="{width: '100%'}" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('module.basicData.ScrapInfo.updateTime')" prop="boardTime">
            <el-date-picker
              v-model="dataForm.boardTime"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              :style="{width: '100%'}"
              :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.updateTime')])"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('module.basicData.ScrapInfo.RegisterPerson')" prop="creatorName">
            <el-input v-model="dataForm.creatorName" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.RegisterPerson')])" clearable :style="{width: '100%'}" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('module.basicData.ScrapInfo.source')" prop="source">
            <el-input v-model="dataForm.source" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.source')])" clearable :style="{width: '100%'}" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('module.basicData.ScrapInfo.wasteGrade')" prop="wasteGrade">
            <el-select
              v-model="dataForm.wasteGrade"
              :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.wasteGrade')])"
              clearable
              :style="{width: '100%'}"
            >
              <el-option
                v-for="item in wasteGradeArr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('module.basicData.ScrapInfo.cause')" prop="description">
            <el-input
              v-model="dataForm.description"
              type="textarea"
              :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.cause')])"
              :autosize="{minRows: 4, maxRows: 4}"
              :style="{width: '100%'}"
            />
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { ScrapInfoDetail, ScrapInfoUpdate, ScrapInfoAdd, ScrapInfoCode } from '@/api/basicData/ScrapInfo'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        code: '',
        basalId: '',
        source: '',
        boardTime: '',
        creatorName: '',
        wasteGrade: '',
        description: ''
      },
      wasteGradeArr: [{
        value: 0,
        label: '加工可用'
      },
      {
        value: 1,
        label: '完全废品'
      }],
      rules: {
        name: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.ScrapInfo.WasteName')]),
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.ScrapInfo.WasteCode')]),
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          ScrapInfoDetail(this.dataForm.id).then(res => {
            this.dataForm = res.data
          })
        } else {
          ScrapInfoCode().then(res => {
            this.dataForm.code = res.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'basalId': this.dataForm.basalId,
            'source': this.dataForm.source,
            'boardTime': this.dataForm.boardTime,
            'creatorName': this.dataForm.creatorName,
            'wasteGrade': this.dataForm.wasteGrade,
            'description': this.dataForm.description,
            'name': this.dataForm.name,
            'code': this.dataForm.code,
            'id': this.dataForm.id
          }
          if (this.dataForm.id) {
            ScrapInfoUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            ScrapInfoAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
