<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <div class="manage-container">
      <vab-query-form>
        <el-button type="primary" @click="handleAdd">添加组</el-button>
      </vab-query-form>
      <el-table :data="groupData" border stripe style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          :label="$t('grouping.zmc')"
          width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('defalult.cz')"
          align="center"
        >
          <template #default="{ row }">
            <el-button type="text" @click="handleDelete(row)">
              {{ $t('defalult.sc') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <group-edit ref="edit" @RefreshGroup="RefreshGroup"></group-edit>
    </div>
  </el-dialog>
</template>
<script>
import { doDelete, getList } from '@/api/group';
import groupEdit from './groupEdit';
export default {
  name: 'Group',
  components: {
    groupEdit,
  },
  data() {
    return {
      groupData: [],
      lazy: true,
      activeName: 'first',
      layout: 'total, sizes, prev, pager, next, jumper',
      title: '',
      dialogFormVisible: false,
      total: 0,
      input: '',
      textarea: '',
      projectId: '',
      list: [],
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {},
  methods: {
    async fetchData() {
      this.listLoading = true;
      const params = {
        projectId: this.projectId,
      };
      const { data } = await getList(params);
      this.groupData = data;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    showGroup(row) {
      this.projectId = row.id;
      this.dialogFormVisible = true;
      this.fetchData();
    },
    RefreshGroup(projectId) {
      this.projectId = projectId;
      this.fetchData();
    },
    handleAdd() {
      this.$refs['edit'].showEdit(this.projectId);
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
    close() {
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    handleDelete(row) {
      const that = this;
      if (row.id) {
        this.$confirm('你确定要删除当前项吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const { msg } = doDelete({ groupId: row.id });
            this.$message({
              type: 'success',
              message: msg == undefined ? '删除成功' : msg,
            });
            setTimeout(function () {
              that.fetchData();
            }, 1000);
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            });
          });
      }
    },
  },
};
</script>
