<template>
  <div>
    <n-card>
      <data-table
        :columns="data.columns"
        :row-key="(row) => row.id"
        :request="loadDataTable"
        size="large"
      >
        <template #tableTitle>
          <n-button type="primary" @click="handleUser"> 新建 </n-button>
          <!-- <n-button type="primary" @click="deleteUsers"> 删除 </n-button> -->
        </template>
      </data-table>
    </n-card>
  </div>
  <!-- 新增、编辑弹窗 -->
  <user-modal v-if="data.modalShow" v-model:visible="data.modalShow" :row="rowData" :title="modalTitle" />
</template>

<script>
import dataTable from '@/components/DataTable/index.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import TableImage from '@/components/DataTable/tools/Image.vue'
import TableTags from '@/components/DataTable/tools/Tags.vue'
import { getUserList } from '@/api/system/user/index.js'
import { h, unref, ref } from 'vue'
import { reactive } from 'vue'
import UserModal from './components/UserModal.vue'
export default {
  name: 'MenuPage',
  components: { dataTable, UserModal },
  setup() {
    const data = reactive({
      columns: [
        {
          title: '用户编号',
          key: 'code',
          align: 'center'
        },
        {
          title: '头像',
          key: 'avatar',
          align: 'center',
          render(row) {
            return h(TableImage, {
              images: {
                width: 36,
                height: 36,
                src: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQiKrYt-fEfJ4SZhF9KfLLaX9f15KV6Ve1ptA&usqp=CAU'
              }
            })
          }
        },
        {
          title: '用户账号',
          key: 'username',
          align: 'center'
        },
        {
          title: '用户姓名',
          key: 'realname',
          align: 'center'
        },
        {
          title: '用户类型',
          key: 'type',
          align: 'center',
          width: 100
        },
        {
          title: '角色',
          key: 'roles',
          align: 'center',
          render(row) {
            return h(TableTags, {
              data: row.roles
            })
          }
        },
        {
          title: '状态',
          key: 'status',
          align: 'center',
          width: 100
        },
        {
          title: '部门',
          key: 'deptName',
          align: 'center'
        },
        {
          title: '创建时间',
          key: 'createTime',
          align: 'center',
          width: 160
        },
        {
          title: '更新时间',
          key: 'updateTime',
          align: 'center',
          width: 160
        },
        {
          title: '操作',
          align: 'center',
          width: 150,
          fixed: 'right',
          render(row) {
            return h(TableAction, {
              actions: [
                {
                  label: '修改',
                  type: 'button',
                  props: {
                    type: 'primary',
                    onClick: play.bind(null, row)
                  },
                  auth: 'basic_list'
                },
                {
                  label: '删除',
                  type: 'popconfirm',
                  auth: 'basic_list'
                }
              ],
              align: 'center'
            })
          }
        }
      ],
      modalShow: false
    })
    const modalTitle = ref('添加用户')
    let rowData = reactive({})
    // 打开编辑弹窗
    function play(row) {
      rowData = row
      modalTitle.value = '编辑用户'
      console.log('编辑弹窗内容：', row)
      data.modalShow = true
    }

    const params = reactive({
      name: ''
    })

    const loadDataTable = async(res) => {
      const _params = {
        ...unref(params),
        ...res
      }
      return await getUserList(_params)
    }
    function handleUser() {
      rowData = {}
      modalTitle.value = '添加用户'
      data.modalShow = true
    }

    return { data, loadDataTable, handleUser, rowData, modalTitle }
  }
}
</script>
<style scoped lang='scss'>
.n-button + .n-button {
  margin-left: 10px;
}
</style>
