<template>
  <ai-detail>
    <template slot="title">
      <ai-title title="公告详情" isShowBack isShowBottomBorder @onBackClick="$parent.goBack"></ai-title>
    </template>
    <template #content>
      <ai-sidebar v-model="index" :tabTitle="tabTitle" @change="onChange"></ai-sidebar>
      <template v-if="index==0">
        <ai-card :title="detailObj.title" class="title">
          <template #content>
            <el-row type="flex" justify="space-between" class="info">
              <span>时间：{{detailObj.releaseTime}}</span>
              <span style="display:flex">发布单位：
                <ai-open-data type="departmentName"  :openid="detailObj.unitName"></ai-open-data>
              </span>
              <span style="display:flex">发布人：
                <ai-open-data type="userName"  :openid="detailObj.releaseUserName"></ai-open-data>
              </span>
            </el-row>
            <div v-html="detailObj.content" style="margin: 20px 0;"></div>
          </template>
        </ai-card>
        <ai-card title="附件" v-if="detailObj.files && detailObj.files.length">
          <template #right>
            <span class="Edit" @click="downFileAll"><i class="iconfont iconDownload"></i>下载全部</span>
          </template>
          <template #content>
            <ai-file-list :fileList="detailObj.files" :fileOps="{ name: 'fileName', size: 'size' }" ></ai-file-list>
          </template>
        </ai-card>
      </template>
      <template v-else>
        <ai-list>
          <template #content>
            <ai-search-bar>
              <template #left>
                <ai-select
                  v-model="search.readStatus"
                  @change="search.current=1,getList()"
                  placeholder="查阅状态"
                  :selectList="dict.getDict('announcementReadStatus')"
                ></ai-select>
              </template>
              <template #right>
                <el-input v-model="search.readUserName" @keyup.enter.native="getList()" placeholder="姓名"
                          size="small" suffix-icon="iconfont iconSearch" clearable @clear="search.current=1,getList()"></el-input>
              </template>
            </ai-search-bar>
            <ai-table
              :tableData="tableData"
              :col-configs="colConfigs"
              :total="total"
              :current.sync="search.current"
              :size.sync="search.size"
              @getList="getList">
              <el-table-column slot="readUserName" label="姓名" align="center">
                <template slot-scope="{ row }">
                  <ai-open-data type="userName"  :openid="row.readUserName"></ai-open-data>
                </template>
              </el-table-column>
              <el-table-column slot="unitName" label="所属部门" align="center">
                <template slot-scope="{ row }">
                  <ai-open-data type="departmentName"  :openid="row.unitName"></ai-open-data>
                </template>
              </el-table-column>
            </ai-table>
          </template>
        </ai-list>
      </template>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: "manageDetail",
    props: {
      instance: Function,
      dict: Object,
      detail:Object
    },
    data() {
      return {
        index: 0,
        tableData: [],
        total: 0,
        detailObj: {},
        search: {
          current: 1,
          size: 10
        }
      }
    },
    computed: {
      tabTitle() {
        return ["公告详情", "查询情况"];
      },
      colConfigs(){
        return [
          {slot:"readUserName"},
          {prop:"readUserPhone",label:"手机号",align:"center"},
          {slot:"unitName"},
          {
            prop:"readStatus",label:"查阅状态",align:"center",
            render:(h,{row})=>[<span style={{color:this.dict.getColor("announcementReadStatus",row.readStatus)}}>{this.dict.getLabel("announcementReadStatus",row.readStatus)}</span>]
          },
        ];
      }
    },
    methods: {
      downFileAll () {
        if (this.detailObj.files.length > 0) {
          this.instance.post('/app/appannouncement/downLoadAllFileForDetail', null, {
            responseType: 'blob',
            params: {
              id: this.detailObj.id
            }
          }).then((res) => {
            const link = document.createElement('a')
            let blob = new Blob([res], { type: 'application/octet-stream' })
            link.style.display = 'none'
            link.href = URL.createObjectURL(blob)
            var num = ''
            for (let i = 0; i < 10; i++) {
              num += Math.ceil(Math.random() * 10)
            }
            link.setAttribute('download', '公告文件' + '.zip')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
          })
        } else {
          this.$message.error('暂无附件提供下载')
        }
      },
      onChange(val){
        if(val==0){
          this.getDetail();
        }else {
          this.getList();
        }
      },
      getDetail() {
        this.instance.post("/app/appannouncement/detail", null, {
          params: {
            id: this.detail.id
          }
        }).then(res => {
          if (res && res.data) {
            this.detailObj = res.data;
            this.$initWxOpenData()
          }
        })
      },
      getList() {
        this.instance.post("/app/appannouncementreader/list", null, {
          params: {
            announcementId: this.detail.id,
            ...this.search
          }
        }).then(res => {
          if (res && res.data) {
            this.tableData = res.data.records;
            this.total = res.data.total;
          }
        })
      },
    },
    created() {
      this.dict.load("announcementReadStatus").then(this.getDetail);
    }
  }
</script>

<style lang="scss" scoped>
  ::v-deep .title {
    .aibar-left {
      width: 100%;
      text-align: center;
    }
  }

  .file {
    height: 40px;
    line-height: 40px;
    padding: 0 8px;
    font-size: 14px;
    color: #333;
    background: #fff;
    border-radius: 4px;
    border: 1px solid #d0d4dc;
    margin-bottom: 16px;
    cursor: pointer;
  }

  .info {
    & > span {
      font-size: 14px;
      color: #333;
    }
  }
</style>
