<!--
 * @Description: 新增/编辑弹框
 * @Author: 空白格
 * @Date: 2023-07-04 13:55:00
 * @LastEditors: 空白格
 * @LastEditTime: 2023-07-06 09:08:39
 * @FilePath: \cattle_webui\src\views\system\noticeMgr\Dialog\AddOrEditDialog.vue
 * @Copyright: Copyright (c) 2016~2023 by 空白格, All Rights Reserved.
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="700px"
    :close-on-click-modal="false"
    @close="cancel"
  >
    <div class="dialog"
    >
      <el-form 
      v-loading="loading"
      :model="form" 
      ref="form" 
      :rules="rules" 
      label-width="120px">
        <el-form-item label="设备名称：" prop="deviceName">
          <el-input v-if="type!=3" v-model="form.deviceName" placeholder="请输入设备名称" />
          <span v-else>{{ form.deviceName }}</span>
        </el-form-item>
        <el-form-item label="设备编号：" prop="deviceNo">
          <el-input v-if="type!=3" v-model="form.deviceNo" placeholder="请输入设备编号" />
          <span v-else>{{ form.deviceNo }}</span>
        </el-form-item>
        <el-form-item label="所在位置：" prop="deviceAddress">
          <el-input v-if="type!=3" v-model="form.deviceAddress" placeholder="请输入所在位置" />
          <span v-else>{{ form.deviceAddress }}</span>
        </el-form-item>
        <el-form-item label="设备类型：" prop="deviceType">
          <dict-tag :options="dict.type.device_sys_type" :value="form.deviceType"/>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">关闭</el-button>
      <el-button
        type="primary"
        @click="submitForm"
        :loading="loading"
        element-loading-text="提交中..."
        v-if="type!=3"
      >
      保存
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { 
  saveAndAdd,
  saveAndEdit} from "@/api/device/pda";
export default {
  name: "addAndEdit",
  dicts: ['device_sys_type'],
  data() {
    return {
      title: "编辑",
      type: 0,
      model: "EDIT",
      open: false,
      loading: false,
      form: {},
      rules: {
        deviceName: [{ required: true, message: "请输入设备名称", trigger: ["change","blur"] }],
        deviceNo: [
          { required: true, message: "请输入设备编号", trigger: ["change","blur"] },
        ],
        deviceType: [
          { required: true, message: "请输入设备类型", trigger: ["change","blur"] },
        ],
        deviceAddress: [
          { required: true, message: "请输入所在位置", trigger: ["change","blur"] },
        ],
      },
    };
  },
  methods: {
    /**
     * 打开弹框
     * @date 2022-09-20
     * @param {any} obj
     * @returns {any}
     */
    openDialog(title, obj,type) {
      this.title = title;
      this.type = type
      this.open = true;
      if (obj){
        this.$nextTick(() => {
          this.form = Object.assign({},this.form,obj)
        });
      }else{
        this.$nextTick(() => {
          this.$refs["form"].clearValidate();
          this.$set(this.form,'deviceType',6)
        });
      }
    },
    /**
     * 保存
     * @date 2022-09-20
     * @returns {any}
     */
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true;
            if(this.form.id) {
              const { code } = await saveAndEdit({ ...this.form });
              if (code === 200) {
                this.$message.success("操作成功!");
                this.$emit("getList");
                this.cancel();
              }
            }else {
              const { code } = await saveAndAdd ({ ...this.form });
              if (code === 200) {
                this.$message.success("操作成功!");
                this.$emit("getList");
                this.cancel();
              }
            }
            
          } catch (error) {
          } finally {
            this.loading = false;
          }
        }
      });
    },
    /**
     * 重置
     * @date 2022-09-20
     * @returns {any}
     */
    reset() {
      this.$refs["form"].resetFields();
      this.form.id = undefined;
    },
    /**
     * 关闭弹框
     * @date 2022-09-20
     * @returns {any}
     */
    cancel() {
      this.reset();
      this.open = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  padding: 0 30px;
  max-height: 65vh;
  overflow-y: auto;
}
</style>
