<template>
  <div class="tels-evaluationKey-container layout-pd">
    <el-card shadow="never">
      <!-- 通用搜索 -->
      <el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt15">
        <el-form-item label="评价名称" prop="satisName">
          <el-input v-model="state.queryParams.satisName" placeholder="请输入评价名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="评价值" prop="satisVal">
          <el-input v-model="state.queryParams.satisVal" placeholder="评价值" clearable @keyup.enter="handleQuery" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery" :loading="state.loading" v-waves> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
          <el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button" :loading="state.loading">
            <SvgIcon name="ele-Refresh" class="mr5" />重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never">
      <div class="mb20">
        <el-button type="primary" @click="onAddWhitelist" v-auth="'tels:evaluationKey:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
        <el-button type="primary" @click="onRemove" v-auth="'tels:evaluationKey:delete'" :disabled="!multipleSelection.length">
          <SvgIcon name="ele-Delete" class="mr5" />删除
        </el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="state.tableData" v-loading="state.loading" row-key="id" ref="multipleTableRef" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="satisName" label="评价名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="satisVal" label="评价值" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="70" fixed="right" align="center">
          <template #default="{ row }">
            <el-button type="primary" link @click="onEdit(row)" v-auth="'tels:evaluationKey:edit'">编辑</el-button>
          </template>
        </el-table-column>
        <template #empty>
          <Empty />
        </template>
      </el-table>
    </el-card>
    <!--  新增编辑按键  -->
    <evaluation-key-add ref="evaluationKeyAddRef" @updateList="handleQuery" />
  </div>
</template>

<script lang="ts" setup name="evaluationKey">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import type { FormInstance } from 'element-plus';
import { auth } from '/@/utils/authFunction';
import { satisPaged, satisRemove } from '/@/api/public/wex';
import { throttle } from '/@/utils/tools';

// 引入组件
const evaluationKeyAdd = defineAsyncComponent(() => import('/@/views/tels/evaluationKey/component/EvaluationKey-add.vue')); // 新增白名单

// 定义变量内容
const state = reactive<any>({
  queryParams: {
    // 查询参数
    satisName: '', // 评价名称
    satisVal: '', // 评价值
  },
  loading: false, // 加载状态
  tableData: [],  // 表格数据
  multipleSelection: [],  // 多选数据
});
const ruleFormRef = ref<FormInstance>();  // 表单ref
/** 搜索按钮操作 节流操作 */
const handleQuery = throttle(() => {
  queryList();
}, 300);
/** 获取白名单列表 */
const queryList = () => {
  if (!auth('tels:evaluationKey:query')) ElMessage.error('抱歉，您没有权限获评价按键列表！');
  else {
    state.loading = true;
    satisPaged(state.queryParams)
        .then((response: any) => {
          state.tableData = response.data ?? [];
          state.loading = false;
        })
        .catch(() => {
          state.loading = false;
        });
  }
};
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  handleQuery();
}, 300);
// 打开新增评价按键弹窗
const evaluationKeyAddRef = ref<RefType>();
const onAddWhitelist = () => {
  evaluationKeyAddRef.value.openDialog();
};
// 编辑评价按键
const onEdit = (row: any) => {
  evaluationKeyAddRef.value.openDialog(row);
};
// 表格多选
const multipleTableRef = ref<RefType>();
const multipleSelection = ref<any>([]);
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
};
// 删除评价按键
const onRemove = () => {
  const ids:string = multipleSelection.value.map((item: any) => item.id).join(',');
  const phoneNo:string = multipleSelection.value.map((item: any) => item.satisName).join(',');
  ElMessageBox.confirm(`此操作将删除：【${phoneNo}】的评价按键，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    draggable: true,
    cancelButtonClass: 'default-button',
    autofocus: false,
  })
      .then(() => {
        satisRemove(ids).then(() => {
          ElMessage.success('操作成功');
          queryList();
        });
      })
      .catch(() => {});
};
onMounted(() => {
  queryList();
});
</script>
