<template>
  <div>
    <el-button type="primary" @click="showAddDialog">添加</el-button>

    <el-dialog v-model="dialogVisible" title="添加" width="30%" @close="clearForm">
      <el-form :model="form" ref="formData" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="ip" prop="ip">
          <el-input v-model="form.ip"></el-input>
        </el-form-item>
        <el-form-item label="端口号" prop="port">
          <el-input v-model="form.port"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input v-model="form.status"></el-input>
        </el-form-item>

        <el-form-item>
          <!-- Add button to submit the form -->
          <el-button type="primary" @click="addOrUpdateData">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="id" width="180">
        <template #default="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" width="180">
        <template #default="scope">
          <span style="margin-left: 10px">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主机ip" width="180">
        <template #default="scope">
          <span style="margin-left: 10px">{{ scope.row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column label="端口" width="180">
        <template #default="scope">
          <span style="margin-left: 10px">{{ scope.row.port }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="180">
        <template #default="scope">
          <span style="margin-left: 10px">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180">
        <template #default="scope" dataFormat="yyyy-MM-dd HH:mm:ss">
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建用户" width="180">
        <template #default="scope">
          <span style="margin-left: 10px">{{ scope.row.createUser }}</span>
        </template>
      </el-table-column>

      <el-table-column label="修改时间" width="180">
        <template #default="scope" dataFormat="yyyy-MM-dd HH:mm:ss">
          <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改用户" width="180">
        <template #default="scope">
          <span style="margin-left: 10px">{{ scope.row.updateUser }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-dialog v-model="dialogVisible1" title="修改" width="30%" @close="clearForm1">
            <el-form :model="form" ref="formData" label-width="80px">
              <el-form-item label="id" prop="id">
                <el-input v-model="form.id"></el-input>
              </el-form-item>
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name"></el-input>
              </el-form-item>
              <el-form-item label="ip" prop="ip">
                <el-input v-model="form.ip"></el-input>
              </el-form-item>
              <el-form-item label="端口号" prop="port">
                <el-input v-model="form.port"></el-input>
              </el-form-item>
              <el-form-item label="状态" prop="status">
                <el-input v-model="form.status"></el-input>
              </el-form-item>
              <el-form-item label="创建人" prop="creatUser">
                <el-input v-model="form.createUser"></el-input>
              </el-form-item>
              <el-form-item label="创建时间" prop="createTime">
                <el-input v-model="form.createTime"></el-input>
              </el-form-item>
              <el-form-item label="修改人" prop="updateUser">
                <el-input v-model="form.updateUser"></el-input>
              </el-form-item>
              <el-form-item label="修改时间" prop="updateTime">
                <el-input v-model="form.updateTime"></el-input>
              </el-form-item>

              <el-form-item>
                <!-- Add button to submit the form -->
                <el-button type="primary" @click="addOrUpdateData">确认</el-button>
              </el-form-item>
            </el-form>
          </el-dialog>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import 《组件名称》 from '《组件路径》,
import { listDatax, updateDatax } from "@/api/data-datax/datax";
import { insertDatax } from "@/api/data-datax/datax";
import { delDatax } from "@/api/data-datax/datax";

export default {
    //import引入的组件需要注入到对象中才能使用"
    components: {},
    props: {},
    data() {
      //这里存放数据"

      return {
        tableData: [],
        form: {
          id: '',
          name: '',
          ip: '',
          port: '',
          status: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: ''
        },
        dialogVisible: false,
        dialogVisible1: false
      };
    },
    //计算属性 类似于data概念",
    computed: {},
    //监控data中的数据变化",
    watch: {},
    //方法集合",
    methods: {
      show() {
        listDatax(this.tableData).then(res => {
          this.tableData = res.data;
        });
      },
      showAddDialog() {
        this.dialogVisible = true;
      },
      handleEdit(form) {
        this.form = form
        this.dialogVisible1 = true
      },
      addOrUpdateData() {
        this.$refs.formData.validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              await updateDatax({ data: this.form });
              location.reload()
            } else {
              console.log(this.form)
              await insertDatax({ data: this.form });
              location.reload()
            }

            this.dialogVisible = false;
            this.list();
          } else {
            this.$message.error("Please fill in all the required fields");
          }
        });
      },
      clearForm() {
        this.$refs.formData.resetFields();
      },
      clearForm1() {
        this.$refs.formData.resetFields();
      },
      handleDelete(id) {
        this.form.id=id
        delDatax({ id: this.form.id }).then(res=>{
          location.reload()
        })
      }
    },
    //生命周期 - 创建完成（可以访问当前this实例）",
    created() {
      this.show()
    },
    //生命周期 - 挂载完成（可以访问DOM元素）",
    mounted() {
    },
    beforeCreate() {
    }, //生命周期 - 创建之前",
    beforeMount() {
    }, //生命周期 - 挂载之前",
    beforeUpdate() {
    }, //生命周期 - 更新之前",
    updated() {
    }, //生命周期 - 更新之后",
    beforeDestroy() {
    }, //生命周期 - 销毁之前",
    destroyed() {
    }, //生命周期 - 销毁完成",
    activated() {
    } //如果页面有keep-alive缓存功能，这个函数会触发",
  };
</script>
<style scoped>

</style>
