Newer
Older
rescue_front / src / views / markerManage / editMarker.vue
[wangxitong] on 26 Mar 2022 8 KB first commit
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="markerForm" label-well-code="right" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="标识器ID" prop="markerId">
            <el-input v-model.trim="markerForm.markerId" :disabled="isEditMode" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="图片防伪ID" prop="securityId">
            <el-input v-model.trim="markerForm.securityId" :disabled="isEditMode" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="产品信息" prop="productInfo">
            <el-input v-model.trim="markerForm.productInfo" type="text" placeholder=""/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户信息" prop="deptId">
            <dept-select v-model="markerForm.deptId" :need-top="deptShowTop" :dept-show="deptShow" dept-type="03" placeholder="选择权属单位"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="批次号" prop="batchNum">
            <el-input v-model.trim="markerForm.batchNum" type="text" placeholder=""/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="巡检周期" prop="inspectionPeriod">
            <el-input v-model.trim="markerForm.inspectionPeriod" type="text" placeholder="必填">
              <template slot="append">天</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="安装日期" prop="installDate">
            <el-date-picker
              v-model.trim="markerForm.installDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="保质期" prop="shelfLife">
            <el-date-picker
              v-model.trim="markerForm.shelfLife"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="经度" prop="longitude">
            <el-input v-model.trim="markerForm.longitude" type="text" placeholder=""/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纬度" prop="latitude">
            <el-input v-model.trim="markerForm.latitude" type="text" placeholder=""/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="详细地址" prop="position">
            <el-input v-model.trim="markerForm.position" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="具体描述" prop="description">
            <el-input v-model.trim="markerForm.description" type="textarea" placeholder=""/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addMarker, updateMarker } from '@/api/marker'
import DeptSelect from '@/components/DeptSelect'

export default {
  name: 'EditMarker',
  components: { DeptSelect },
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update
      markerForm: {
        id: '',
        markerId: '',
        securityId: '',
        deptId: '',
        inspectionPeriod: '',
        productInfo: '',
        installDate: '',
        shelfLife: '',
        description: '',
        batchNum: '',
        longitude: '',
        latitude: ''
      }, // 表单
      deptProps: {
        parent: 'pid',
        value: 'id',
        label: 'name',
        children: 'children'
      },
      textMap: {
        update: '编辑',
        create: '新增'
      }, // 表头显示标题
      rules: {
        markerId: [{ required: true, message: '标识器ID不能为空', trigger: ['blur', 'change'] }],
        securityId: [{ required: true, message: '图片防伪ID不能为空', trigger: ['blur', 'change'] }],
        deptId: [{ required: true, message: '用户信息不能为空', trigger: ['blur', 'change'] }],
        inspectionPeriod: [{ required: true, message: '巡检周期不能为空', trigger: ['blur', 'change'] }],
        longitude: [{ required: true, message: '经度不能为空', trigger: ['blur', 'change'] }],
        latitude: [{ required: true, message: '纬度不能为空', trigger: ['blur', 'change'] }],
        position: [{ required: true, message: '详细地址不能为空', trigger: ['blur', 'change'] }]

      }, // 前端校验规则
      isEditMode: false,
      deptShowTop: false, // 权属单位下拉是否显示顶级
      deptShow: true
    }
  },
  watch: {
  },
  mounted() {
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.resetForm()
        this.isEditMode = false
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中
        this.markerForm = {
          id: row.id,
          markerId: row.markerId,
          securityId: row.securityId,
          deptId: row.deptId,
          inspectionPeriod: row.inspectionPeriod,
          productInfo: row.productInfo,
          installDate: row.installDate,
          shelfLife: row.shelfLife,
          position: row.position,
          description: row.description,
          batchNum: row.batchNum,
          longitude: row.longitude,
          latitude: row.latitude

        }
        // console.log('markerForm', this.markerForm)
        // if (row.deviceType !== '') {
        //   this.fetchDeviceModel(row.deviceType)
        // }
        this.isEditMode = true
      }
    },
    // 重置表单
    resetForm() {
      this.markerForm = {
        id: '',
        markerId: '',
        securityId: '',
        deptName: '',
        inspectionPeriod: '',
        productInfo: '',
        installDate: '',
        shelfLife: '',
        description: '',
        batchNum: '',
        longitude: '',
        latitude: ''
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        // console.log(this.markerForm)
        if (valid) {
          addMarker(this.markerForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功,是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
            }
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log('update valid')
          updateMarker(this.markerForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
</style>