import { h, ref, reactive } from 'vue'
import TableImage from '@/components/DataTable/tools/Image.vue'
import TableTags from '@/components/DataTable/tools/Tags.vue'
import TableSwitch from '@/components/DataTable/tools/Switch.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { resetPassword, deleteUser, setUserStatus } from '@/api/system/user/index.js'

/* 注册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
}

// 设置状态
function setStatus(row) {
  setUserStatus({ 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)
  })
}

/**
 * @description: 重置密码
 * @param {Number} id 选中数据id
 * @return {*}
 */
function handlePasswordReset(id) {
  resetPassword({ id })
    .then(res => {
      if (res.code === 0) {
        handleSearch()
      }
    }).catch(e => {
      console.log(e)
    })
}

/**
 * @description: 删除用户接口
 * @param {Array} ids 用户id集合
 * @return {*}
 */
function deleteData(ids) {
  deleteUser(ids).then((res) => {
    if (res.code === 0) {
      handleSearch()
    }
  }).catch(e => {
    console.log(e)
  })
}

const data = reactive({
  tableRef,
  searchParams,
  rowData: {},
  modalType: 'create',
  modalShow: false,
  handleSearch,
  deleteData,
  columns: [
    {
      type: 'selection'
    },
    {
      title: '用户编号',
      key: 'code',
      align: 'center'
    },
    {
      title: '头像',
      key: 'avatar',
      align: 'center',
      render(row) {
        return h(TableImage, {
          images: {
            width: 36,
            height: 36,
            src: row.avatar
          }
        })
      }
    },
    {
      title: '用户账号',
      key: 'username',
      align: 'center'
    },
    {
      title: '用户姓名',
      key: 'realname',
      align: 'center'
    },
    {
      title: '角色',
      key: 'roleList',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.roles,
          rowKey: 'name'
        })
      }
    },
    {
      title: '部门',
      key: 'deptName',
      align: 'center'
    },
    {
      title: '创建时间',
      key: 'createTime',
      align: 'center',
      width: 160
    },
    {
      title: '更新时间',
      key: 'updateTime',
      align: 'center',
      width: 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: setStatus.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: 'popconfirm',
              tip: '确定要重置为123456吗?',
              props: {
                onPositiveClick: handlePasswordReset.bind(null, row.id)
              },
              ButtonProps: {
                text: true,
                type: 'primary'
              },
              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
