<template>
  <div class="Announcement">
    <div class="AnnouncementMain">
      <div class="CRTop">
        <div>
          <span>办公OA / 公告管理</span>
        </div>
        <div style="text-align:right">
          <el-button type="primary" v-if="AnnouncementBtn.AnnouncementBtn132" round plain @click="addAnnouncement">发布公告</el-button>
        </div>
      </div>
      <el-row>
        <el-tabs
          class="BCtabs"
          v-model="activeName"
          type="border-card"
        >
          <p class="SearchTitle">
            总记录条数
            <span style="color:#379FFF">{{announcementListTotal}}</span>条
          </p>
          <div class="AnnouncementTable">
            <el-table
              ref="multipleTable"
              :data="announcementList"
              style="width: 100%"
              tooltip-effect="dark"
              :header-cell-style="{'background-color':'rgba(250,250,250,1)'}"
              border
              v-loading="announcementLoad"
            >
              <el-table-column prop="title" label="公告标题" min-width="100px" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column prop="content" label="公告内容" min-width="150px" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column label="图片" min-width="200px" :show-overflow-tooltip="true">
                <template slot-scope="props">
                  <div v-if="props.row.imgs" class="demo-image" @click="showImage(props.row.imgs[0].imgUrl)">
                    <el-image
                      style="width: 100px; height: 100px"
                      :src="props.row.imgs[0].imgUrl"
                      fit="fill"></el-image>
                  </div>
                  <!-- props.row.imgs[0].imgUrl</span> -->
                </template>
              </el-table-column>
              <el-table-column prop="username" label="发布人" min-width="100px" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column prop="postDate" label="发布时间" min-width="150px" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column
                label="操作"
                min-width="100px"
                fixed="right"
              >
                <template slot-scope="props">
                  <el-button
                    type="primary"
                    plain
                    round
                    @click="showAnnoucement(props.row)"
                  >查看</el-button>
                  <el-button
                    type="warning"
                    plain
                    round
                    v-if="AnnouncementBtn.AnnouncementBtn133"
                    @click="deleteAnnoucement(props.row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="pagenation">
              <span>
                <el-pagination
                  background
                  layout="prev, pager, next, sizes, total, jumper"
                  :total="announcementListTotal"
                  @current-change="click"
                  @size-change="handleSizeChange"
                  :page-sizes="[5, 10, 15, 20]"
                  :page-size="pagesize"
                ></el-pagination>
              </span>
            </div>
          </div>
        </el-tabs>
      </el-row>
      
    </div>

    <div class="Image">
      <el-dialog
        title="图片"
        :visible.sync="dialogImageUrl"
        width="40%"
        :before-close="handleClose"
      >
        <div class="block">
          <el-carousel height="350px">
            <el-carousel-item v-for="item in Image" :key="item.sort">
              <el-image :src="item.imgUrl" fit="fill"></el-image>
            </el-carousel-item>
          </el-carousel>
        </div>
        <div slot="footer" class="dialog-footer Txc">
          <el-button @click="dialogImageUrl = false">关 闭</el-button>
        </div>
      </el-dialog>
    </div>

<!--锁定项目-->
    <div class="deleteAnnouce">
      <el-dialog
        title="删除公告"
        :visible.sync="dialogdeleteAnnouce"
        width="30%"
        :before-close="handleClose"
      >
        <p>确定删除该公告吗？</p>
        <div slot="footer" class="dialog-footer Txc">
          <el-button type="primary" @click="submitdeleteAnnouce">确 定</el-button>
          <el-button @click="dialogdeleteAnnouce = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>


    <!-- dialog -->
    <el-dialog
      :title="IsAnnouncementdia?'发布公告':'查看公告'"
      :visible.sync="notice"
      width="500px"
      class="Announcementdia"
    >
      <!-- <div>
        <span>
          <img src="https://ty-picture.oss-cn-hangzhou.aliyuncs.com/tx123.png " alt />
        </span>
        <span></span>
      </div> -->
      <el-form
          :model="AnnouncementFrom"
          class="AnnouncementFrom"
          :inline="true"
          label-position="left"
          :rules="AnnouncementFromrule" ref="AnnouncementFrom"
        >
        <el-row class="mb10">
          <el-form-item label="公告标题：" prop="title">
            <el-input v-if="IsAnnouncementdia" placeholder="请填写公告标题" v-model="AnnouncementFrom.title"></el-input>
            <span v-else>{{AnnouncementFrom.title}}</span>
          </el-form-item>
        </el-row>
        <el-row class="mb10">
          <el-form-item label="公告内容：" prop="content">
            <el-input v-if="IsAnnouncementdia" placeholder="请填写公告内容" type="textarea" :rows="3" v-model="AnnouncementFrom.content"></el-input>
            <span v-else>{{AnnouncementFrom.content}}</span>
          </el-form-item>
        </el-row>
        <el-row class="mb10">
          <el-form-item label="上传图片："  >
            <el-upload v-if="IsAnnouncementdia"
            class="avatar-uploader"
            ref="fileList"
            action="https://app.edtyun.com:8443/business/common/upload"
            :headers="{'Authorization':token}"
            :show-file-list="false"
            :on-success="handleAvatarSuccess1"
            :on-remove="handleRemove1"
            >
              <img v-if="AnnouncementFrom.imgUrls" :src="AnnouncementFrom.imgUrls" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <img v-else :src="AnnouncementFrom.imgUrls?AnnouncementFrom.imgUrls[0].imgUrl:''" style="width: 200px;height: 178px;" />
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer Txc" v-if="IsAnnouncementdia">
        <el-button type="info" size="small" plain @click="notice=false">取消</el-button>
        <el-button type="primary" size="small" plain @click="subPicForm('AnnouncementFrom')">确定</el-button>
      </div>
    </el-dialog>
    <!--  -->
  </div>
