<template>
  <div class="p-2">
    <!-- <el-card shadow="hover" class="mb-[10px]"> -->
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item label="模板名称" prop="phone">
            <el-input v-model="queryParams.name" placeholder="请输入模板名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="类型" prop="phone">
            <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
              <el-option v-for="item in getStaList" :label="item.label" :value="item.value" :key="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="phone">
            <el-select v-model="queryParams.status" placeholder="请选择类型" clearable>
              <el-option v-for="item in signList" :label="item.label" :value="item.value" :key="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>
    <!-- </el-card> -->
    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="showRefuteDialog = true">创建模板</el-button>
          </el-col>
        </el-row>
      </template>
      <el-table
        ref="categoryTableRef"
        :data="messList"
        row-key="categoryId"
        border
        :default-expand-all="isExpandAll"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="id" prop="id" align="center" width="180" />
        <el-table-column label="模板名称" prop="name" align="center" width="150" />
        <el-table-column label="模版类型" prop="type" align="center" width="150">
          <template #default="{ row }">
            {{ row.type == 1 ? '通知' : '公告' }}
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" align="center" width="150">
          <template #default="{ row }">
            {{ row.status == 0 ? '正常' : '关闭' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="查看" placement="top">
              <el-button link type="primary" v-hasPermi="['workflow:video:search']" @click="seachfun(scope.row.id)">查看</el-button>
            </el-tooltip>
            <el-tooltip content="编辑" placement="top">
              <el-button link type="primary" v-hasPermi="['workflow:video:edit']" @click="updatefun(scope.row.id)">编辑</el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" v-hasPermi="['workflow:video:remove']" @click="deltemplatefun(scope.row.id)">删除</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <pagination v-if="total > 0" :total="total" v-model:page="queryParams.current" v-model:limit="queryParams.size" @pagination="getlist" />
      </div>
    </el-card>
    <el-dialog v-model="showRefuteDialog" title="创建消息模板">
      <el-form :model="dataparams" :rules="rules">
        <el-form-item label="模板类型">
          <el-select v-model="dataparams.type" placeholder="请选择类型">
            <el-option v-for="item in getStaList" :label="item.label" :value="item.value" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板场景">
          <el-select v-model="dataparams.action" placeholder="请选择场景">
            <el-option v-for="item in getAction" :label="item.label" :value="item.value" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板名称">
          <el-input v-model="dataparams.name" placeholder="模板名称" />
        </el-form-item>
        <el-form-item label="模板参数">
          <el-input v-model="dataparams.params" placeholder="模板参数" />
        </el-form-item>
        <el-form-item label="模板标题">
          <el-input v-model="dataparams.title" placeholder="模板标题" />
        </el-form-item>
        <el-form-item label="模板描述">
          <el-input v-model="dataparams.description" placeholder="模板描述" type="textarea" :autosize="{ minRows: 4, maxRows: 8 }" />
        </el-form-item>
        <el-form-item label="模板内容">
          <el-input v-model="dataparams.content" placeholder="模板内容" type="textarea" :autosize="{ minRows: 4, maxRows: 8 }" />
        </el-form-item>
        <el-form-item label="扩展字段">
          <el-input v-model="dataparams.ext" placeholder="扩展字段" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="cilcikgAdd">创建</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog v-model="seachookDialog" title="查看消息模板">
      <el-form :model="seachook" :rules="rules">
        <el-form-item label="模板类型">
          <el-select v-model="seachook.type" placeholder="请选择类型" disabled>
            <el-option v-for="item in getStaList" :label="item.label" :value="item.value" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板场景">
          <el-select v-model="seachook.action" placeholder="请选择场景" disabled>
            <el-option v-for="item in getAction" :label="item.label" :value="item.value" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板名称">
          <el-input v-model="seachook.name" placeholder="模板名称" disabled />
        </el-form-item>
        <el-form-item label="模板参数">
          <el-input v-model="seachook.params" placeholder="模板参数" disabled />
        </el-form-item>
        <el-form-item label="模板标题">
          <el-input v-model="seachook.title" placeholder="模板标题" disabled />
        </el-form-item>
        <el-form-item label="模板描述">
          <el-input v-model="seachook.description" placeholder="模板描述" type="textarea" disabled :autosize="{ minRows: 4, maxRows: 8 }" />
        </el-form-item>
        <el-form-item label="模板内容">
          <el-input v-model="seachook.content" placeholder="模板内容" type="textarea" disabled :autosize="{ minRows: 4, maxRows: 8 }" />
        </el-form-item>
        <el-form-item label="扩展字段">
          <el-input v-model="seachook.ext" placeholder="扩展字段" disabled />
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog v-model="updatedialog" title="修改消息模板">
      <el-form :model="updatahook" :rules="rules">
        <el-form-item label="模板类型">
          <el-select v-model="updatahook.type" placeholder="请选择类型">
            <el-option v-for="item in getStaList" :label="item.label" :value="item.value" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板场景">
          <el-select v-model="updatahook.action" placeholder="请选择场景">
            <el-option v-for="item in getAction" :label="item.label" :value="item.value" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板名称">
          <el-input v-model="updatahook.name" placeholder="模板名称" />
        </el-form-item>
        <el-form-item label="模板参数">
          <el-input v-model="updatahook.params" placeholder="模板参数" />
        </el-form-item>
        <el-form-item label="模板标题">
          <el-input v-model="updatahook.title" placeholder="模板标题" />
        </el-form-item>
        <el-form-item label="模板描述">
          <el-input v-model="updatahook.description" placeholder="模板描述" type="textarea" :autosize="{ minRows: 4, maxRows: 8 }" />
        </el-form-item>
        <el-form-item label="模板内容">
          <el-input v-model="updatahook.content" placeholder="模板内容" type="textarea" :autosize="{ minRows: 4, maxRows: 8 }" />
        </el-form-item>
        <el-form-item label="扩展字段">
          <el-input v-model="updatahook.ext" placeholder="扩展字段" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="clickupdate">修改</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Leave" lang="ts">
import { getDictionaryByKey } from '@/utils/dict';
import { listtemplate, templateAdd, deltemplate, getnotice, updateCategory } from '@/api/set/messageindex';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const showSearch = ref(true);
const queryFormRef = ref<ElFormInstance>();
import { LeaveForm, AccountQuery, AccountOV, paramsaccountOV, seachookOV } from '@/api/set/messagetype';
const data = reactive<PageData<LeaveForm, AccountQuery>>({
  form: {},
  queryParams: {
    current: 1,
    size: 10,
    name: undefined,
    templatecode: undefined,
    type: undefined,
    status: undefined
  },
  rules: {}
});
const isExpandAll = ref(true);
const { queryParams } = toRefs(data);
const messList = ref<AccountOV[]>([]);
const total = ref(0);
const showRefuteDialog = ref<boolean>(false);
const logmess = ref<boolean>(false);
const getlist = async () => {
  const res = await listtemplate(queryParams.value);
  if (res.code == 200) {
    messList.value = res.data.records;
    total.value = res.data.total;
  }
};
const deltemplatefun = async (row: string) => {
  ElMessageBox.confirm('确定要删除该模板吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = await deltemplate(row);
    if (res.code == 200) {
      ElMessage.success('删除成功');
      getlist();
    }
  });
};
const dataparams = ref<paramsaccountOV>({
  type: null,
  name: null,
  content: null,
  title: null,
  ext: null,
  description: null,
  action: null,
  params: null
});

const rules = reactive({
  templateType: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  name: [{ required: true, message: '请输入内容', trigger: 'blur' }],
  content: [{ required: true, message: '请输入内容', trigger: 'blur' }],
  title: [{ required: true, message: '请输入标题内容', trigger: 'blur' }]
});
const handleQuery = () => {
  getlist();
};
const resetQuery = () => {
  queryParams.value = {
    type: undefined,
    status: undefined,
    name: undefined,
    templatecode: undefined,
    current: 1,
    size: 10
  };
  getlist();
};
//查看
const seachookDialog = ref<boolean>(false);
const seachook = ref<seachookOV>({
  createBy: null,
  createTime: null,
  id: null,
  status: null,
  content: null,
  name: null,
  type: null,
  title: null,
  updateBy: null,
  updateTime: null,
  ext: null,
  description: null,
  action: null,
  params: null
});

const seachfun = async (id?: string) => {
  const res = await getnotice(id);
  if (res.code == 200) {
    seachook.value = res.data;
    seachookDialog.value = true;
  }
};
//编辑
const updatedialog = ref<boolean>(false);
const updatahook = ref<seachookOV>({
  createBy: null,
  createTime: null,
  id: null,
  status: null,
  content: null,
  name: null,
  type: null,
  title: null,
  updateBy: null,
  updateTime: null
});
const updatefun = async (id?: string) => {
  const res = await getnotice(id);
  if (res.code == 200) {
    updatahook.value = res.data;
    updatedialog.value = true;
  }
};
const clickupdate = async () => {
  const res = await updateCategory(updatahook.value);
  if (res.code == 200) {
    updatedialog.value = false;
    getlist();
  }
};
const signList = ref([]);
const getStaList = ref([]);
const getAction = ref([]);
const getByKey = async () => {
  const value1 = await getDictionaryByKey('sys_notice_status');
  const value2 = await getDictionaryByKey('sys_notice_type');
  const value3 = await getDictionaryByKey('im_message_action');
  signList.value = value1;
  getStaList.value = value2;
  getAction.value = value3;
};
//新增
const cilcikgAdd = async () => {
  const res = await templateAdd(dataparams.value);
  if (res.code == 200) {
    showRefuteDialog.value = false;
    dataparams.value = {
      type: null,
      name: null,
      content: null,
      title: null,
      ext: null,
      description: null,
      action: null,
      params: null
    };
    getlist();
  }
};
onMounted(() => {
  getByKey();
  getlist();
});
</script>
