import { h, ref, reactive } from 'vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import TableSwitch from '@/components/DataTable/tools/Switch.vue'
import { setRoleStatus, roleDelete } from '@/api/system/role/index.js'

/* 注册table */
const tableRef = ref()
const searchParams = ref()

function handleSearch(params) {
  searchParams.value = { ...params }
  tableRef.value.reFetch({ searchParams })
}

/**
 * @description: 改变状态
 * @param {*} row 选中数据
 * @return {*}
 */
function handleStatusChange(row) {
  console.log(row)
  setRoleStatus({ id: row.data.id, status: row.value })
    .then((res) => {
      if (res.code === 0) {
        handleSearch()
      }
    })
    .catch((e) => {
      console.log(e)
    })
}

function deleteData(id) {
  roleDelete(id)
    .then((res) => {
      if (res.code === 0) {
        handleSearch()
      }
    })
    .catch((e) => {
      console.log(e)
    })
}
/**
 * @description: 获取数据及操作
 * @param {*} row 单行数据
 * @param {*} type 操作类型 create:创建，preview:预览，edit:编辑
 * @return {*}
 */
function getRowData(row, type) {
  data.rowData = row || {}
  data.modalType = type
  if (type === 'relate') {
    data.relateShow = true
  } else {
    data.modalShow = true
  }
}

const data = reactive({
  tableRef,
  searchParams,
  rowData: {},
  modalType: 'create',
  modalShow: false,
  relateShow: false,
  handleSearch,

  columns: [
    {
      title: '编号',
      key: 'key',
      render: (_, index) => {
        return `${index + 1}`
      },
      align: 'center',
      width: 50
    },
    {
      title: '角色名称',
      key: 'roleName',
      align: 'center',
      width: 200
    },
    // {
    //   title: '角色编码',
    //   key: 'code',
    //   align: 'center',
    //   width: 200
    // },
    {
      title: '状态',
      key: 'status',
      align: 'center',
      width: 100,
      render(row) {
        return h(TableSwitch, {
          data: { id: row.id, status: row.status },
          rowKey: 'status',
          checkedValue: 1,
          uncheckedValue: 0,
          onChange: handleStatusChange.bind(row)
        })
      }
    },
    // {
    //   title: '描述',
    //   key: 'remark',
    //   align: 'center',
    //   ellipsis: {
    //     tooltip: true
    //   },
    //   width: 400
    // },
    {
      title: '操作',
      align: 'center',
      width: 200,
      fixed: 'right',
      render(row) {
        return h(TableAction, {
          actions: [
            {
              label: '详情',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: getRowData.bind(null, row, 'preview')
              },
              auth: 'system:role:preview'
            },
            {
              label: '编辑',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: getRowData.bind(null, row, 'update')
              },
              auth: 'system:role:edit'
            },
            {
              label: '分配权限',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: getRowData.bind(null, row, 'relate')
              },
              auth: 'system:role:dispatch'
            },
            {
              label: '删除',
              type: 'popconfirm',
              auth: 'system:role:delete',
              tip: '确定删除这个角色吗?',
              props: {
                onPositiveClick: deleteData.bind(null, [row.id])
              },
              ButtonProps: {
                text: true,
                type: 'primary'
              }
            }

          ],
          align: 'center'
        })
      }
    }
  ]
})

export default data
