<template>
  <div class="main">
    <div class="main-body">
      <el-card class="box-card">
         <div slot="header" class="clearfix" >
            <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
              <el-form-item label="选择部门">
                <el-select v-model="searchInfo.department" collapse-tags placeholder="请选择"  clearable>
                  <div class="el-input" style="width:90%;margin-left:5%;">
                    <input type="text" placeholder="请输入" class="el-input__inner" v-model="dropDownValue">
                  </div>
                  <el-option
                  v-for="(item,index) in userDepartments"
                  :value="item.departmentId"
                  v-show="item.isLeader>1"
                  @click.native="selectDepartment(item)"
                  :key="index"
                  :label="item.department"></el-option>
                </el-select>

              </el-form-item>

              <el-form-item label="选择分类">
                <el-select v-model="searchInfo.category" collapse-tags placeholder="请选择"  clearable>
                  <el-option v-for="(item,index) in categorys"
                  :value="index" :key="index" :label="item"></el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="文件标题">
                <el-input placeholder="" v-model="searchInfo.keyword" clearable></el-input>
              </el-form-item>


              <el-form-item>
                <el-button @click="getApplyArticalList" type="primary">搜索</el-button>
              </el-form-item>
            </el-form>
          </div>

        <el-table :data="restaurants"  border stripe>
          <el-table-column label="序号" width="50" prop="id" align="center">
          </el-table-column>
          <el-table-column label="下载文件" min-width="300" prop="title">
          </el-table-column>

          <el-table-column label="申请人" width="80" prop="username">
          </el-table-column>

          <el-table-column label="申请时间" min-width="110" prop="createAt">
            <template slot-scope="{row}">
              <span> {{row.createAt|toDatetime}}</span>
            </template>
          </el-table-column>

          <el-table-column label="申请原因" min-width="80" prop="applyInfo">
          </el-table-column>

          <el-table-column
            align="center"
            min-width="80"
            label="操作"
            class-name="small-padding fixed-width"
          >

            <template slot-scope="{row, $index}">
              <el-button
                v-if="!row.isSend"
                type="success"
                size="small"
                icon="el-icon-circle-check"
                @click="applyDoc(row, $index)"
              >审核</el-button>

              <el-button
                v-else-if="row.isSend==1"
                type="text"
                size="small"
                icon="el-icon-circle-check"
                @click="applyDoc(row, $index)"
              >允许下载</el-button>

              <el-button
                v-else-if="row.isSend==2"
                type="text"
                size="small"
                icon="el-icon-circle-close"
                 @click="applyDoc(row, $index)"
              >拒绝下载</el-button>
            </template>
          </el-table-column>

        </el-table>


        <el-pagination
          :current-page="page"
          :page-size="pageSize"
          :page-sizes="[10, 30, 50, 100]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :style="{float:'center',padding:'20px'}"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
      </el-card>

    </div>
    <el-dialog title="[办公室]XX 申请下载资料" center :visible.sync="dialogFormVisible"  >
      <h3> 【{{form.username}}】需要下载文件 【{{form.title}}】  </h3>
      <div class="dialog-footer" slot="footer">
        <el-button type="warning" size="medium" style="width: 100px; padding: 10px; margin: 20px;"
			@click="forbitDownload(form)" >拒绝下载</el-button>
        <el-button type="primary" size="medium" style="width: 100px; padding: 10px; margin: 20px;"
			@click="allowDownload( form)">允许下载</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import IHeader from "./components/IHeader";
import IFooter from "./components/IFooter";
import glIcon from '@/icons/img/2016smyjj_gl_icon.jpg'
import data  from '../data/data.js'
import {replyArtical,getApplyArticalList}  from '@/api/article'
import { mapGetters } from "vuex";
import {toDatetime} from '@/utils/date.js'

export default {
  name: 'application',
  components: {
    IHeader,
    IFooter
  },
  data() {
    return {
      categorys: data.categorys,
      dialogFormVisible: false,
      glIcon: glIcon,
      list: data.departs,
      total: 0,
      page:1,
      pageSize: 10,
      searchInfo:{},
      dropDownValue:'',
      departmentId: 0,
      restaurants: [],
      state3: '',
      form:{},
      selectDepartId:1,
      selectDepartName: '',
      category:'',
      isShowMore: false
    }
  },
  watch:{
    "searchInfo.category"(val){
      this.page = 1
      this.getApplyArticalList()
    }
  },
   computed: {
  	...mapGetters("user", ["userDepartments"])
  },
  mounted() {
    this.getApplyArticalList();
  },
  filters:{
    filterTitle(val){
      if( val.length > 35) {
        return val.substr(0,32)+"..."
      }else{
        return val
      }
    },
    toDatetime
  },
  methods: {
    selectDepartment(item){
      this.departmentId = item.departmentId
      this.page = 1
      this.getApplyArticalList()
    },
    handleSizeChange(size){
      this.pageSize = size
      this.getApplyArticalList()
    },
    handleCurrentChange(page){
      this.page = page
      this.getApplyArticalList()
    },
    applyDoc( row, index ){
      this.dialogFormVisible = true
      row.index = index
      this.form = row
    },
    forbitDownload(item){
      let param = {articalId: item.articalId, id:item.id, action:2}
      replyArtical( param ).then( res=>{
        if( res.code == 200){
          item.isSend = 2
          this.dialogFormVisible = false;
          this.$message.successMsg("处理完成", 2)
        }
      })
    },
    allowDownload(item){
      let param = {articalId: item.articalId,id:item.id, action:1}
      replyArtical( param ).then( res=>{
        if( res.code == 200){
          item.isSend = 1
          this.dialogFormVisible = false;
          this.$message.successMsg("处理完成", 2)
        }
      })

    },
    getApplyArticalList() {
      let {page,pageSize, departmentId} = this
      let param = {page,pageSize, departmentId}
      Object.assign( param , this.searchInfo)
      getApplyArticalList( param  ).then( res=>{
        this.restaurants = res.data.list
        this.total = res.data.total
        console.log("getHotArticalList", res)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-breadcrumb{
    display: inline-block;
    padding-left: 10px;
  }
  .box-card{
    margin-bottom: 20px;
  }
  .my-autocomplete {
    li {
      line-height: normal;
      padding: 7px;

      .name {
        text-overflow: ellipsis;
        overflow: hidden;
      }
      .addr {
        font-size: 12px;
        color: #b4b4b4;
      }

      .highlighted .addr {
        color: #ddd;
      }
    }
  }
  .el-card__body .text{
    margin: 5px 0;
  }
  .el-card{
    color: #1890ff;
  }
  .el-card__header{
    padding: 15px 20px;
  }
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }

  .oneline {
    line-height: 30px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>
