<template>
  <div class="back-form-container">
    <div>
    <el-form :model="query" label-width="80px" :inline="true">
      
      
      
      
      
      
      
        <el-form-item label="文章名称" prop="articleTitle">
          <el-input v-model="query.articleTitle" placeholder="请输入文章名称"/>
        </el-form-item>
      
      
      
      
      
      
      
      
      
      
      
      
        <el-form-item label="状态" prop="state">
          <el-select v-model="query.state" placeholder="请选择状态">
            <el-option
              v-for="item in store.state.dicts.get('sys_state')"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
        <el-form-item label="发表时间" prop="publishTime">      <DatePicker v-model:startDate=query.publishTimeStart v-model:endDate=query.publishTimeEnd/>
        </el-form-item>
      
      
      
      
      
      
        <el-form-item label="浏览量" prop="eyeFill">
          <InputNumberRange v-model:min=query.eyeFillStart v-model:max=query.eyeFillEnd/>
        </el-form-item>
      
      
      
      
      
      
        <el-form-item label="点赞量" prop="likeCount">
          <InputNumberRange v-model:min=query.likeCountStart v-model:max=query.likeCountEnd/>
        </el-form-item>
      
      
      
      
      
      
      
      
      
      
      
      
        <el-form-item label="创建时间" prop="createTime">      <el-date-picker v-model="query.createTime" type="datetime" placeholder="请选择时间" value-format="YYYY-MM-DD hh:mm:ss"/>
        </el-form-item>
      
      
      
      
      
      
      
      
      
      
      
      
        <el-form-item label="更新时间" prop="updateTime">      <el-date-picker v-model="query.updateTime" type="datetime" placeholder="请选择时间" value-format="YYYY-MM-DD hh:mm:ss"/>
        </el-form-item>
      
      
      
      
      
      
      
      
    </el-form>
    </div>
    <div class="back-table-handler">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain size="small" @click="getList(pageNum)">
            <el-icon>
              <RefreshLeft/>
            </el-icon>
            <span>刷新</span>
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain size="small" @click="openDialog('save', Article.Create())">
            <el-icon>
              <Plus/>
            </el-icon>
            <span>新增</span>
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain size="small" @click="deleteByIds()">
            <el-icon>
              <Delete/>
            </el-icon>
            <span>删除</span>
          </el-button>
        </el-col>
      </el-row>
    </div>

    <div class="back-table">
      <el-table v-loading="listLoading" :data="list" @selection-change="selected" border :key="ListKey">
        <el-table-column type="selection" width="40" align="center"/>
        
        <el-table-column label="id" align="center" prop="id"/>
        <el-table-column label="文章名称" align="center" prop="articleTitle"/>
        <el-table-column label="文章内容" align="center" prop="articleContent"/>
      <el-table-column label="状态" align="center" prop="state">
        <template #default="scope">
          <DictTag :dict-type="'sys_state'" :dict-value="scope.row.state"/>
        </template>
      </el-table-column>
        <el-table-column label="文章标签" align="center" prop="articleTagIds"/>
        <el-table-column label="文章主题id" align="center" prop="articleTopicId"/>
      <el-table-column label="图片" align="center" prop="image">
        <template #default="scope">
          <el-image  :src="scope.row.image"></el-image>
        </template>
      </el-table-column>
        <el-table-column label="发表时间" align="center" prop="publishTime"/>
        <el-table-column label="浏览量" align="center" prop="eyeFill"/>
        <el-table-column label="点赞量" align="center" prop="likeCount"/>
        <el-table-column label="创建人" align="center" prop="createBy"/>
        <el-table-column label="创建时间" align="center" prop="createTime"/>
        <el-table-column label="更新人" align="center" prop="updateBy"/>
        <el-table-column label="更新时间" align="center" prop="updateTime"/>
        <el-table-column label="文章简介" align="center" prop="articleDesc"/>
        <el-table-column label="操作" align="center" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="openDialog('update',scope.row)">修改</el-button>
            <el-button link type="danger" @click="deleteById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
            background
            :current-page="pageNum"
            @current-change="getList"
            :page-size="pageSize"
            :page-sizes="[10, 20, 30, 50,100]"
            :total="total"
            @size-change="SetPageSized"
            layout="total, sizes, prev, pager, next"
        />
      </div>

        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="dialogType=='update'?'更改字典类型':'新增字典类型'" v-model="open" width="500px" append-to-body>
          <el-form ref="dictRef" :model="dialogForm" label-width="80px">
            
        <el-form-item label="文章名称" prop="articleTitle">
          <el-input v-model="dialogForm.articleTitle" placeholder="请输入文章名称"/>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleContent">              <EditorComponent v-model:htmlString="dialogForm.articleContent"/>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="dialogForm.state" placeholder="请选择状态">
            <el-option
              v-for="item in store.state.dicts.get('sys_state')"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="文章标签" prop="articleTagIds">                <el-text class="mx-1">{{dialogForm.articleTagIds}}</el-text>
        </el-form-item>
        <el-form-item label="文章主题id" prop="articleTopicId">                <el-text class="mx-1">{{dialogForm.articleTopicId}}</el-text>
        </el-form-item>
        <el-form-item label="图片" prop="image">              <ImageUpload v-model:ImgUrl="dialogForm.image"/>
        </el-form-item>
        <el-form-item label="发表时间" prop="publishTime">      <el-date-picker v-model="dialogForm.publishTime" type="datetime" placeholder="请选择时间" value-format="YYYY-MM-DD hh:mm:ss"/>
        </el-form-item>
        <el-form-item label="浏览量" prop="eyeFill">
          <el-input-number v-model="dialogForm.eyeFill" placeholder="请输入浏览量"/>
        </el-form-item>
        <el-form-item label="点赞量" prop="likeCount">
          <el-input-number v-model="dialogForm.likeCount" placeholder="请输入点赞量"/>
        </el-form-item>
        <el-form-item label="创建人" prop="createBy">                <el-text class="mx-1">{{dialogForm.createBy}}</el-text>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">      <el-date-picker v-model="dialogForm.createTime" type="datetime" placeholder="请选择时间" value-format="YYYY-MM-DD hh:mm:ss"/>
        </el-form-item>
        <el-form-item label="更新人" prop="updateBy">                <el-text class="mx-1">{{dialogForm.updateBy}}</el-text>
        </el-form-item>
        <el-form-item label="更新时间" prop="updateTime">      <el-date-picker v-model="dialogForm.updateTime" type="datetime" placeholder="请选择时间" value-format="YYYY-MM-DD hh:mm:ss"/>
        </el-form-item>
        <el-form-item label="文章简介" prop="articleDesc">                <el-text class="mx-1">{{dialogForm.articleDesc}}</el-text>
        </el-form-item>
          </el-form>
          <template #footer>
            <div class="dialog-footer">
              <el-button type="primary" @click="submitForm()">确 定</el-button>
              <el-button @click="open=false">取 消</el-button>
            </div>
          </template>
        </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref} from "vue";
