<template>
  <div>
    <div class="header_button">
      <el-form label-width="80px">
        <el-row :span="24">
          <el-col :span="8">
            <el-form-item label="发布时间">
              <el-date-picker   v-model="query.publishTime"  type="datetimerange"  range-separator="至"  start-placeholder="开始日期"  end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss"  unlink-panels></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5" style="margin-right:15px">    
            <el-form-item label="标题">
              <el-input class="wdh" v-model="query.title"></el-input>
            </el-form-item>
          </el-col>
          <el-button type="success" @click="clear">清空</el-button>
          <el-button type="success" @click="search">查询</el-button>
          <!-- <el-popover placement="bottom"  trigger="click" style="float:right">
              <el-button @click="addBtn('1')">分公司</el-button>
              <el-button @click="addBtn('2')">粮库</el-button>
            <el-button type="success" slot="reference">新建公告</el-button>
          </el-popover> -->
          <el-button type="success" style="float:right" @click="addBtn()">新建公告</el-button>
        </el-row>
      </el-form>
    </div>
    <div class="table">
      <el-table border stripe :data="tableList" highlight-current-row>
        <el-table-column type="index" align="center" label="序号" width="50px" />
        <el-table-column prop="title" label="标题" align="center" />
        <el-table-column prop="publishTime" label="发布时间" align="center" />
        <el-table-column prop="publishUserName" label="发布人" align="center" />
        <el-table-column prop="publishOrgName" label="发布人所属单位" align="center" />
        <el-table-column prop="recvDetailsInfo" label="接收人" align="center" />
        <el-table-column prop="operation" label="操作" align="center" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleDetail(scope.row)">查看</el-button>
            <!-- <el-button size="mini" @click="handleDelete(scope.row)">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
          <el-pagination
            class="right"
            :current-page.sync="listQuery.page"
            :page-size="listQuery.limit"
            :page-sizes="[10,20,30,40]"
            :total="total"
            @size-change="sizeChange"
            @current-change="pageChange"
            layout="total, sizes, prev, pager, next, jumper"
          ></el-pagination>
        </div>
    </div>
  </div>
</template>
<script>
import { getList,deleteFun,enableWarn,stopWarn,getAllRole,getDepotAll,getDepotById } from '@/api/system/notice'
import { getUser } from '@/utils/auth'
export default {
  name: "NewNotice",
  data() {
    return {
      // 列表数据
      tableList: [],
      query: {
        publishTime:[],
        title:'',
        annoType:''
      },
      // 分页
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
      },
      roleList:[],
      depotIdList:[],
    }
  },
  created() {
    this.getRoleList()
    this.getDepotList()
    this.getList()
  },
  methods: {
    getRoleList() {
      getAllRole().then(res => {
        this.roleList = res.data
      })
    },
    getDepotList() {
      let depotId = ''
      getDepotAll(depotId).then(res => {
        this.depotIdList = res.data
      })
    },
    getList() {
      console.log(this.query.publishTime)
      let condition = {
        publishTimeStart:'',
        publishTimeEnd:'',
        title: this.query.title,
        annoType: this.query.annoType
      }
      if(this.query.publishTime && this.query.publishTime.length>0){
        condition.publishTimeStart = this.query.publishTime[0]
        condition.publishTimeEnd = this.query.publishTime[1]
      }
      let data = {
        pageIndex: this.listQuery.page,
        pageSize: this.listQuery.limit,
        condition: JSON.stringify(condition)
      }
      getList(data).then((res) => {
          if (res.code == 200) {
            this.tableList = res.data.records
            this.listQuery.limit = res.data.size
            this.total = res.data.total
          }
      }).catch((err) => {
          console.log(err)
        })
    },
    changeRecvDetailsName(list) {
      list.forEach(item => {
        if(item.recvType == '20'){
          let recvDetailsNew = item.recvDetails
          let recvDetailsarrNew = recvDetailsNew.split(',')
          let recvDetailsNameNew = []
          recvDetailsarrNew.forEach(itemId => {
            getDepotById(itemId).then(response => {
              recvDetailsNameNew.push(response.data.depot.dname)
              item.recvDetailsName = recvDetailsNameNew.join(',');
            })
          })
        }else{
          item.recvDetailsName = ''
        }
      })
    },
    search() {
      this.getList()
    },
    clear() {
      this.query = {
        publishTime:[],
        title:'',
        annoType:''
      }
    },
    sizeChange(v) {
      this.listQuery.limit = v;
      this.getList();
    },
    pageChange(v) {
      this.listQuery.page = v;
      this.getList();
    },
    // 新增按钮
    addBtn() {
      if(JSON.parse(getUser()).depotId == '1'){
        this.$router.push({ path: "/notice/addnotice",query: { state: 'add',orgType: '1' } })
      }else{
        this.$router.push({ path: "/notice/addnotice",query: { state: 'add',orgType: '2' } })
      } 
    },
    // 查看
    handleDetail(row) {
      if(row.orgType == '分公司'){
        this.$router.push({ path: "/notice/addnotice",query: { state: 'detail',id: row.id }})
      }else if(row.orgType == '粮库'){
        this.$router.push({ path: "/notice/addnotice",query: { state: 'detail',id: row.id }})
      }
      
    },
    // // 编辑
    // handleEdit(row) {
    //   this.$router.push({ name: 'addEarlyWarningManagement',query: { state: 'edit',id: row.id }})
    // },
    // 删除
    handleDelete(row) {
      this.$confirm('确定删除该项记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          deleteFun(row.id).then((res) => {
            if (res.code == 200) {
              this.getList()
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
            }
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
        })
    },
  }
}
</script>
<style scoped>
.header_button {
  margin-bottom: 5px;
}
.pagination-container {
  margin-top:10px;
  margin-right:20px;
  text-align: right;
}
</style>
