<template>
  <div class="fullHeight">
    <div class="searchBox whiteBlock">
      <el-button @click="addBtn" size="medium" type="primary">添加 </el-button>
    </div>
    <div class="table_wrap whiteBlock">
      <vue-scroll>
        <el-table class="tables" :data="workerList">
          <el-table-column
            align="center"
            prop="title"
            label="标题"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="createTime"
            label="新增时间"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="updateTime"
            label="修改时间"
          ></el-table-column>
          <el-table-column width="200">
            <template slot-scope="scope">
              <div class="tableBtns">
                <div class="operationText" @click="editBtn(scope.row)">
                  <img
                    src="@/assets/images/icon-edit.png"
                    width="15px"
                    height="15px"
                  />
                  <span>编辑</span>
                </div>
                <div class="operationText" @click="deleteBtn(scope.row)">
                  <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"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="$store.state.PAGESIZRS"
          :page-size="size"
          layout="total, sizes, prev, pager, next"
          :total="Number(total)"
        ></el-pagination>
      </vue-scroll>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      :modal-append-to-body="false"
      :title="type"
      :visible.sync="dialogVisible"
      width="667px"
    >
      <div class="dialog_content">
        <el-form
          label-width="120px"
          size="medium"
          ref="addEditForm"
          :model="workerInfo"
        >
          <el-form-item
            label="标题"
            prop="title"
            :rules="[
              { required: true, message: '请输入标题', trigger: 'blur' }
            ]"
          >
            <el-input
              placeholder="请输入"
              v-model="workerInfo.title"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="内容"
            prop="content"
            :rules="[
              { required: true, message: '请输入播报内容', trigger: 'blur' }
            ]"
          >
            <!-- 失去焦点时手动校验该字段 (当前 articleBody 属性值)
         <quill-editor  
            @blur="$refs.addEditForm.validateField('content')"
            v-model="workerInfo.content"/> -->
            <quill-editor
              v-model="workerInfo.content"
              ref="myQuillEditor"
              :options="editorOption"
              @blur="onEditorBlur($event)"
              @focus="onEditorFocus($event)"
              @change="onEditorChange($event)"
            >
            </quill-editor>
          </el-form-item>

          <div class="dialog-footer">
            <el-button
              class="cancleBtn"
              @click="dialogVisible = false"
              icon="el-icon-circle-close"
              size="medium"
              >取消
            </el-button>
            <el-button
              @click="saveUav"
              type="primary"
              icon="el-icon-circle-check"
              size="medium"
              >确定
            </el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
  <script>
// import { quillEditor } from "vue-quill-editor";
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 {
  addInformationBroadcastingApi, //新增
  deleteInformationBroadcastingApi, //删除
  updateInformationBroadcastingApi, //修改
  selectPageInformationBroadcastingApi //分页查询
} from '@/assets/js/api/broadcast.js'
export default {
  components: {
    VueQuillEditor
  },
  data() {
    return {
      type: '',
      workerList: [], //表格数据
      dialogVisible: false,
      current: 1, //当前页
      size: 10, //每页数量
      total: 0,
      workerInfo: {
        title: '', //标题
        content: '', //内容
        id: ''
      }, //表单
      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'
      }
    }
  },
  computed: {
    editor() {
      return this.$refs.myQuillEditor.quill
    }
  },
  created() {
    this.projectSn = this.$store.state.projectSn

    this.getWorkerList()
  },
  methods: {
    onEditorReady(editor) {
      // 准备编辑器
    },
    onEditorBlur() {
      // 失去焦点事件
    },
    onEditorFocus() {
      // 获得焦点事件
    },
    onEditorChange() {
      // 内容改变事件
    },

    //分页查询
    getWorkerList() {
      let data = {
        projectSn: this.$store.state.projectSn,
        current: this.current,
        size: this.size
      }
      selectPageInformationBroadcastingApi(data).then((res) => {
        console.log('信息播报列表', res)
        this.workerList = res.result.records
        this.total = res.result.total
      })
    },
    //添加
    addBtn() {
      this.type = '新增播报'
      this.dialogVisible = true
      this.workerInfo = {
        title: '', //标题
        content: '' //内容
      }
    },
    //编辑
    editBtn(val) {
      this.type = '编辑播报'
      this.dialogVisible = true
      this.workerInfo = JSON.parse(JSON.stringify(val))
      this.workerInfo.id = val.id
    },
    //删除
    deleteBtn(val) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteInformationBroadcastingApi({ id: val.id }).then((res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getWorkerList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //添加或编辑提交
    saveUav() {
      let data = {
        projectSn: this.$store.state.projectSn,
        title: this.workerInfo.title,
        content: this.workerInfo.content,
        id: this.workerInfo.id
      }
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.type == '新增播报') {
            addInformationBroadcastingApi(data).then((res) => {
              this.$message.success(res.message)
              this.dialogVisible = false
              this.workerInfo = {
                title: '', //标题
                content: '' //内容
              }
              this.getWorkerList()
            })
          } else if (this.type == '编辑播报') {
            updateInformationBroadcastingApi(data).then((res) => {
              this.$message.success(res.message)
              this.dialogVisible = false
              this.workerInfo = {
                title: '', //标题
                content: '' //内容
              }
              this.getWorkerList()
            })
          }
        }
      })
    },

    //查看条数
    handleSizeChange(val) {
      this.current = val
      this.size = 10
      this.getWorkerList()
    },
    //查看页
    handleCurrentChange(val) {
      this.current = val
      this.size = 10
      this.getWorkerList()
    }
  }
}
</script>
  <style lang="less" scoped>
.videoUpload {
  width: 320px;

  > div {
    margin-left: 10px;
  }
  video {
    margin-top: 10px;
    width: 200px;
    height: 200px;
  }
}

::v-deep .ql-editor {
  // 样式穿透

  min-height: 180px !important;
}
</style>