<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="pigstyId">
        <!-- <el-input v-model="dataForm.pigstyId"></el-input> -->
        <el-select placeholder="请选择" v-model="dataForm.pigstyId" style="width: 100%">
          <el-option
            v-for="item in pigstyList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备编号" prop="id">
        <el-input v-model="dataForm.id" placeholder="请输入"></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,
          pigstyId: ''
        },
        dataRule: {
          pigstyId: [
            { required: true, message: '猪舍不能为空', trigger: 'blur' }
          ],
          id: [
            { required: true, message: '设备编号不能为空', trigger: 'blur' }
          ]
        },
        tempKey: -666666, // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
        pigstyList: []
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        console.log(id);
        this.pigstyList = []
        this.$http({
          url: this.$http.adornUrl("/management/pigsty/findAll"),
          method: "get",
          params: this.$http.adornParams({})
        }).then(({data}) => {
          if (data && data.code === 0) {
            data.all.forEach(pigsty => {
              let item = {
                value: pigsty.id,
                label: pigsty.number
              }
              this.pigstyList.push(item)
            })
          }
        })
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/management/device/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            console.log(data);
            if (data && data.code === 0) {
              console.log(data);
              this.dataForm.pigstyId = data.device.pigstyId
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/management/device/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'deviceId': this.dataForm.id,
                'pigstyId': this.dataForm.pigstyId
                // '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>
