<template>
  <div>
    <n-card>
      <headSearch :info="info" @search="handleSearch" />
      <data-table
        ref="tableRef"
        :columns="columns"
        :row-key="(row) => row.id"
        :request="loadDataTable"
        size="large"
        @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>
  <!-- 新增、编辑弹窗 -->
  <user-modal
    v-if="modalShow"
    v-model:visible="modalShow"
    :row="rowData"
    @done="handleSearch"
  />
</template>

<script>
import headSearch from '@/components/Search/index.vue'
import dataTable from '@/components/DataTable/index.vue'
import TableSwitch from '@/components/DataTable/tools/Switch.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { getUserList, resetPassword, deleteUser, setUserStatus } from '@/api/system/user/index.js'
import { h, unref, ref, toRefs, reactive } from 'vue'
import UserModal from './components/UserModal.vue'
import info from './info.js'
import table from './table.js'
export default {
  name: 'MenuPage',
  components: { dataTable, UserModal, headSearch },
  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: 'popconfirm',
                  auth: 'basic_list',
                  tip: '确定要重置为123456吗?',
                  props: {
                    negativeText: '取消',
                    positiveText: '确认',
                    onPositiveClick: resetPsw.bind(null, row.id)
                  }
                },
                {
                  label: '删除',
                  type: 'popconfirm',
                  auth: 'basic_list',
                  tip: '确定删除这条数据吗?',
                  props: {
                    negativeText: '取消',
                    positiveText: '确认',
                    onPositiveClick: deleteSingle.bind(null, row.id)
                  }
                }
              ],
              align: 'center'
            })
          }
        }
      ],
      info: ref(info),
      modalShow: false,
      rowData: {}
    })
    // 打开编辑弹窗
    function play(row) {
      data.rowData = row
      data.modalShow = 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 getUserList(_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) {
      deleteUser(data).then((res) => {
        if (res.code === 0) {
          handleSearch()
          $message.success(res.msg)
        } else {
          $message.error(res.msg)
        }
      }).catch(e => {
        console.log(e)
      })
    }

    // 重置密码
    function resetPsw(id) {
      resetPassword({ id }).then(res => {
        if (res.code === 0) {
          handleSearch()
          $message.success(res.msg)
        } else {
          $message.error(res.msg)
        }
      }).catch(e => {
        console.log(e)
      })
    }

    // 设置状态
    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)
      })
    }

    return {
      loadDataTable,
      handleModal,
      ...toRefs(data),
      tableRef,
      handleSearch,
      selectedIds,
      deleteComplex,
      handleCheck,
      deleteData,
      resetPsw,
      setStatus
    }
  },
  methods: {

  }
}
</script>
<style scoped lang='scss'>
.n-button + .n-button {
  margin-left: 10px;
}
</style>
