<template>
  <el-dialog
    :title="!dataForm.materialId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="500px">
    <el-form :model="dataForm" :rules="dataRule" size="mini" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="物料编码" prop="materialCode">
        <el-input v-model="dataForm.materialCode" placeholder="物料编码"></el-input>
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="dataForm.materialName" placeholder="物料名称"></el-input>
      </el-form-item>
      <el-form-item label="物料类型" prop="materialType">
        <el-select v-model="dataForm.materialType" placeholder="物料类型">
          <el-option
            v-for="item in dictData[0]"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="isOn">
        <el-switch
          v-model="dataForm.isOn"
          active-color="#409EFF"
          inactive-color="#F56C6C"
          :active-value="1"
          :inactive-value="0"
        />
      </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,
      dataForm: {
        materialId: 0,
        materialCode: '',
        materialName: '',
        materialType: '',
        isOn: 0
      },
      dataRule: {
        materialCode: [
          { required: true, message: '物料编码不能为空', trigger: 'blur' }
        ],
        materialName: [
          { required: true, message: '物料名称不能为空', trigger: 'blur' }
        ],
        materialType: [
          { required: true, message: '物料类型不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    dictData: Array
  },
  methods: {
    init (id) {
      this.dataForm.materialId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.materialId) {
          this.$http({
            url: this.$http.adornUrl(`/material/material/info/${this.dataForm.materialId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 200) {
              this.dataForm.materialCode = data.material.materialCode
              this.dataForm.materialName = data.material.materialName
              this.dataForm.materialType = String(data.material.materialType)
              this.dataForm.isOn = data.material.isOn
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/material/material/${!this.dataForm.materialId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'materialId': this.dataForm.materialId || undefined,
              'materialCode': this.dataForm.materialCode,
              'materialName': this.dataForm.materialName,
              'materialType': this.dataForm.materialType,
              'isOn': this.dataForm.isOn
            })
          }).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)
            }
          })
        }
      })
    }
  }
}
</script>