</template>

<script>
export default {
  components: {},
  name: "Announcement",
  data() {
    return {
      announcementList: [],
      announcementLoad:false,
      announcementListTotal:10,
      dialogdeleteAnnouce:false,
      deleteAnnouceId:"",
      activeName: "first",
      pagesize: 10,
      currpage: 1,
      notice: false,
      dialogImageUrl: false,
      AnnouncementFrom:{
        title:'',
        content:'',
        imgUrls:'',
      },
      IsAnnouncementdia:true,
      AnnouncementFromrule:{
        title: [
          { required: true, message: "公告标题不能为空", trigger: "blur" },
        ],
        content:[{ required: true, message: "公告内容不能为空", trigger: "blur" },],
      },
      Image:[],
      dialogVisible: false,
      multiple: "",
      fileList:[],
      itemForm:{
        head_img: ''
      },
      fd: '',
      handleClose:null,
      token:'',
      AnnouncementBtn:{
        AnnouncementBtn132:0,
        AnnouncementBtn133:0,
      }
    };
  },
  created() {
    this.showAnnouncementList(1,10)
    this.token= JSON.parse(sessionStorage.getItem('token'))

    var AnnouncementBtn = {}
    this.getButton(131).then(result => {
      if (result.data.statusCode==="1") {
        AnnouncementBtn = result.data.data
        AnnouncementBtn.map((item)=>{
          if(item.id==132){
            this.AnnouncementBtn.AnnouncementBtn132 = item.checked?1:0 
          }else if(item.id==133){
            this.AnnouncementBtn.AnnouncementBtn133 = item.checked?1:0  
          }
        })
      }
    });
  },
  methods: {
    handleSizeChange(val) {
      this.showAnnouncementList(this.currpage,val)
      this.pagesize = val;
    },
    handleAvatarSuccess1(res, file) {
      this.AnnouncementFrom.imgUrls= res.data;
    },
    handleRemove1(file, fileList) {
      console.log(file, fileList);
    },
    addAnnouncement(){
      this.IsAnnouncementdia = true;
      this.AnnouncementFrom={
        title:'',
        content:'',
        imgUrls:'',
      },
      this.notice = true;
    },
    showAnnouncementList(pageNum, pageSize) {
      this.announcementLoad = true;
      var url =
        "enterprise/oa/announcement/list";
      var param = {
        mtCompanyId:JSON.parse(sessionStorage.getItem('mtCompanyId')),
        pageNum: pageNum,
        pageSize: pageSize
      };
      this.axios.post(url, param).then(result => {
        var data = result;
        if (data.statusCode) this.announcementLoad = false
        if (data.statusCode === "1" || data.statusCode === "9") {
          this.announcementList = data.data.page.rows;
          console.log(this.announcementList )
          this.announcementListTotal = data.data.count;
        } else {
          this.$message.error(data.message);
        }
      });
    },
    showAnnoucement(row){
      this.IsAnnouncementdia = false;
      this.AnnouncementFrom = row
      this.notice = true;
    },
    deleteAnnoucement(row){
      this.deleteAnnouceId = row.id;
      this.dialogdeleteAnnouce = true;
    },
    submitdeleteAnnouce(){
      var url =
        "enterprise/oa/announcement/delete";
      var param = {
        id : this.deleteAnnouceId
      };
      this.axios.post(url, param).then(result => {
        var data = result;
        if (data.statusCode === "1") {
          this.$message.success(data.message)
          this.showAnnouncementList(this.currpage,this.pagesize)
          this.dialogdeleteAnnouce = false
        } else {
          this.$message.error(data.message);
        }
      });
    },
    showImage(imgUrl){
      this.Image[0] = {
        imgUrl:imgUrl,
        sort:0,
      }
      this.dialogImageUrl = true
    },
    trans(image){
      var imageList = [];
      imageList[0] = image
      return imageList
    },
    click(val) {
      this.currpage = val;
      this.showAnnouncementList(val,this.pagesize)
    },
    handleColse(done) {},

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    uploadFile(file) {
      this.formDate.append("file", file.file);
      console.log(this.formDate)
    },
    beforeAvatarUpload(file){
      const isJPG =file.type == 'image/jpeg' || file.type == 'image/png' || file.type == 'image/gif'
　　　　const isLt2M = file.size / 1024 / 1024 < 2
　　　　if (!isJPG) {
　　　　　　this.$message.warning('上传头像图片只能是 JPG/PNG/GIF 格式!')
　　　　　　return isJPG
　　　　}
　　　　if (!isLt2M) {
　　　　　　this.$message.warning('上传头像图片大小不能超过 2MB!')
　　　　　　return isLt2M
　　　　}
　　　　this.multfileImg = file
　　　　return isJPG && isLt2M
    },
    handleAvatarSuccess(res, file) {
　　　　this.itemForm.head_img = URL.createObjectURL(file.raw)
　　},
    getNowTime() {
        var date = new Date();
        this.year = date.getFullYear();
        this.month =(date.getMonth() + 1 )< 10 ? "0" +(date.getMonth() + 1 ) : (date.getMonth() + 1 );
        this.date = date.getDate() < 10 ? "0" +date.getDate() : date.getDate();
        this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        this.milliSeconds = date.getMilliseconds();
        var currentTime = this.year+'-'+this.month + '-' + this.date + ' ' + this.hour + ':' + this.minute + ':' + this.second;
        return currentTime;
      },
    subPicForm(formName) {
      console.log(formName)
        this.$refs[formName].validate(valid => {
          if (valid) {
            var url = "enterprise/oa/announcement/add";
            var param = {}
            param = this.AnnouncementFrom
            var imgUrls = [];
            imgUrls[0] = this.AnnouncementFrom.imgUrls
            param.imgUrls =imgUrls;
            param.postDate =this.getNowTime()
            param.createDate =this.getNowTime()
            param.userId = sessionStorage.getItem('user')
            param.mtCompanyId = JSON.parse(sessionStorage.getItem('mtCompanyId'))
            this.axios.post(url,param).then(result => {
              var data = result
              if(data.statusCode==="1"){
                this.$message.success(data.message);
                this.showAnnouncementList(this.currpage,this.pagesize)
                this.notice = false;
              }else{
                this.$message.error(data.message);
              }
            });
          } else {
            this.$message({
              message: "请输入正确信息！",
              type: "warning"
            });
            return;
          }
        });
    }
//     subPicForm(){
//       　this.$refs.itemForm.validate(valid => {
// 　　　　　　if (valid) {
// 　　　　　　　　this.fd = new FormData()
// 　　　　　　　　this.fd.append('token', sessionStorage.getItem('loginToken')) //传其他参数
// 　　　　　　　　this.fd.append('id', this.itemForm.id)
// 　　　　　　　　this.fd.append('user_name', this.itemForm.user_name)
// 　　　　　　　　this.fd.append('user_nike_name', this.itemForm.user_nike_name)
// 　　　　　　　　this.fd.append('user_sex', this.itemForm.user_sex)
// 　　　　　　　　this.fd.append('user_phone', this.itemForm.user_phone)
// 　　　　　　　　this.fd.append('user_email', this.itemForm.user_email)
//                 console.log(fd);
// 　　　　　　　　if (this.multfileImg != null) {
// 　　　　　　　　　　this.fd.append('multfile', this.multfileImg)
// 　　　　　　　　}
// 　　　　　　　　api.updateUserInfo(this.fd).then(res => {
// 　　　　　　　　　　if (res) {
// 　　　　　　　　　　　　this.$message({ showClose: true, type: 'success', message: '设置成功' })
// 　　　　　　　　　　　　this.initPage()
// 　　　　　　　　　　}
// 　　　　　　　　})
// 　　　　　　} else {
// 　　　　　　　　　　this.$message({
// 　　　　　　　　　　showClose: true,
// 　　　　　　　　　　type: 'error',
// 　　　　　　　　　　message: '请检查表单信息的正确性'
// 　　　　　　　　　　})
// 　　　　　　　　return false
// 　　　　　　}
// 　　　　})
//     }

  }
};
</script>
<style lang="stylus">
  .Announcement {
    height: 100%;
    .AnnouncementMain{
      height: 100%;
      background: #fff;
      .AnnouncementTable .el-button {
            padding: 8px;
            font-size: 14px;
            border-radius: 10px;
            margin-bottom: 5px;
        }
    }
  }
  .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        width: 200px;
        height: 178px;
      }

      .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
      }

      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 100%;
        height: 178px;
        line-height: 178px;
        text-align: center;
      }

      .avatar {
        width: 100%;
        height: 100%;
        display: block;
      }

      .el-upload-list {
        width: 90%;
      }
      .el-tabs--border-card.BCtabs .el-tabs__header.is-top {
          margin: 5px 20px;
      }

</style>