<template>
  <div class="main">
    <div class="main-table">
      <div class="main-left">
        <el-menu
          default-active="1-5"
          class="el-menu-vertical-demo"
          style="min-height: 700px;"
          >
          <template  v-for="(item, index) in options">
            <el-menu-item :index="'1-'+item.id" @click="selectTag(item)" :key="index">
              <span>{{ item.value }}</span>
            </el-menu-item>
          </template>

        </el-menu>

      </div>

      <div class="main-right">
        <!-- <el-card class="nopad"> -->
          <div slot="header" class="clearfix">
            <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
            	<el-form-item :label="selectName">
                <el-input placeholder="" v-model="searchInfo.keyword" style="width: 500px;" clearable></el-input>
              </el-form-item>

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

          </div>


          <el-table :data="list"  border stripe :row-style="{'height':'75px'}">

            <el-table-column label="序号" width="95"  prop="articalId" align="center">
              <template v-slot="{row}">
                <span> 【{{openMap[row.isOpen]}}】{{row.articalId}}</span>
              </template>
            </el-table-column>


            <el-table-column label="标题" min-width="180" prop="title">
              <template slot-scope="{row}">
                <el-input  v-if="row._edit" v-model="row.title" type="text"></el-input>
                <span v-else> {{row.title}}</span>
              </template>
            </el-table-column>

            <el-table-column label="部门" width="110" prop="department" align="center">
            </el-table-column>


           <el-table-column label="时间" width="100" prop="createAt" align="center">
              <template slot-scope="{row}">
                <span> {{row.createAt|toDate}}</span>
              </template>
            </el-table-column>

            <el-table-column label="操作" width="150" align="center">
              <template v-slot="{row, $index}">
                <el-button  type="text"  @click="cancelEdit(row)" v-if="row._edit"> 取消 </el-button>
                <el-button  type="text" style="color: green;" @click="confirmEdit(row)" v-if="row._edit"> 确认 </el-button>


                <el-button  type="text" @click="initEdit(row)" v-if="!row._edit &&(selectId==5 || selectId==1)"> 改标题 </el-button>

                <!-- look -->
                <el-button  type="text" @click="editFile(row)" v-if="!row._edit && (selectId==5 || selectId==1)">编辑</el-button>

                <el-button  type="text" @click="gotoDetail(row)"  v-else-if="!row._edit"> 查看 </el-button>

                <!-- delete -->

                <el-button v-if="!row._edit &&(selectId==5 || selectId==1) " type="text" style="color:darkorange"  @click="row._delete=true" >删除</el-button>

                <el-popover placement="top" width="150"  v-model="row._delete"  :style="{'margin-top': $index>0?'0px':'-60px'}">
                  <p>你是否确认删除？</p>
                  <div style="text-align: right; top: 0">
                    <el-button size="mini" type="text" @click="row._delete = false">取消</el-button>
                    <el-button type="primary" size="mini" @click="deleteArtical(row)">确定</el-button>
                  </div>
                </el-popover>
              </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>
    </div>
  </div>
</template>

<script>
import IHeader from "./components/IHeader";
import IFooter from "./components/IFooter";
import {getArticalList,getApplyArticalList, deleteArtical}  from '@/api/article'
import {getEditArticalList,downloadFile, editArticalTitle}  from '@/api/article'
import {download} from '@/utils/download.js'
import {toDatetime, toDate} from '@/utils/date.js'
import data from '../data/data.js'
import { mapActions } from "vuex";

export default {
  name: 'application',
  components: {
    IHeader,
    IFooter
  },
  watch:{
    'searchInfo.keyword'(val){
      console.log("watch", val)
      this.getArticalList()
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      options:data.myfiles,
      pageSize: 10,
      page:1,
      searchDepartId: "",
      openMap:data.openMap,
      listApi: getEditArticalList,
      restaurants: [],
      searchInfo:{
        keyword:''
      },
      state3: '',
      selectId:"5",
      selectName: '我经办文件',
      isShowMore: false
    }
  },
  filters:{
    toDatetime,
    toDate
  },
  created() {
    this.getArticalList();
  },
  methods: {
    ...mapActions("user", ["logout"]),
    download( row ){
      let query= {articalId: row.articalId }
      this.$router.push( {path: '/file-info', query})
    },
    deleteArtical( row ){
      deleteArtical( {articalId: row.articalId}).then(res=>{
        if( res.code == 200){
          row._delete = false
          row.status = 4
          row.isOpen = 2
        }
      })
    },
    cancelEdit(row) {
    	row = Object.assign(row, row.temp);
    	row._edit = false;
    	delete row["temp"];
    },
    initEdit(row) {
    	let form = Object.assign({}, row);
    	row._edit = true;
    	row.temp = form;
    },
    confirmEdit(row) {
    	let param = {articalId:row.articalId, title:row.title}
    	editArticalTitle( param ).then((res) => {
    		if (res.code == 200) {
    			row._edit = false;
    			delete row["temp"];
    			this.$message.successMsg("编辑成功", 1);
    		}
    	});
    },

    editFile( row ){
      let path = "/file-add"
      let query = {articalId: row.articalId}
      this.$router.push( {path, query})
    },
    gotoDetail(row ){
      let path = "/file-info"
      let query = {articalId: row.articalId}
      this.$router.push( {path, query})
    },
    doLogout(){
      this.logout().then( res=>{
        console.log("logout")
      })
    },
    selectTag( item ) {
      this.selectId = item.id;
      this.listApi = item.listApi,
      this.selectName = item.value
      this.getArticalList()
      // 切换文件列表
    },
    handleSizeChange(size){
      this.pageSize = size
      this.getArticalList()
    },
    handleCurrentChange(page){
      this.page = page
      this.getArticalList()
    },
    getArticalList(){
      let {page,pageSize} = this
      let param = {page,pageSize, keyword: this.searchInfo.keyword}
      this.listApi( param ).then( res=>{
        if( res.code == 200){
          let list = res.data.list||[];
          this.list = list.map( item => {
            item._delete = false;
            item._edit = false;
            return item
           });
          this.total = res.data.total||0
        }
      })
    }
  }
}
</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;
  }
</style>
