<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </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>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
        >导出</el-button>
      </el-col>-->
    </el-row>
    <br>

    <el-table v-loading="loading" :data="announcementList">
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="置顶标识" align="center" prop="isTop">
        <template #default="scope">
          <el-tag v-if="scope.row.isTop === 1" type="danger">置顶</el-tag>
          <el-tag v-else type="info">普通</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 1" type="success">显示</el-tag>
          <el-tag v-else type="warning">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-if="scope.row.isTop === 0"
            @click="toggleTop(scope.row, 1)"
            icon="Top"
          >设为置顶</el-button>
          <el-button
            link
            type="primary"
            v-if="scope.row.isTop === 1"
            @click="toggleTop(scope.row, 0)"
            icon="Bottom"
          >取消置顶</el-button>
          <el-button
            link
            type="primary"
            v-if="scope.row.status === 1"
            @click="toggleStatus(scope.row, 0)"
            icon="Hide"
          >隐藏</el-button>
          <el-button
            link
            type="primary"
            v-if="scope.row.status === 0"
            @click="toggleStatus(scope.row, 1)"
            icon="View"
          >显示</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryParams.pageSize"
        layout="->, total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" v-model="open" width="650px" append-to-body :close-on-click-modal="false">
      <el-form ref="announcementRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <Editor 
          :value="form.content || ''" 
          @updateValue="updateContent" 
          filePath="announcementContentImg" />
        </el-form-item>
        <!-- <el-form-item label="置顶标识" prop="isTop">
          <el-input v-model="form.isTop" placeholder="请输入置顶标识" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Announcement">
import { ref, reactive, toRefs, getCurrentInstance } from 'vue';
import { listAnnouncement, getAnnouncement, delAnnouncement, addAnnouncement, updateAnnouncement, setTopAnnouncement } from "@/api/announcement";
import Editor from "@/components/Editor.vue";

const { proxy } = getCurrentInstance();

const announcementList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");

const isAdd = ref(false)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    content: null,
    isTop: null,
    status: null,
    creator: null,
    updater: null
  },
  rules: {
    title: [
      { required: true, message: "标题不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "内容不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listAnnouncement(queryParams.value).then(response => {
    announcementList.value = response.list;
    total.value = response.total;
    loading.value = false;
  });
}

// 改变每页条数大小
function handleSizeChange(val) {
  queryParams.value.pageSize = val;
  getList();
}

// 换页
function handleCurrentChange(val) {
  queryParams.value.pageNum = val;
  getList();
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    content: null,
    isTop: null,
    status: null,
    createTime: null,
    creator: null,
    updateTime: null,
    updater: null
  };
  const announcementRef = proxy.$refs["announcementRef"]
  if (announcementRef) {
    announcementRef.resetFields();
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  const queryRef = proxy.$refs["queryRef"]
  if (queryRef) {
    queryRef.resetFields();
  }
  handleQuery();
}


/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加公告";
  isAdd.value = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getAnnouncement(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改公告";
    isAdd.value = false;
  });
}

/** 表单提交按钮 */
function submitForm() {
  proxy.$refs["announcementRef"].validate(valid => {
    if (valid) {
      if (isAdd.value) {  // 添加
        addAnnouncement(form.value).then(response => {
          proxy.$message({
            message: response.msg,
            type: 'success'
          })
          open.value = false;
          getList();
        });
      } else {  // 更新
        updateAnnouncement(form.value).then(response => {
          proxy.$message({
            message: response.msg,
            type: 'success'
          })
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$confirm('是否确认删除公告编号为"' + row.id + '"的数据项？').then(function() {
    return delAnnouncement(row.id);
  }).then(response => {
    getList();
    proxy.$message({
      message: response.msg,
      type: 'success'
    })
  }).catch(() => {});
}

function toggleTop(row, nextVal) {
  const text = nextVal === 1 ? '是否要将该公告置顶？' : '是否要取消该公告的置顶？';
  proxy.$confirm(text).then(function() {
    if (nextVal === 1) {
      return setTopAnnouncement(row.id)
    } else {
      return updateAnnouncement({ id: row.id, isTop: nextVal })
    }
  }).then(response => {
    getList();
    proxy.$message({
      message: response.msg,
      type: 'success'
    })
  }).catch(() => {});
}

function toggleStatus(row, nextVal) {
  const text = nextVal === 1 ? '是否要显示该公告？' : '是否要隐藏该公告？';
  proxy.$confirm(text).then(function() {
    return updateAnnouncement({ id: row.id, status: nextVal });
  }).then(response => {
    getList();
    proxy.$message({
      message: response.msg,
      type: 'success'
    })
  }).catch(() => {});
}

/** 导出按钮操作 */
// function handleExport() {
//   proxy.download('system/announcement/export', {
//     ...queryParams.value
//   }, `announcement_${new Date().getTime()}.xlsx`)
// }
function updateContent(value) {
  form.value.content = value;
}

getList();
</script>

