<template>
  <div>
    <el-dialog
      width="340px"
      @close="closeDialog"
      :title="!this.addOrUpdate ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :model="dataForm" label-position="top" :inline="true" ref="dataForm"
               @keyup.enter.native="dataFormSubmit()"
               label-width="120px">
        <el-form-item required>
          <el-link @click="getBaseList(113)" slot="label">设备编码</el-link>
          <el-input v-model="dataForm.resourceId"></el-input>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input disabled v-model="dataForm.resourceName"></el-input>
        </el-form-item>
        <el-form-item required>
          <el-link @click="getBaseList(114)" slot="label">备品备件编码</el-link>
          <el-input v-model="dataForm.partNo"></el-input>
        </el-form-item>
        <el-form-item label="备品备件名称">
          <el-input disabled v-model="dataForm.partDesc"></el-input>
        </el-form-item>
        <el-form-item required label="可用数量">
          <el-input-number style="width: 146px" :min="0" v-model="dataForm.qtyUsable"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
       <el-button type="primary" @click="visible = false">取消</el-button>
    </span>

    </el-dialog>
    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
import Chooselist from '@/views/modules/common/Chooselist_eam'
import {getResourceSpareInfo, saveResourceSpare, updateResourceSpare} from '@/api/partspare/resourcespare.js'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        resourceId: '',
        resourceIdOld: '',
        resourceName: '',
        partNo: '',
        partNoOld: '',
        partDesc: '',
        qtyUsable: '',
        delflag: '',
        createdBy: '',
        site: this.$store.state.user.site
      },
      addOrUpdate: false,
    }
  },
  components: {
    Chooselist
  },
  methods: {
    // 获取基础数据列表S
    getBaseList(val, type) {
      this.tagNo = val
      this.$nextTick(() => {
        let strVal = ''
        let conSql = ''
        if (val === 113) {
          strVal = this.dataForm.resourceId
        }
        if (val === 114) {
          strVal = this.dataForm.partNo
        }
        this.$refs.baseList.init(val, strVal, conSql)
      })
    },
    /* 列表方法的回调 */
    getBaseData(val) {
      if (this.tagNo === 113) {
        this.dataForm.resourceId = val.ObjectID
        this.dataForm.resourceName = val.ObjectDesc
      }
      if (this.tagNo === 114) {
        this.dataForm.partNo = val.part_no
        this.dataForm.partDesc = val.part_description
      }
    },
    init(row) {
      this.addOrUpdate = row ? true : false
      this.visible = true
      this.$nextTick(() => {
        if (this.addOrUpdate) {
          this.dataForm.site = row.site
          this.dataForm.resourceId = row.resourceId
          this.dataForm.partNo = row.partNo
          getResourceSpareInfo(this.dataForm).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.resourceId = data.resourceSpare.resourceId
              this.dataForm.resourceName = data.resourceSpare.resourceDesc
              this.dataForm.partNo = data.resourceSpare.partNo
              this.dataForm.partDesc = data.resourceSpare.partDescription
              this.dataForm.site = data.resourceSpare.site
              this.dataForm.qtyUsable = data.resourceSpare.qtyUsable
              this.dataForm.resourceIdOld = data.resourceSpare.resourceId
              this.dataForm.partNoOld = data.resourceSpare.partNo
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      if (this.addOrUpdate) {
        updateResourceSpare(this.dataForm).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success(data.msg)
            this.visible = false
            this.$emit('refreshDataList')
          } else {
            this.$message.error(data.msg)
          }
        })
      } else {
        saveResourceSpare(this.dataForm).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success(data.msg)
            this.visible = false
            this.$emit('refreshDataList')
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    },
    closeDialog() {
      this.$nextTick(() => {
        this.$emit('refreshDataList')
        Object.assign(this.$data, this.$options.data.call(this));
      })
    },
  }
}
</script>
