<template>
  <div class="fullHeight whiteBlock">
    <div style="padding: 15px 15px 0">
      <!-- 党员关怀 -->
      <el-form
        :inline="true"
        ref="searchForm"
        :model="searchForm"
        size="medium"
      >
        <el-form-item label="标题">
          <el-input
            v-model="searchForm.title"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="getList">{{
            $t('message.energyManage.waybill.query')
          }}</el-button>
          <el-button type="warning" plain @click="refresh">{{
            $t('message.deviceManage.refresh')
          }}</el-button>
          <el-button type="primary" size="medium" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table_wrap whiteBlock">
      <el-table class="tables" :data="List" >
        <el-table-column
        type="index"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="image"
          align="center"
          label="图片"
        >
        <template slot-scope="scope">
            <img
              :preview="
                scope.row.image[0]
                  ? scope.row.image[0].url
                  : []
              "
              :src="
                scope.row.image[0]
                  ? scope.row.image[0].url
                  : []
              "
              alt=""
              width="50px"
            />
          </template>
      </el-table-column>
        <el-table-column
          prop="title"
          align="center"
          label="标题"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          align="center"
          label="时间"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <div class="tableBtns" style="margin-left: 110px !important">
              <div class="operationText" @click.stop="showDetailFn(scope.row)">
                <i
                  class="el-icon-tickets"
                  style="color: #8dacfa; font-size: 16px; margin-right: 2px"
                ></i>
                <span>详情</span>
              </div>
              <div @click.stop="edit(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-edit.png"
                  width="15px"
                  height="15px"
                />
                <span>编辑</span>
              </div>
              <div @click.stop="deleteDev(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-delete.png"
                  width="15px"
                  height="15px"
                />
                <span>删除</span>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagerBox"
        @size-change="SizeChange"
        @current-change="CurrentChange"
        :current-page="pagInfo.pageNo"
        :page-sizes="$store.state.PAGESIZRS"
        :page-size="pagInfo.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="Number(pagInfo.total)"
        background
      ></el-pagination>
    </div>
    <!-- 新增 -->
    <el-dialog
      :modal-append-to-body="false"
      :title="title"
      :visible.sync="dialogShow"
      @close="close"
      width="1200px"
    >
      <div class="dialog_content">
        <el-form
          size="medium"
          :model="addEditForm"
          ref="addEditForm"
          :rules="addEditRules"
          label-width="80px"
          :inline="true"
        >
          <el-form-item label="标题" prop="title">
            <el-input
              v-model="addEditForm.title"
              style="width: 1000px"
              placeholder="请输入标题"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="内容"
            prop="content"
            style="line-height: 140px"
          >
            <quill-editor
              class="ql-editor"
              style="width: 1025px; margin-left: -15px; margin-top: -20px"
              v-model="addEditForm.content"
              ref="myQuillEditor"
              :options="editorOption"
              @blur="onEditorBlur($event)"
              @focus="onEditorFocus($event)"
              @change="onEditorChange($event)"
            >
            </quill-editor>
          </el-form-item>
          <el-form-item label="图片" style="line-height: 140px">
            <el-upload
              :action="$store.state.UPLOADURL"
              list-type="picture-card"
              multiple
              name="files"
              :limit="1"
              accept=".png, .jpg, .jpeg"
              :file-list="fileUplodList"
              :on-success="(res, file) => handleSuccess(res, file)"
              :on-remove="(file, fileList) => handleRemove(file, fileList)"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div class="dialog-footer">
          <el-button
            class="cancleBtn"
            @click="dialogShow = false"
            icon="el-icon-circle-close"
            size="medium"
            >取消
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-circle-check"
            @click="submit"
            size="medium"
            >确认
          </el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog
      :modal-append-to-body="false"
      title="详情"
      :visible.sync="dialogContent"
      width="1200px"
    >
      <div style="padding: 10px 80px">
        <h1 style="text-align: center">{{ detailData.title }}</h1>
        <div class="detailHeader">
          <span>{{ detailData.createTime }}</span>
        </div>
        <div class="detailContent">
          <div
            class="ql-editor"
            style="white-space: pre-wrap"
            v-html="detailData.content"
          ></div>
        </div>
        <div class="dialog-footer">
          <el-button
            type="primary"
            icon="el-icon-circle-check"
            @click="dialogContent = false"
            size="medium"
            >确认
          </el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import {
  addPartyMemberActivityApi,
  deletePartyMemberActivityApi,
  editPartyMemberActivityApi,
  getPartyMemberActivityPageApi
} from '@/assets/js/api/materialManagement.js'
export default {
  components: {
    VueQuillEditor
  },
  mounted() {
    this.getList()
  },
  data() {
    return {
      pagInfo: {
        pageNo: 1, //页数
        pageSize: 10, //条数
        total: 0 //总条数
      },
      detailData: {},
      dialogContent: false,
      current: -1,
      title: "",
      dialogShow: false,
      List: [],
      addEditForm: {
        title: '',
        content: '',
        image: '',
      },
      addEditRules: {
        title:[
        {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
        ],
        content:[
        {
            required: true,
            message: '必填',
            trigger: 'blur'
          },
        ]
      },
      searchForm: {
        title: '',

      },
      fileUplodList: [],
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], //加粗，斜体，下划线，删除线
            ['blockquote', 'code-block'], //引用，代码块

            [{ header: 1 }, { header: 2 }], // 标题，键值对的形式；1、2表示字体大小
            [{ list: 'ordered' }, { list: 'bullet' }], //列表
            [{ script: 'sub' }, { script: 'super' }], // 上下标
            [{ indent: '-1' }, { indent: '+1' }], // 缩进
            [{ direction: 'rtl' }], // 文本方向

            [{ size: ['small', false, 'large', 'huge'] }], // 字体大小
            [{ header: [1, 2, 3, 4, 5, 6, false] }], //几级标题

            [{ color: [] }, { background: [] }], // 字体颜色，字体背景颜色
            [{ font: [] }], //字体
            [{ align: [] }], //对齐方式

            ['clean'], //清除字体样式
            ['image'] //上传图片、上传视频
          ]
        },
        theme: 'snow'
      },
    }
  },
  methods: {
    onEditorBlur() {
      // 失去焦点事件
    },
    onEditorFocus() {
      // 获得焦点事件
    },
    onEditorChange() {
      this.$refs.addEditForm.clearValidate()

      // 内容改变事件
    },
    //显示检查详细弹框
    showDetailFn(row) {

      this.detailData = row
      this.dialogContent = true
    },

    // 删除文件
    handleRemove(file, fileList) {
      this.fileUplodList = fileList
    },
    handleSuccess(res, file, type) {
      if (res.status == 'SUCCESS') {

        this.fileUplodList.push({
          name: file.name,
          url: this.$store.state.FILEURL + file.response.data[0].imageUrl
        })
      }
    },
    //查询数据
    getList() {
      getPartyMemberActivityPageApi({
        projectSn: this.$store.state.projectSn,
        title: this.searchForm.title,
        pageNo: this.pagInfo.pageNo,
        pageSize: this.pagInfo.pageSize,
        type:3
      }).then((result) => {
        if (result.success) {
          
          result.result.records.map((item) => {
            if (item.image.includes("[")) {
              item.image = JSON.parse(item.image);
              if (item.image.length !== 0 && !item.image[0].url.includes(this.$store.state.FILEURL)) {
                item.image[0].url =
                  this.$store.state.FILEURL + item.image[0].url;
              }
            } else {
              let url = item.image

              if (url.length !== 0 && !url.includes(this.$store.state.FILEURL)) {
                item.image = [{}]
                item.image[0].url =
                  this.$store.state.FILEURL + url;
              }
            }

          })
          this.List = result.result.records
          this.pagInfo.total = result.result.total
          console.log(' this.List', this.List);
        }
      })
    },

    add() {
      this.title = '新增'
      this.dialogShow = true
      this.fileUplodList = []
      this.close()
    },

    edit(obj) {
      this.title = '编辑'
      this.dialogShow = true
      this.addEditForm = JSON.parse(JSON.stringify(obj))
      this.fileUplodList = obj.image
    },
    submit() {
      if(this.fileUplodList.length==0){
        this.$message.error('请上传图片')
        return false
      }
      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      params.type=3
      params.image = JSON.stringify(this.fileUplodList)
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.title == '新增') {
            addPartyMemberActivityApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          } else if (this.title == '编辑') {
            editPartyMemberActivityApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          }
          this.dialogShow = false
        } else {
          return false
        }
      })
    },
    deleteDev(obj) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deletePartyMemberActivityApi({ id: obj.id }).then((res) => {
            if (res.success) {
              this.getList()
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              this.$message({
                type: "error",
                message: res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    close() {
      this.fileUplodList = []
      this.addEditForm = {}
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    SizeChange(val) {
      this.pagInfo.pageSize = val
      this.getList()
    },
    CurrentChange(val) {
      this.pagInfo.pageNo = val
      this.getList()
    },
    refresh() {
      this.searchForm = {}
         this.pagInfo.pageNo = 1 //页数
      this.pagInfo.pageSize = 10 //条数
      this.getList()
    },
  }
}
</script>
<style lang="less" scoped>
.detailHeader {
  text-align: center;
  padding: 20px;
  span {
    color: #ccc;
    font-size: 12px;
    margin-right: 20px;
  }
}
</style>