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

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

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

/**
 * @description: 获取数据及操作
 * @param {*} row 单行数据
 * @param {*} type 操作类型 create:创建，preview:预览，edit:编辑
 * @return {*}
 */
function getRowData(row, type) {
  data.rowData = row
  data.modalType = type
  data.modalShow = true
}

/**
 * @description: 打开分配权限窗口
 * @param {*} row 单行数据
 * @return {*}
 */
function configure(row) {
  data.rowData = row
  data.configModalShow = true
}

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

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

const data = reactive({
  tableRef,
  searchParams,
  rowData: {},
  modalType: 'create',
  modalShow: false,
  configModalShow: false,
  handleSearch,
  deleteData,
  columns: [
    { type: 'selection' },
    {
      title: '角色编号',
      key: 'code',
      align: 'center'
    },
    {
      title: '角色名称',
      key: 'name',
      align: 'center'
    },
    {
      title: '创建时间',
      key: 'createTime',
      align: 'center',
      Minwidth: 160
    },
    {
      title: '更新时间',
      key: 'updateTime',
      align: 'center',
      Minwidth: 160
    },
    {
      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: handleStatusChange.bind(row)
        })
      }
    },
    {
      title: '操作',
      align: 'center',
      width: 150,
      fixed: 'right',
      render(row) {
        return h(TableAction, {
          actions: [
            {
              label: '修改',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: getRowData.bind(null, row, 'update')
              },
              auth: 'basic_list'
            },
            {
              label: '分配权限',
              type: 'button',
              props: {
                type: 'primary',
                ghost: true,
                text: true,
                onClick: configure.bind(null, row)
              },
              auth: 'basic_list'
            },
            {
              label: '删除',
              type: 'popconfirm',
              auth: 'basic_list',
              tip: '确定删除这条数据吗?',
              props: {
                onPositiveClick: deleteData.bind(null, [row.id])
              },
              ButtonProps: {
                text: true,
                type: 'primary'
              }
            }
          ],
          align: 'center'
        })
      }
    }
  ]
})

export default data
