  <template>
  <div class="box">
    <div class="condition">
      <div class="main" :style="tableData && tableData.length?'height: calc(100% - 50px);':'height: calc(100% - 46px);'">
        <el-table ref="multipleTable" :data="tableData" v-loading='loading' stripe height="100%" :header-cell-style="headerStyle"
          @expand-change="changeMessageState">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column width="25">
            <template slot-scope="scope">
              <el-badge class="item" type="warning" is-dot v-if="!scope.row.Read"></el-badge>
            </template>
          </el-table-column>
          <el-table-column prop="Title" label="标题内容" min-width="300" show-overflow-tooltip></el-table-column>
          <el-table-column label="项目">
            <template slot-scope="scope">
              {{ scope.row.Project[0].ProjLocalName }}
            </template>
          </el-table-column>
          <el-table-column label="模块" width="200">
            <template slot-scope="scope">
              {{ moduleMap[scope.row.Module]?moduleMap[scope.row.Module]:scope.row.Module }}
            </template>
          </el-table-column>
          <el-table-column prop="CreateTime" label="消息时间">
          </el-table-column>
          <el-table-column label="消息类型">
            <template slot-scope="scope">
              <div>
                <i :class="iconClassMap[scope.row.Type]?iconClassMap[scope.row.Type]:'msg-icon el-icon-warning warning-color'" style="font-size:14px;"></i>
                <span>{{ typeMap[scope.row.Type]?typeMap[scope.row.Type]:scope.row.Type }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column type="expand">
            <template slot-scope="scope">
              <p>{{ scope.row.Content.Message }}</p>
              <el-link v-for="(btn, index) in scope.row.Content.ButtonList" style="float:left;font-size:12px;color:white;width:"
                :key="index" :href="`/image-service/common/file_get?systemId=revit&key=${btn.Url}`" :download="btn.FileName?btn.FileName:''">
                {{btn.Name}}
              </el-link>
            </template>
          </el-table-column>
          <template slot="empty">
            <div style="height: 60%;transform: translateY(50%);">
              <i class="icon-wushuju iconfont"></i>
              数据暂无
            </div>
          </template>
        </el-table>
      </div>
      <el-pagination class="right" v-show="tableData && tableData.length" @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="page.pageNumber" :page-sizes="page.pageSizes" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
  </div>
</template>

  <script>
import { mapGetters } from 'vuex'
import { messgeQuery, messgeUpdate } from '@/api/msgsever'
import Bus from '@/utils/bus.js'

export default {
  data() {
    return {
      tableData: [],
      loading: false,
      page: {
        pageSize: 50,
        pageSizes: [10, 20, 50, 100],
        pageNumber: 1,
        total: 0
      },
      headerStyle: {// 列表样式
        backgroundColor: '#e1e4e5',
        color: '#2b2b2b',
        lineHeight: '30px'
      }, 
      moduleMap: {//消息模块映射
        "Model": "模型文件管理"
      },
      iconClassMap: { //消息类型图标映射
        "Success": "msg-icon el-icon-success success-color",
        "Error": "msg-icon el-icon-error error-color",
        "Warning": "msg-icon el-icon-warning warning-color",
        "Info": "msg-icon el-icon-info info-color",
      },
      typeMap: { //消息类型文字映射
        "Success": "成功",
        "Error": "错误",
        "Warning": "警告",
        "Info": "提醒",
      }
    }
  },
  computed: {
    ...mapGetters('layout', ['userId'])
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    // 获取列表数据
    getTableData() {
      let params = {
        Cascade: [
          {
            Name: "project"
          }
        ],
        Filters: `UserId='${this.userId}'`,
        Orders: "CreateTime desc, Id asc",
        PageNumber: this.page.pageNumber,
        PageSize: this.page.pageSize
      }
      this.loading = true;
      messgeQuery(params, res => {
        this.tableData = res.Content;
        this.page.total = res.Total;
        this.loading = false;
      })
    },
    // 切换每页显示数量
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getTableData();
    },
    // 切换页码
    handleCurrentChange(val) {
      this.page.pageNumber = val;
      this.getTableData();
    },
    // 更新消息状态：已读、未读
    changeMessageState(val) {
      if(!val.Read){
        val.Read = true;
        let params = {
          Content: [{
            Id: val.Id,
            Read: true
          }]
        }
        messgeUpdate(params, res => { 
          Bus.$emit('getUnreadCountUpdate')
        });// 展开更新为已读状态,未读数量同步变更
      }
    }
  }
};
  </script>

<style scoped lang='less'>
.box {
  .condition {
    padding: 10px;
    display: flex;
    height: 100%;
    flex-direction: column;
    border: 1px solid #dfe6ec;
    background: #fff;
    .main {
      margin-top: 10px;
      a{
        text-decoration: none;
      }
    }
  }
}
.el-badge.item {
  height: 10px;
}
.el-table__expanded-cell {
  p {
    margin-left: 40px;
  }
}
.el-link {
  margin: 20px 0 0 40px;
  background: #409eff;
  border-radius: 3px;
  padding: 3px 15px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  margin-left: 120px;
  width: 100%;
}
.success-color {
  color: #67c23a;
}
.error-color {
  color: #f56c6c;
}
.warning-color {
  color: #e6a23c;
}
.info-color {
  color: #909399;
}
</style>