import {
  DeleteBaseArticleById,
  GetBaseArticleListByArticle,
  SaveBaseArticle,
  UpdateBaseArticle,
  Article,
  ArticleQuery
} from "../../../api/back/BaseBackApi.ts";
import {ElMessage} from "element-plus";
import {Delete, Plus, RefreshLeft} from "@element-plus/icons-vue";
import router from "../../../router";
import {store} from "../../../state/TokenState.ts";
import DictTag from "../../../components/dict/DictTag.vue";
import MultipleTag from "../../../components/MultipleTag/MultipleTag.vue";
import ImageUpload from "../../../components/ImageUpload/ImageUpload.vue";

let listLoading = ref(true)
let total = ref(0)
let pageNum = ref(1)
let pageSize = ref(10)
let list = ref<Article[]>([])
let ListKey = ref(0)
//搜索用
//let search = ref<Article>(Article.Create())
//修改，添加用
let open = ref(false)
let dialogType = ref("")//save添加，update修改
let dialogForm = ref<Article>(null)
let query = ref<ArticleQuery>(ArticleQuery.Create())
let selectItems = ref<Article[]>([])
const selected = (arr: Article[]) => {
  selectItems.value = arr
}

getList()
const SetPageSized = (val: number) => {
  pageSize.value = val
  getList(pageNum.value)
}

function openDialog(type: string, row: Article) {
  open.value = true
  dialogType.value = type
  if (type==="save"){
    dialogForm.value = Article.Create()
    return
  }
  //深拷贝，不会影响数据
  dialogForm.value = JSON.parse(JSON.stringify(row))
}

function deleteByIds() {
  for (let i = 0; i < selectItems.value.length; i++) {
      deleteById(selectItems[i].id)
  }
}

function deleteById(id) {
  DeleteBaseArticleById(id).then(response => {
    if (response.code == 200) {
      ElMessage.success("删除成功,id:" + id)
      getList()
    } else {
      ElMessage.error("删除失败,id:" + id)
    }
  })
}

function getList(pn = 1) {
  pageNum.value = pn
  GetBaseArticleListByArticle(query.value, pageNum.value, pageSize.value).then(response => {
    let data = response.data
    total.value = data.total
    list.value = response.data.list
    ListKey.value++
    listLoading.value = false
  })
}

function submitForm() {
  switch (dialogType.value) {
    case "update":
      UpdateBaseArticle(dialogForm.value, dialogForm.value.id).then(response => {
        if (response.code == 200) {
          open.value = false
          getList()
          ElMessage.success("修改成功")
        } else {
          ElMessage.error("修改失败")
        }
      })
      break
    case "save":
      SaveBaseArticle(dialogForm.value).then(response => {
        if (response.code == 200) {
          open.value = false
          getList()
          ElMessage.success("添加成功")
        } else {
          ElMessage.error("添加失败")
        }
      })
      break
    default:
      break
  }
}
</script>
