<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" 
    width="600px">
    <el-form 
      :model="dataForm" 
      :rules="dataRule" 
      ref="dataForm" 
      @keyup.enter.native="dataFormSubmit()" 
      label-width="80px"
      style="margin-left: 20px;width: 500px"
      size="mini">
      <el-form-item label="饲料名称" prop="ingredients">
        <el-input v-model="dataForm.ingredients"></el-input>
      </el-form-item>
      <el-form-item label="详细信息" prop="details">
        <el-input v-model="dataForm.details"></el-input>
      </el-form-item>
      <el-form-item label="使用阶段" prop="stage">
        <el-input v-model="dataForm.stage"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">关闭</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        menuList: [],
        menuListTreeProps: {
          label: 'name',
          children: 'children'
        },
        dataForm: {
          id: 0,
          ingredients: '',
          details: '',
          stage: '',
          remark: ''
        },
        dataRule: {
          ingredients: [
            { required: true, message: '饲料名称不能为空', trigger: 'blur' }
          ],
          details: [
            { required: true, message: '详细信息不能为空', trigger: 'blur' }
          ],
          stage: [
            { required: true, message: '使用阶段不能为空', trigger: 'blur' }
          ]
        },
        tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        console.log(id);
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/management/basefeedformula/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            console.log(data);
            if (data && data.code === 0) {
              this.dataForm.ingredients = data.baseFeedFormula.ingredients
              this.dataForm.details = data.baseFeedFormula.details
              this.dataForm.stage = data.baseFeedFormula.stage
              this.dataForm.remark = data.baseFeedFormula.remark
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/management/basefeedformula/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'ingredients': this.dataForm.ingredients,
                'details': this.dataForm.details,
                'stage': this.dataForm.stage,
                'remark': this.dataForm.remark || undefined
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.visible = false
                this.$emit('refreshDataList')
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  // onClose: () => {
                  // }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style scoped>

</style>
