<!--
 * @Description: 考核等级管理列表页面
 * @Author: 李亚光
 * @Date: 2023-09-14
 -->
<script lang="ts" setup name="RuleGradeList">
import { reactive, ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import { delGrade, exportGrade, getListPage } from '@/api/home/rule/grade'
import { exportFile } from '@/utils/exportUtils'
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  offset: 1,
  limit: 20,
  levelDesc: '', // 等级评定描述
  levelName: '', // 等级评定名称
})
const columns = ref([
  {
    text: '等级评定名称',
    value: 'levelName',
    align: 'center',
  },
  {
    text: '等评定描述',
    value: 'levelDesc',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  getListPage(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 重置
const reset = () => {
  listQuery.offset = 1
  listQuery.limit = 20
  listQuery.levelDesc = ''
  listQuery.levelName = ''
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
const $router = useRouter()
// 新建编辑操作
const handler = (row: any, type: string) => {
  $router.push({
    path: `/gradelist/${type}`,
    query: {
      row: JSON.stringify(row),
      id: row.id,
    },
  })
}
// 删除
const delHandler = (row: any) => {
  ElMessageBox.confirm(
    `确定删除${row.levelName}吗？一经删除，不可恢复！`,
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    delGrade({ id: row.id }).then((res) => {
      ElMessage.success('操作成功')
      search()
    })
  })
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
// 导出列表
const exportList = () => {
  if (list.value.length) {
    const loading = ElLoading.service({
      lock: true,
      text: 'Loading',
      background: 'rgba(255, 255, 255, 0.8)',
    })
    const data = {
      ...listQuery,
      offset: undefined,
      limit: undefined,
      // ids: selectList.value.map(item => item.id),
    }
    exportGrade(data).then((res) => {
      exportFile(res.data, '考核等级评定')
      loading.close()
    })
      .catch((_) => {
        loading.close()
      })
  }
  else {
    ElMessage.warning('无可导出内容')
  }
}
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.levelName" placeholder="等级评定名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.levelDesc" placeholder="等级评定描述" clearable />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <el-button v-if="proxy.hasPerm('/grade/add')" type="primary" @click="handler({}, 'create')">
          新增
        </el-button>
        <el-button v-if="proxy.hasPerm('/grade/export')" type="primary" @click="exportList">
          导出
        </el-button>
      </template>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="false" :is-multi="true" @change="changePage" @multi-select="multiSelect"
      >
        <template #columns>
          <el-table-column label="等级评定规则" width="760" align="center" style="padding: 0;">
            <template #default="scope">
              <div v-for="(item, index) in scope.row.evaluationRules" :key="item.id" class="row" :class="`${index === 0 ? '' : 'border-top'}`">
                <div class="name">
                  {{ item.levelCodeName }}
                </div>
                <div class="value">
                  {{ item.remarks }}
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center">
            <template #default="scope">
              {{ scope.row.remarks }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="140" align="center">
            <template #default="scope">
              <el-button v-if="proxy.hasPerm('/grade/update')" link type="primary" size="small" @click="handler(scope.row, 'update')">
                编辑
              </el-button>
              <el-button v-if="proxy.hasPerm('/grade/delete')" link type="danger" size="small" @click="delHandler(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

<style lang="scss" scoped>
.border-top {
  border-top: 1px solid #ebeef5;
}

.row {
  // display: flex;
  text-align: center;
  margin: 5px 0;

  &::after {
    content: "";
    height: 0;
    display: block;
    clear: both;
    zoom: 1;
  }

  .name {
    width: 25%;
    height: 100%;
    float: left;
    vertical-align: middle;
    // position: relative;
    // left: 50%;
    // top: 50%;
    // transform: translate(-50%, -50%);
  }

  .value {
    float: right;
    width: 75%;
    border-left: 1px solid #ebeef5;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
  }
}
</style>

