<template>
<keep-alive>
  <div class="app-container">
    <span><i class="el-icon-user"></i> 个人发表 - {{nickName}}</span>
    <el-divider></el-divider>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="内容" prop="contents">
        <el-input
          v-model="queryParams.contents"
          placeholder="请输入内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发表时间">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:publish:remove']"
        >删除</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="publishList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="发表内容" align="center" prop="contents" :formatter="contentsFormat" width="450"/>
      <el-table-column label="发布时间" align="center" prop="createTime" width="160"/>
      <el-table-column
        label="纪念留言"
        align="center"
        prop="message"
        :formatter="messageFormat"
        width="100"
      />
      <el-table-column label="点赞数" align="center" prop="thumbs" />
      <el-table-column label="评论数" align="center" prop="comments" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:publish:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:publish: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  :visible.sync="open" width="600px" append-to-body :close-on-click-modal=false>
      <el-card>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-card shadow="hover">
          <div style="line-height:150%">{{form.contents}}</div>
        <el-divider />

        <div>
          <div style="float: left;padding:0px 0px 10px;">
            <el-tag type="info"> 点赞数：{{form.thumbs}} </el-tag>
          </div>
          <div v-if="form.message == 'Y'" style="float: right;font-size: 10px;color:#909399;">
            <div style="padding:1px;">留 念 人：{{form.messageUser}}</div>
            <div style="padding:2px;">发布时间：{{form.createTime}}</div>
          </div>
        </div>
        </el-card>

        <div class="block1">
          <el-collapse>
            <el-collapse-item>
              <template slot="title">
                <i class="header-icon el-icon-picture-outline-round"></i> 查看图片
              </template>
              <div v-for="item in imgList" :key="item.key" class="block">
                <el-image :key="item.key" :src="item.value" :preview-src-list="[item.value]" :fit="'cover'" class="images" />
              </div>
            </el-collapse-item>
          </el-collapse>
        </div>

      </el-form>
        <el-card shadow="hover" :body-style="{height:'200px'}">
          <div slot="header" class="clearfix">
            <span>评论</span>
            <div style="float: right; padding: 3px 0">{{form.comments}}</div>
          </div>
          
          <el-carousel :autoplay="false" :loop="false" arrow="always">
            <el-carousel-item v-for="item in commentList" :key="item.id">

              <el-row :gutter="10" type="flex" justify="start">
                <el-col :span="3" style="padding: 10px;margin:0 auto;text-align:center;">
                    <el-avatar :size="40" :src="baseUrl+item.appUserUrl"></el-avatar>
                    <div style="width: 100%;margin:0 auto;text-align:center;">
                      <span>{{item.appUser}}</span>
                    </div>
                </el-col>
                <el-col :span="21" style="padding: 10px">
                  <div style="float: right;">
                    <el-button type="danger" size="mini" icon="el-icon-delete" circle @click="delComm(item)"/>
                  </div> 
                  <div style="float: top;font-size:11px;color:#909399;padding:0px 0px 10px">
                    {{item.createTime}}
                  </div>
                  <div style="float: bottom;font-size:12px;height:120px">
                    {{item.contents}}
                  </div>
                </el-col>
              </el-row>

            </el-carousel-item>
          </el-carousel>
        </el-card>

      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" type="primary">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</keep-alive>
</template>

<script>
import { listPublish, getPublish, delPublish, addPublish, updatePublish, exportPublish,listComment,delComment } from "@/api/system/publish";
import { getUser } from "@/api/system/appUser";

export default {
  name: "Publish",
  data() {
    return {
      baseUrl: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      pid: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 日期范围
      dateRange: [],
      // 总条数
      total: 0,
      // 我的近况表格数据
      publishList: [],
      imgList:[],
      messageOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      nickName: "",
      commentList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appUserId: null,
        contents: null,
        message: null,
        messageUserId: null,
        deleted: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  activated() {
    this.init();
  },
  created(){
    this.init();
  },
  methods: {
    init(){
      this.baseUrl = process.env.VUE_APP_BASE_API;
      const uid = this.$route.query.uid;
      this.queryParams.appUserId = uid;

      this.getDicts("sys_yes_no").then(response => {
        this.messageOptions = response.data;
      });

      this.getUser(uid);
      this.getList(); 
    },
      getUser(id){
        getUser(id).then(response => {
            const nickName = response.data.nickName;
            this.nickName = nickName == null ? "全部" : nickName;
        });
      },
    /** 查询我的近况（发）列表 */
    getList() {
      this.loading = true;
      this.queryParams.deleted = 'N'
      listPublish(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.publishList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否纪念留言 字典翻译
    messageFormat(row, column) {
      return this.selectDictLabel(this.messageOptions, row.message);
    },
    contentsFormat(row, column) {
      var len = 28;
      if(row.contents.length >= len) {
        return row.contents.substr(0,len)+"...";
      }
      return row.contents;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        appUserId: null,
        contents: null,
        message: null,
        messageUserId: null,
        thumbs: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        imgList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      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;

      getPublish(id).then(response => {
        this.form = response.data;
        this.title = "查看我的发表";

        this.imgList = [];
        var apiUrl = process.env.VUE_APP_BASE_API;
        var items = response.data.imgList;
        for (const i in items) {
            var url = apiUrl + items[i].url;
            this.imgList.push({ key: items[i].id, value: url})
        }
        this.listComment(id);
      });
      this.$nextTick(function(){
        this.open = true;
      });
    },
    listComment(id) {
        listComment(id).then(res => {
          this.commentList = res.rows;
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePublish(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addPublish(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPublish(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有我的发表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPublish(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    delComm(item) {
      var id = item.id;
      this.$confirm('是否删除这条评论?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delComment(id);
      }).then(() => {
        this.listComment(item.publishId);
        this.form.comments -= 1;
        this.msgSuccess("删除成功");
      }).catch(function() {});
    }
  }
};
</script>

<style>
.el-dialog__body {
  padding: 10px 20px ! important;
}
/* .el-form-item {
  margin-bottom: 8px;
} */
.block {
    padding: 1px 1px 0px;
    display: inline-block;
    align-items: center;
}
.block1 {
    width: 100%;
    padding: 10px 0px 10px;
}
 .images {
    width: 140px;
    height: 140px;
    display: block;
  }
.image-slot {
    display: flex ! important;
    justify-content: center ! important;
    align-items: center ! important;
    width: 100% ! important;
    height: 100% ! important;
    background: #f5f7fa ! important;
    color: #909399 ! important;
}
.el-divider--horizontal {
  margin: 10px 0px ! important;
}
.el-card__body {
  padding: 12px ! important;
}
.el-carousel__item {
  height: 200px ! important;
}
</style>