<template>
  <el-dialog
    :title="!id ? '新增': display ? '详情' : '修改'"
    width="70%"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
      <el-row class="my-row">
        <el-form-item label="编码" prop="conversionCode">
          <el-input v-model="dataForm.conversionCode" :disabled="true" placeholder="系统自动生成"></el-input>
        </el-form-item>
      </el-row>
      <el-row class="my-row">
        <el-col :span="12">
          <el-form-item label="转换前数量">
            <el-input value="1" :disabled="true" placeholder="数量"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" style="padding-left: 20px">
          <el-form-item label="单位" prop="convertUnitBefore">
            <el-input v-model="dataForm.convertUnitBefore" :disabled="display" placeholder="单位"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="my-row">
        <el-col :span="12">
          <el-form-item label="转换后数量" prop="cnt">
            <el-input-number v-model="dataForm.cnt" :disabled="display" placeholder="转换后数量" :min="1" :step="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12" style="padding-left: 20px">
          <el-form-item label="单位" prop="convertUnit">
            <el-input v-model="dataForm.convertUnit" :disabled="display" placeholder="转换后单位"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="my-row">
        <el-form-item label="备注说明" prop="notes">
          <el-input v-model="dataForm.notes" :disabled="display" placeholder="编码"></el-input>
        </el-form-item>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button v-if="!display" @click="visible = false">取消</el-button>
      <el-button v-if="!display" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getConversionDetail, addOrUpdateConversion } from '../../../api/warehouse'
  export default {
    name: 'unit-add',
    data () {
      return {
        visible: false,
        display: false,
        id: 0,
        dataForm: {
          cnt: 1
        },
        dataRule: {
          convertUnitBefore: [{ required: true, message: '转换前单位不能为空', trigger: 'blur' }],
          cnt: [{ required: true, message: '转换后数量不能为空', trigger: 'blur' }],
          convertUnit: [{ required: true, message: '转换后单位不能为空', trigger: 'blur' }]
        }
      }
    },
    methods: {
      async init (id, display) {
        this.dataForm = {}
        this.visible = true
        this.id = id || 0
        this.display = display
        if (!id) return
        await getConversionDetail(this.id).then(({data}) => {
          if (data && data.code === '200') {
            this.dataForm = data.data
          }
        })
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            let params = {
              cnt: this.dataForm.cnt,
              conversionCode: this.dataForm.conversionCode,
              conversionId: this.id ? this.id : null,
              convertUnit: this.dataForm.convertUnit,
              convertUnitBefore: this.dataForm.convertUnitBefore,
              orgId: this.orgId,
              notes: this.dataForm.notes
            }
            addOrUpdateConversion(params, !this.id).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            }).catch(() => {})
          }
        })
      }
    }
  }
</script>

<style scoped>
.my-line{
  border-bottom: 1px solid #c0c4cc;
  margin-bottom: 10px;
}
</style>
