<template>
  <div>
    <n-card>
      <headSearch :info="info" @search="handleSearch" />
      <data-table
        ref="tableRef"
        :columns="columns"
        :row-key="(row) => row.id"
        :request="loadDataTable"
        size="large"
        scroll-x="1200"
        @update:checked-row-keys="handleCheck"
      >
        <template #tableTitle>
          <n-button type="primary" @click="handleModal"> 新建 </n-button>
          <n-popconfirm
            negative-text="取消"
            positive-text="确认"
            @positive-click="deleteComplex"
          >
            <template #trigger>
              <n-button type="primary">删除</n-button>
            </template>
            确定删除选中的数据吗?
          </n-popconfirm>
        </template>
      </data-table>
    </n-card>
  </div>
  <!-- 新增、编辑弹窗 -->
  <role-modal
    v-if="modalShow"
    v-model:visible="modalShow"
    :row="rowData"
    @done="handleSearch"
  />
  <!-- 分配权限 -->
  <config-modal
    v-if="configModalShow"
    v-model:visible="configModalShow"
    :row="rowData"
    @done="handleSearch"
  />
</template>

<script>
import headSearch from '@/components/Search/index.vue'
import dataTable from '@/components/DataTable/index.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import TableSwitch from '@/components/DataTable/tools/Switch.vue'
import {
  getRoleList,
  deleteRole,
  setRoleStatus
} from '@/api/system/role/index'
import { h, ref, unref, toRefs, reactive } from 'vue'
import table from './table.js'
import info from './info.js'
import RoleModal from './components/RoleModal.vue'
import ConfigModal from './components/ConfigModal.vue'

export default {
  name: 'MenuPage',
  components: { headSearch, dataTable, RoleModal, ConfigModal },
  setup() {
    const data = reactive({
      columns: [
        ...table.columns,
        {
          title: '状态',
          key: 'status',
          align: 'center',
          width: 100,
          render(row) {
            return h(TableSwitch, {
              data: { id: row.id, status: row.status },
              rowKey: 'status',
              checkedValue: 1,
              uncheckedValue: 2,
              onChange: setStatus.bind(row)
            })
          }
        },
        {
          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: 'button',
                  props: {
                    type: 'primary',
                    ghost: true,
                    onClick: configure.bind(null, row)
                  },
                  auth: 'basic_list'
                },
                {
                  label: '删除',
                  type: 'popconfirm',
                  auth: 'basic_list',
                  tip: '确定删除这条数据吗?',
                  props: {
                    negativeText: '取消',
                    positiveText: '确认',
                    onPositiveClick: deleteSingle.bind(null, row.id)
                  }
                }
              ],
              align: 'center'
            })
          }
        }
      ],
      info: ref(info),
      modalShow: false,
      configModalShow: false,
      rowData: {}
    })
    // 编辑
    function play(row) {
      data.rowData = row
      data.modalShow = true
    }

    // 配置权限
    function configure(row) {
      data.rowData = row
      data.configModalShow = true
    }

    const params = ref({})
    const tableRef = ref()
    function handleSearch(data) {
      params.value = {
        ...data
      }
      tableRef.value.reFetch({ ...unref(params) })
    }

    const loadDataTable = async(res) => {
      const _params = {
        ...unref(params),
        ...res
      }
      return await getRoleList(_params)
    }

    // 新增用户
    function handleModal() {
      data.rowData = {}
      data.modalShow = true
    }

    // 选择表格数据
    const selectedIds = ref([])
    function handleCheck(rowKeys) {
      selectedIds.value = rowKeys
    }
    // 批量删除
    function deleteComplex() {
      if (selectedIds.value.length) {
        deleteData(selectedIds.value)
      } else {
        $message.warning('请至少选中一条数据')
      }
    }
    // 单个删除数据
    function deleteSingle(id) {
      deleteData([id])
    }

    // 删除接口
    function deleteData(data) {
      deleteRole(data)
        .then((res) => {
          if (res.code === 0) {
            handleSearch()
            $message.success(res.msg)
          } else {
            $message.error(res.msg)
          }
        })
        .catch((e) => {
          console.log(e)
        })
    }

    // 设置状态
    function setStatus(row) {
      console.log(row)
      setRoleStatus({ id: row.data.id, status: row.value })
        .then((res) => {
          if (res.code === 0) {
            handleSearch()
            $message.success(res.msg)
          } else {
            $message.error(res.msg)
          }
        })
        .catch((e) => {
          console.log(e)
        })
    }

    return {
      ...toRefs(data),
      tableRef,
      loadDataTable,
      handleSearch,
      handleModal,
      handleCheck,
      selectedIds,
      deleteSingle,
      deleteComplex,
      deleteData,
      setStatus
    }
  }
}
</script>
<style scoped lang='scss'>
.n-button + .n-button {
  margin-left: 10px;
}
</style>
