<template>
  <div id="list-2">
    <van-col span="24" class="main">
      <van-col span="24" class="list" v-for="(item, index) in list" :key="index">
        <van-col span="24" class="other">
          <van-col span="24" class="otherInfo">
            发送人：<span>{{ item.send_name || '暂无' }}</span>
          </van-col>
          <van-col span="24" class="otherInfo">
            发送时间：<span>{{ getDate(item.meta) }}</span>
          </van-col>
          <van-col span="24" class="otherInfo" v-if="item.to_type == '4'">
            是否阅读：<span>{{ item.is_read == true ? '已读' : '未读' }}</span>
          </van-col>
          <van-col span="24" class="otherInfo">
            <span>{{ item.content || '暂无' }}</span>
          </van-col>
        </van-col>
        <van-col span="24" class="btn">
          <van-button type="info" size="small" @click="download(item.notice_file)">文件下载</van-button>
          <van-button type="info" size="small" @click="toEdit(item)" v-if="item.to_type == '4' && item.is_read == false">消息已读</van-button>
        </van-col>
      </van-col>
    </van-col>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
var moment = require('moment');
export default {
  name: 'list-2',
  props: {
    list: { type: Array },
  },
  components: {},
  data: function () {
    return {};
  },
  created() {},
  methods: {
    toEdit(data) {
      this.$emit('toEdit', data);
    },
    getDate(val) {
      let newDate = moment(val.createdAt).format('YYYY-MM-DD HH:mm:ss');
      if (newDate) return newDate;
    },
    // 文件下载
    download(data) {
      if (data.length > 0) {
        let url = data.map((i) => i.url);
        window.location.href = `${process.env.VUE_APP_HOST}${url[0]}`;
      } else {
        this.$toast({ type: `fail`, message: `非正常文件，无法下载` });
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  padding: 8px 0 0 0;
  .list {
    background-color: #fff;
    margin: 0 0 8px 0;
    padding: 8px;
    border-radius: 5px;
    .title {
      font-size: 16px;
      font-weight: bold;
      margin: 0 0 5px 0;
    }
    .other {
      margin: 0 0 5px 0;
      .otherInfo {
        font-size: 14px;
        color: #666;
        margin: 0 0 5px 0;
        span {
          color: #000;
          line-height: 20px;
        }
      }
    }
    .btn {
      text-align: center;
      .van-button {
        margin: 0 5px;
      }
    }
  }
}
</style>
