<template>
  <div class='app-container'>
    <el-form>
      <el-form-item>
        <el-button type="primary" icon="Plus" plain @click="getDialog()">新增分组</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="data.tableData" v-loading="loading">
      <el-table-column label="分组名称" prop="name" align="center"></el-table-column>
      <el-table-column label="相关设备数量" prop="deviceNum" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="getDialog(scope.row.id)" v-hasUser="scope.row.userId">编辑</el-button>
          <el-button type="danger" link @click="getDelete(scope.row)" v-hasUser="scope.row.userId">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="data.form.pageNum" v-model:limit="data.form.pageSize"
      @pagination="getList" />
    <!-- 弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="500" :before-close="getClose">
      <el-form :model="data.dialogForm" ref="dialogForm" :rules="data.rules">
        <el-form-item label="分组名称：" prop="name">
          <el-input v-model="data.dialogForm.name" placeholder="请输入分组名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="getClose">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { list, submit, detail, remove, edit } from "@/api/store/group"
import useUserStore from "@/store/modules/user"
let { proxy } = getCurrentInstance()

// 遮罩层
const loading = ref(false)

// 数据
const data = reactive({
  // 表单
  form: {
    pageNum: 1,
    pageSize: 10
  },
  // 列表
  tableData: [],
  // 弹窗表单
  dialogForm: {},
  // 校验
  rules: {
    name: [{
      required: true, message: '请输入分组名称', trigger: 'blur'
    }]
  }
})

// 总数据
const total = ref(0)

// 列表
const getList = () => {
  loading.value = true
  list(data.form).then(res => {
    if (res.code === 0) {
      data.tableData = res.data
      total.value = res.total
      loading.value = false
    }
  })
}
getList()

// 弹窗
const dialogVisible = ref(false)
// 弹窗标题
const title = ref('')
// 打开弹窗
function getDialog(id) {
  dialogVisible.value = true
  title.value = '新增'
  if (id) {
    getDetail(id)
  }
}

// 详情
const getDetail = (id) => {
  title.value = '编辑'
  detail({ id: id }).then(res => {
    if (res.code === 0) {
      data.dialogForm = res.data
    }
  })
}

// 新增
const getSubmit = () => {
  proxy.$refs.dialogForm.validate(valid => {
    if (valid) {
      if (data.dialogForm.id) {
        edit(data.dialogForm).then(res => {
          if (res.code === 0) {
            proxy.$modal.msgSuccess('编辑成功！')
            getClose()
            getList()
          }
        })
      } else {
        submit(data.dialogForm).then(res => {
          if (res.code === 0) {
            proxy.$modal.msgSuccess('新增成功！')
            getClose()
            getList()
          }
        })
      }
    } else {
      return false
    }
  })
}

// 关闭
const getClose = () => {
  dialogVisible.value = false
  data.dialogForm = {}
  proxy.$refs.dialogForm.resetFields()
}

// 删除
const getDelete = (row) => {
  proxy.$modal.confirm(`是否删除分组名称为：${row.name}的数据?`).then(() => {
    remove({ id: row.id }).then(res => {
      if (res.code === 0) {
        proxy.$modal.msgSuccess('删除成功！')
        getList()
      }
    })
  })
}
</script>

<style lang="scss" scoped>
.header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}
</style>