<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="内容形式" prop="contentType">
        <el-select v-model="queryParams.contentType" placeholder="请选择内容形式" clearable>
          <el-option label="文章" value="article"></el-option>
          <el-option label="视频" value="video"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="内容形式" prop="contentType">
        <el-select v-model="queryParams.contentType" placeholder="请选择内容分类" clearable>
          <el-option v-for="dict in dict.type.knowledge_content_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="内容分类" prop="contentCategory">
        <el-select v-model="queryParams.contentCategory" placeholder="请选择内容分类" clearable>
          <el-option label="农技知识" value="tech"></el-option>
          <el-option label="政策解读" value="policy"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="内容分类" prop="contentCategory">
        <el-select v-model="queryParams.contentCategory" placeholder="请选择内容分类" clearable>
          <el-option v-for="dict in dict.type.knowledge_content_category" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="发布状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择发布状态" clearable>
          <el-option label="已发布" :value="1"></el-option>
          <el-option label="已下架" :value="0"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="发布状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择发布状态" clearable>
          <el-option v-for="dict in dict.type.knowledge_content_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 统计面板 -->
    <div class="statistics-panel">
      <el-row :gutter="12">
        <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4">
          <div class="stat-card total-card">
            <div class="stat-content">
              <div class="stat-title">内容总数</div>
              <div class="stat-number">{{ statistics.totalCount || 0 }}</div>
              <div class="stat-desc">累计内容数量</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4">
          <div class="stat-card tech-card">
            <div class="stat-content">
              <div class="stat-title">农技知识</div>
              <div class="stat-number">{{ statistics.techCount || 0 }}</div>
              <div class="stat-desc">农业技术内容</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4">
          <div class="stat-card policy-card">
            <div class="stat-content">
              <div class="stat-title">政策解读</div>
              <div class="stat-number">{{ statistics.policyCount || 0 }}</div>
              <div class="stat-desc">政策相关内容</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4">
          <div class="stat-card published-card">
            <div class="stat-content">
              <div class="stat-title">已发布</div>
              <div class="stat-number">{{ statistics.publishedCount || 0 }}</div>
              <div class="stat-desc">正在展示中</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4">
          <div class="stat-card draft-card">
            <div class="stat-content">
              <div class="stat-title">已下架</div>
              <div class="stat-number">{{ statistics.draftCount || 0 }}</div>
              <div class="stat-desc">待重新发布</div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:knowledgeContent:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:knowledgeContent:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:knowledgeContent:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:knowledgeContent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="knowledgeContentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="80" type="index">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <!-- <el-table-column label="内容形式" align="center" prop="contentType">
        <template slot-scope="scope">
          <span>{{ scope.row.contentType === 'article' ? '文章' : scope.row.contentType === 'video' ? '视频' : scope.row.contentType }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="内容形式" align="center" prop="contentType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.knowledge_content_type" :value="scope.row.contentType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="内容分类" align="center" prop="contentCategory">
        <template slot-scope="scope">
          <span>{{ scope.row.contentCategory === 'tech' ? '农技知识' : scope.row.contentCategory === 'policy' ? '政策解读' : scope.row.contentCategory }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="内容分类" align="center" prop="contentCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.knowledge_content_category" :value="scope.row.contentCategory" />
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="source" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column label="阅读量" align="center" prop="viewCount" />
      <el-table-column label="发布时间" align="center" prop="publishDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="发布状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status === 1 ? '已发布' : scope.row.status === 0 ? '已下架' : scope.row.status }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="发布状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.knowledge_content_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status !== 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:knowledgeContent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            :icon="scope.row.status === 1 ? 'el-icon-bottom' : 'el-icon-top'"
            @click="handleToggleStatus(scope.row)"
            v-hasPermi="['base:knowledgeContent:edit']"
          >{{ scope.row.status === 1 ? '下架' : '发布' }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:knowledgeContent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农技知识管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="内容分类" prop="contentCategory">
              <el-select v-model="form.contentCategory" placeholder="请选择内容分类" style="width: 100%">
                <el-option label="农技知识" value="tech"></el-option>
                <el-option label="政策解读" value="policy"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="内容形式" prop="contentType">
              <el-select v-model="form.contentType" placeholder="请选择内容形式" style="width: 100%" @change="handleContentTypeChange">
                <el-option label="文章" value="article"></el-option>
                <el-option label="视频" value="video"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="来源" prop="source">
              <el-input v-model="form.source" placeholder="请输入来源" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="作者" prop="author">
              <el-input v-model="form.author" placeholder="请输入作者" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布时间" prop="publishDate">
              <el-date-picker
                v-model="form.publishDate"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择发布时间"
                style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="封面图片" prop="imageUrl">
          <image-upload v-model="form.imageUrl" :limit="1" />
        </el-form-item>
        
        <el-form-item v-if="form.contentType === 'video'" label="视频上传" prop="videoUrl">
          <file-upload v-model="form.videoUrl" :fileType="['mp4', 'avi', 'mov', 'wmv']" :fileSize="500" :limit="1" />
          <div style="color: #999; font-size: 12px; margin-top: 5px;">支持格式：MP4、AVI、MOV、WMV，大小不超过500MB</div>
        </el-form-item>
        
        <el-form-item v-if="form.contentType === 'article'" label="文章内容" prop="articleContent">
          <div class="editor-container">
            <editor v-model="form.articleContent" :min-height="300" />
          </div>
        </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">发布</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKnowledgeContent, getKnowledgeContent, delKnowledgeContent, addKnowledgeContent, updateKnowledgeContent } from "@/api/base/knowledgeContent"

export default {
  name: "KnowledgeContent",
  dicts: ['knowledge_content_type', 'knowledge_content_category', 'knowledge_content_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农技知识管理表格数据
      knowledgeContentList: [],
      // 统计数据
      statistics: {
        totalCount: 0,
        techCount: 0,
        policyCount: 0,
        publishedCount: 0,
        draftCount: 0
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        description: null,
        contentType: null,
        contentCategory: null,
        imageUrl: null,
        source: null,
        viewCount: null,
        videoUrl: null,
        duration: null,
        articleContent: null,
        author: null,
        tags: null,
        publishDate: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        contentType: [
          { required: true, message: "内容形式不能为空", trigger: "change" }
        ],
        contentCategory: [
          { required: true, message: "内容分类不能为空", trigger: "change" }
        ],
        source: [
          { required: true, message: "来源不能为空", trigger: "blur" }
        ],
        author: [
          { required: true, message: "作者不能为空", trigger: "blur" }
        ],
        imageUrl: [
          { required: false, message: "封面图片不能为空", trigger: "change" }
        ],
        articleContent: [
          { required: true, message: "文章内容不能为空", trigger: "blur" }
        ],
        publishDate: [
          { required: true, message: "发布时间不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getStatistics()
  },
  methods: {
    /** 查询农技知识管理列表 */
    getList() {
      this.loading = true
      listKnowledgeContent(this.queryParams).then(response => {
        this.knowledgeContentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 获取统计数据 */
    getStatistics() {
      // 这里可以调用专门的统计API，目前先用列表数据计算
      listKnowledgeContent({}).then(response => {
        const data = response.rows || []
        
        this.statistics = {
          totalCount: data.length,
          techCount: data.filter(item => item.contentCategory === 'tech').length,
          policyCount: data.filter(item => item.contentCategory === 'policy').length,
          publishedCount: data.filter(item => item.status === 1).length,
          draftCount: data.filter(item => item.status === 0).length
        }
      }).catch(() => {
        this.statistics = {
          totalCount: 0,
          techCount: 0,
          policyCount: 0,
          publishedCount: 0,
          draftCount: 0
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        description: null,
        contentType: 'article', // 默认选中文章
        contentCategory: null,
        imageUrl: null,
        source: null,
        viewCount: null,
        videoUrl: null,
        duration: null,
        articleContent: null,
        author: null,
        tags: null,
        publishDate: null,
        createTime: null,
        updateTime: null,
        status: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加农技知识管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getKnowledgeContent(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改农技知识管理"
      })
    },
    /** 内容形式变化处理 */
    handleContentTypeChange(value) {
      // 切换到文章时清空视频相关字段
      if (value === 'article') {
        this.form.videoUrl = null
        this.form.duration = null
      }
      // 切换到视频时清空文章内容
      else if (value === 'video') {
        this.form.articleContent = null
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 设置发布状态为已发布
          this.form.status = 1
          
          if (this.form.id != null) {
            updateKnowledgeContent(this.form).then(response => {
              this.$modal.msgSuccess("发布成功")
              this.open = false
              this.getList()
              this.getStatistics()
            })
          } else {
            addKnowledgeContent(this.form).then(response => {
              this.$modal.msgSuccess("发布成功")
              this.open = false
              this.getList()
              this.getStatistics()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除农技知识管理编号为"' + ids + '"的数据项？').then(function() {
        return delKnowledgeContent(ids)
      }).then(() => {
        this.getList()
        this.getStatistics()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/knowledgeContent/export', {
        ...this.queryParams
      }, `knowledgeContent_${new Date().getTime()}.xlsx`)
    },
    /** 发布/下架状态切换 */
    handleToggleStatus(row) {
      const statusText = row.status === 1 ? '下架' : '发布'
      const newStatus = row.status === 1 ? 0 : 1
      this.$modal.confirm('确认要' + statusText + '该农技知识吗？').then(() => {
        // 先获取完整的数据，确保富文本内容不丢失
        return getKnowledgeContent(row.id)
      }).then(response => {
        // 使用完整数据更新状态
        return updateKnowledgeContent({ ...response.data, status: newStatus })
      }).then(() => {
        this.getList()
        this.getStatistics()
        this.$modal.msgSuccess(statusText + "成功")
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.article-content-cell {
  max-width: 180px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.5;
}

.editor-container {
  max-height: 400px;
  overflow-y: auto;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}

.editor-container::-webkit-scrollbar {
  width: 6px;
}

.editor-container::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.editor-container::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.editor-container::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 统计面板样式 */
.statistics-panel {
  margin-bottom: 16px;
  padding: 20px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.statistics-panel .el-row {
  display: flex;
  justify-content: space-between;
}

.statistics-panel .el-col {
  flex: 1;
  max-width: none !important;
}

/* 统一各部分间距 */
.app-container .el-form {
  margin-bottom: 16px;
}

.app-container .mb8 {
  margin-bottom: 16px !important;
}

.app-container .el-table {
  margin-top: 0;
}

.stat-card {
  border-radius: 12px;
  padding: 16px;
  height: 120px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transition: all 0.3s ease;
  cursor: pointer;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1), 0 2px 6px rgba(0, 0, 0, 0.05);
  position: relative;
  overflow: hidden;
  color: #fff;
  margin-bottom: 10px;
}

.stat-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15), 0 4px 10px rgba(0, 0, 0, 0.1);
}

.stat-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
}

.stat-title {
  font-size: 13px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.9);
  margin-bottom: 0;
  line-height: 1.2;
}

.stat-number {
  font-size: 32px;
  font-weight: 700;
  line-height: 1.0;
  margin: 6px 0;
  color: #fff;
}

.stat-desc {
  font-size: 11px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.85);
  line-height: 1.2;
  margin: 0;
}

/* 不同卡片的主题色 */
.total-card {
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
}

.tech-card {
  background: linear-gradient(135deg, #10b981 0%, #34d399 100%);
}

.policy-card {
  background: linear-gradient(135deg, #f59e0b 0%, #fbbf24 100%);
}

.published-card {
  background: linear-gradient(135deg, #ec4899 0%, #f472b6 100%);
}

.draft-card {
  background: linear-gradient(135deg, #06b6d4 0%, #38bdf8 100%);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .stat-card {
    padding: 14px;
    height: 110px;
  }
  
  .stat-title {
    font-size: 12px;
  }
  
  .stat-number {
    font-size: 28px;
    margin: 4px 0;
  }
  
  .stat-desc {
    font-size: 10px;
  }
}

@media (max-width: 992px) {
  .stat-card {
    padding: 16px;
    height: 120px;
  }
  
  .stat-title {
    font-size: 13px;
  }
  
  .stat-number {
    font-size: 30px;
  }
  
  .stat-desc {
    font-size: 11px;
  }
}

@media (max-width: 768px) {
  .stat-card {
    padding: 20px;
    height: 140px;
  }
  
  .stat-title {
    font-size: 14px;
  }
  
  .stat-number {
    font-size: 36px;
    margin: 8px 0;
  }
  
  .stat-desc {
    font-size: 12px;
  }
}
</style>
