<template>
  <!-- 公告详情 -->
  <view class="inform">
    <view class="details">
      <view class="title">{{ noticeInfo.messageHeader }}</view>
      <!--   <rich-text :nodes="noticeInfo.context"></rich-text> -->

      <view class="footer">
        <text style="color: #1492FF;">{{ noticeInfo.publishDept }}</text>
        <text class="segmentation"> | </text>
        <text style="color: #666666;">{{ noticeInfo.createTime !== null ? noticeInfo.createTime : '-' }}</text>
      </view>
      <mp-html :content="noticeInfo.messageContent" />
      <view class="annex">
        <view class="label">附件</view>
        <view>
          <view class="file-item" v-for="item in noticeInfo.files" :key="item.key" @tap="fileDownload(item)">{{
        item.name
      }}</view>
        </view>
      </view>
    </view>
    <!--   <view class="relation-list">
      <view class="list-item" v-for="(item,index) in noticeList" :key="item">
        <view class="list-item__title">{{`${index+1}.${item.title}` }}</view>
        <view class="list-item__time">{{ item.publishTime }}</view>
      </view>
    </view> -->
    <uni-section class="custom-section" title="政策公告" type="line" padding>
      <!-- <uni-steps :options="noticeList" active-color="#007AFF" :active="active" direction="column" @click="handleClick(e)"/> -->
      <view class="notice-list" @tap="handleInform">
        <view v-for="item in noticeList" :key="item.id" class="item">
          <text class="title">{{ item.messageHeader }}</text>
          <view class="time">{{ item.createTime !== null ? item.createTime : '-' }}</view>
          <!-- <text class="notice-tag"></text>
          <uni-tag v-show="index === 0" text="最新" size="mini" type="error" /> -->
        </view>
      </view>
    </uni-section>
  </view>
</template>

<script>
import { getNoticeDetail, getNoticeList, newDownFile } from "@/api/notice.js";
import { replaceSrc } from "@/utils";
const app = getApp();
export default {
  data() {
    return {
      noticeInfo: {},
      curNoticeId: "",
      noticeList: [],
      active: -1,
    };
  },

  async onLoad(options) {
    this.curNoticeId = options.noticeId;
    await this.getNoticeDetail(options.noticeId);
    this.composeNoticeList();
  },

  methods: {
    handleClick(e) {
      console.log("被点击对象", e);
      if (e.uuid === this.curNoticeId) {
        return;
      }
      this.noticeList = [];
      uni.redirectTo({
        url: `/subPages/pages/inform/inform?noticeId=${e.id}`,
      });
    },
    composeNoticeList() {
      getNoticeList({
        current: 1,
        size: 3,
        status: 2,
        type: 0
      }).then((res) => {
        console.log(res, 'list');
        this.noticeList = res.data.records;
      });
      // console.log("app.globalData.noticeList", app.globalData.noticeList);
      // if (app.globalData.noticeList)
      // this.noticeList = app.globalData.noticeList.map((item, index) => {
      //   if (item.uuid == this.curNoticeId) {
      //     this.active = index;
      //   }
      //   return {
      //     title: item.title,
      //     desc: item.publishTime,
      //     uuid: item.uuid,
      //   };
      // });
    },
    async getNoticeDetail(noticeId) {
      const res = await getNoticeDetail({ id: noticeId });
      console.log(
        "%c [ res ]-31",
        "font-size:13px; background:pink; color:#bf2c9f;",
        res
      );
      // this.noticeInfo = res.data;
      this.noticeInfo = {
        ...res.data,
        // context: replaceSrc(res.data.messageContent),
        files: this.filesPretreatment(res.data.attach)
      };
    },
    filesPretreatment(obj) {
      if (obj !== '' && Array.isArray(JSON.parse(obj))) {
        return JSON.parse(obj)
      } else {
        return []
      }
    },
    fileDownload(obj) {
      console.log(obj);

      ZWJSBridge.setClipboardData({
        // "text": `https://fhszmp.fh.gov.cn:8080/business/callingPolice/downFile?uuid=${obj.key}`
        "text": `http://122.247.77.37:9080/business/callingPolice/downFile?uuid=${obj.key}` + ''
      }).then(res => {
        console.log(res);
        ZWJSBridge.toast({
          "message": "复制成功，请将地址粘贴到浏览器后再下载",
          "duration": 2
        }).then(res => {
          console.log(res)
        }).catch(err => {
          console.log(err)
        })
      }).catch(err => {
        console.log(err)
      })



      // newDownFile({
      //   uuid: obj.key
      // }).then(res => {
      //   // console.log(res);
      //   // console.log(wx.env.USER_DATA_PATH, 'wx.env.USER_DATA_PATH');

      //   const fs = uni.getFileSystemManager(); //获取全局唯一的文件管理器
      //   // fs.writeFile({ // 写文件
      //   //   filePath: wx.env.USER_DATA_PATH + '/' + obj.name, // wx.env.USER_DATA_PATH 指定临时文件存入的路径，后面字符串自定义
      //   //   data: res.data,
      //   //   encoding: "binary", //二进制流文件必须是 binary
      //   //   success(res) {
      //   //     // console.log(res);
      //   //     uni.openDocument({
      //   //       filePath: wx.env.USER_DATA_PATH + '/' + obj.name,
      //   //       showMenu: true,
      //   //       success: function (res) {
      //   //         console.log('打开文档成功')
      //   //       }
      //   //     })
      //   //   }
      //   // });
      // })
    },
  },
};
</script>

<style lang="scss" scoped>
.inform {
  height: calc(100% - 40rpx);
  // padding: 20rpx 30rpx;
  overflow: auto;
  // background-color: #ffffff;
  font-size: 30rpx;
  color: #333333;

  .details {
    background-color: white;
    margin-bottom: 10px;
    padding: 20rpx 30rpx;
  }

  .title {
    // text-align: center;
    font-size: 32rpx;
    font-weight: 900;
    color: #333333;
    padding: 10rpx 0;
    // margin-bottom: 10rpx;
  }

  .footer {
    // text-align: right;
    // margin-top: 50rpx;
    font-size: 28rpx;
    padding: 10px 0;
    border-bottom: 1px solid #F2F3F5;
    margin-bottom: 10px;

    .segmentation {
      margin: 0 10px;
      color: #ECECEC;
    }
  }

  .annex {
    margin-top: 20px;
    display: flex;

    .label {
      margin-right: 10px;
      white-space: nowrap;
    }

    .file-item {
      word-break: break-all;
      text-decoration: underline;
      margin-bottom: 10px;
    }
  }

  .custom-section {

    .notice-list {
      flex: auto;
      overflow: hidden;

      .item {
        width: 100%;
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid #ddd;
        font-size: 14px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        display: flex;
        align-items: center;

        &:last-child {
          border-bottom: none;
        }

        .title {
          width: 270px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }

        .time {
          width: calc(100% - 277px);
          margin-left: 7px;
          white-space: nowrap;
        }
      }
    }
  }

  ::v-deep .uni-section-header {
    padding: 18px 0px 0 0;
  }
}
</style>
