<template>
	<div class="system-config-advice-container layout-pd">
    <el-card shadow="never">
      <el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent>
        <el-form-item label="意见类型" prop="CommonType">
          <el-select v-model="state.queryParams.CommonType" placeholder="请选择意见类型">
            <el-option v-for="item in commonType" :value="item.key" :key="item.key" :label="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="常用语分类" prop="isOpen">
          <el-select v-model="state.queryParams.isOpen" placeholder="请选择意见类型">
            <el-option v-for="item in openState" :value="item.key" :key="item.key" :label="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryList" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
          <el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button"> <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="onAdviceAdd" v-waves v-auth="'system:config:advice:add'">
					<SvgIcon name="ele-Plus" class="mr5" />新增
				</el-button>
        <el-button type="primary" @click="onAdviceDelete" v-waves v-auth="'system:config:advice: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="content" label="意见内容" show-overflow-tooltip width="400"></el-table-column>
				<el-table-column prop="commonTypeText" label="意见类型" show-overflow-tooltip width="130"></el-table-column>
        <el-table-column prop="isOpen" label="常用意见分类" show-overflow-tooltip width="170">
          <template #default="{ row }">
            {{row.isOpen ? '公开常用意见' : '个人常用意见'}}
          </template>
        </el-table-column>
				<el-table-column prop="creatorName" label="创建人" show-overflow-tooltip></el-table-column>
        <el-table-column prop="creatorOrgName" label="创建部门" show-overflow-tooltip></el-table-column>
				<el-table-column prop="afterBegin" label="创建时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="120" fixed="right" align="center">
					<template #default="{ row }">
						<el-button link type="primary" @click="updateAdvice(row)" v-auth="'system:config:advice:edit'" title="修改参数"> 修改 </el-button>
						<!--              <el-button link type="danger" @click="parameterDelete(row)" v-auth="'system:timeLimit:detail'" title="删除参数"> 删除 </el-button>-->
					</template>
				</el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
			<!-- 分页 -->
			<pagination
				:total="state.total"
				v-model:page="state.queryParams.PageIndex"
				v-model:limit="state.queryParams.PageSize"
				@pagination="queryList"
			/>
		</el-card>
		<!--  常用意见新增  -->
		<advice-add ref="adviceAddRef" @updateList="queryList" :commonType="commonType"/>
		<!--  常用意见编辑  -->
		<advice-edit ref="adviceEditRef" @updateList="queryList" :commonType="commonType"/>
	</div>
</template>

<script lang="ts" setup name="systemConfigAdvice">
import { reactive, ref, onMounted, defineAsyncComponent } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { throttle } from '/@/utils/tools';
import { formatDate } from '/@/utils/formatTime';
import {auth} from "/@/utils/authFunction";
import {commonBaseData,publicCommonList,deleteCommon} from '/@/api/system/commonAdvice';

// 引入组件
const AdviceAdd = defineAsyncComponent(() => import('/@/views/system/config/advice/component/Advice-add.vue')); // 常用意见新增
const AdviceEdit = defineAsyncComponent(() => import('/@/views/system/config/advice/component/Advice-edit.vue')); // 常用意见编辑

// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
    CommonType: '', // 常用意见类型
    isOpen: null, // 是否公开
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<RefType>(null); // 表单ref
const commonType = ref<EmptyArrayType>([]); // 常用意见类型
const openState = ref<EmptyArrayType>([
  {
    key: true,
    value: '公开常用意见',
  },
  {
    key: false,
    value: '个人常用意见',
  },
]); // 是否公开
const getBaseData = async ()=>{
  try {
    const res = await commonBaseData();
    commonType.value = res.result?.commonType ?? [];
  } catch (error) {
    console.log(error);
  }
}
// 获取参数列表
const queryList = () => {
	state.loading = true;
  if (!auth('system:config:advice:query')) ElMessage.error('抱歉，您没有权限获取公开意见列表！');
  else {
    publicCommonList(state.queryParams)
        .then((res) => {
          state.loading = false;
          state.tableData = res.result.items ?? [];
          state.total = res.result.total ?? 0;
        })
        .finally(() => {
          state.loading = false;
        });
  }
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 新增意见
const adviceAddRef = ref<RefType>(); // 意见新增
const onAdviceAdd = () => {
	adviceAddRef.value.openDialog();
};
// 修改意见
const adviceEditRef = ref<RefType>(); // 修改意见
const updateAdvice = (row: any) => {
	adviceEditRef.value.openDialog(row);
};
// 表格多选
const multipleTableRef = ref<RefType>();
const multipleSelection = ref<any>([]);
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
};
// 删除参数
const onAdviceDelete = () => {
  const contents = multipleSelection.value.map((item: any) => item.content).join('、');
  const ids = multipleSelection.value.map((item: any) => item.id);
	ElMessageBox.confirm(`您确定要删除：【${contents}】的常用意见，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			deleteCommon({ids}).then(() => {
        ElMessage.success('操作成功');
        queryList();
			});
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
  getBaseData();
	queryList();
});
</script>

<style lang="scss" scoped>
.system-config-advice-container {

}
</style>
