<template>
  <div class="container-accountability managementList">
    <el-form :model="formInline" :inline="true" class="search-bm-boder">
        <el-form-item label="考核年度:">
          <!--        <el-select v-model="formInline.year" placeholder="请选择考核年度" clearable class="inputCompatible">-->
<!--          <el-option :key="item.index" :label="item" :value="item" v-for="item in dateyear"></el-option>-->
<!--        </el-select>-->
        <el-date-picker
                type="year"
                v-model="formInline.year"
                class="inputCompatible"
                value-format="yyyy"
                placeholder="请选择考核年度"
        ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <div class="search-button">
            <span class="search-line"></span>
            <el-button type="primary" size="small" class="iconfont iconchaxun" @click="search">查询</el-button>
            <el-button type="primary" size="small" class="iconfont iconzhongzhi" @click="reset">重置</el-button>
          </div>
        </el-form-item>
    </el-form>
    <el-row class="addBtnBg">
        <el-button type="primary" size="small" class="iconfont iconxinzeng" @click="add">新增</el-button>
        <el-button type="primary" size="small" class="iconfont iconneimengicon-4" @click="copyClick">复制评分表</el-button>
        <el-button type="primary" size="small" class="iconfont iconneimengicon-3" @click="uploadClick">导入评分表</el-button>
        <el-button type="primary" size="small" class="iconfont iconxiazaimoban" @click="excelDownload">下载模板</el-button>
    </el-row>
    <el-row>
      <el-col :span="24">
        <!-- @row-click="tableRowClick" -->
        <el-table
          :data="tableList"
          v-loading="loading"
          border
          stripe
          highlight-current-row
        >
          <el-table-column align="center" label="序号" width="60">
            <template slot-scope="scope">
              <span>{{scope.$index+1}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="考核年度" sortable :show-overflow-tooltip="true" width="100">
            <template slot-scope="scope">
              <span>{{scope.row.year}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="评分表名称" sortable :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{scope.row.pfbmc}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="发布时间" sortable :show-overflow-tooltip="true" width="200">
            <template slot-scope="scope">
              <span>{{scope.row.pfb_time}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="状态" sortable :show-overflow-tooltip="true" width="100">
            <template slot-scope="scope">
              <span>{{scope.row.status_value}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="183" align="center">
            <template slot-scope="scope">
              <el-tooltip class="item" effect="light" content="查看" placement="top">
                <el-button
                  type="primary"
                  size="small"
                  class="iconfont iconchakan2 operate_show_btn"
                  @click.stop="handleSee(scope.row)"
                ></el-button>
              </el-tooltip>
               <el-tooltip class="item" effect="light" content="编辑" placement="top" v-if="buttonNameList.indexOf('编辑') > -1 && !(scope.row.status == '1' || scope.row.status == '2')">
                <el-button
                  type="primary"
                  size="small"
                  class="iconfont iconxiugai1 operate_edit_btn"
                  @click.stop="handleModify(scope.row)"
                >
                </el-button>
              </el-tooltip>
              <el-tooltip class="item" effect="light" content="发布" placement="top" v-if="buttonNameList.indexOf('发布') > -1 && !(scope.row.ISFB == 'NO' || scope.row.status == '1' || scope.row.status == '2')">
                <el-button
                  type="primary"
                  size="small"
                  class="iconfont iconfabu operate_issued_btn"
                  :disabled='scope.row.isDisabled'
                  @click="handlePublish(scope.row,scope.$index)">
                </el-button>
              </el-tooltip>
              <el-tooltip class="item" effect="light" content="导出" placement="top" v-if="buttonNameList.indexOf('导出') > -1">
                <el-button type="primary" size="small" class="iconfont icondaochu11 operate_download_btn" @click.stop="exportClick(scope.row)">
                </el-button>
              </el-tooltip>
              <el-tooltip class="item" effect="light" content="删除" placement="top" v-if="buttonNameList.indexOf('删除') > -1 && !(scope.row.status == '1' || scope.row.status == '2')">
                <el-button
                  type="primary"
                  size="small"
                  class="iconfont iconshanchu operate_delete_btn"
                  @click.stop="handleDetail(scope.row)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <div v-show="!listLoading" class="pag-right clearFix">
          <el-pagination
            class="right"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="page.page"
            :page-sizes="[10,20,30,50]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            v-if="paginationShow"
            :total="page.total"
          ></el-pagination>
        </div>
      </el-col>
    </el-row>


  </div>
</template>
<script>
import { authorityControl } from "@/api/login/login"; 
import * as management from "@/api/headResponsibility/management";

import {business } from "@/api/index";
export default {
  data() {
    return {
      curServer: `${business}`,
      formInline: {
        year:"",
        source: "20"
      },
      dateyear: [],
      tableList: [],
      loading: false,
      listLoading: false,
      paginationShow: true,
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      buttonNameList: []
    };
  },
  methods: {
    /**
     * 查询按钮
     */
    search() {
      this.paginationShow = false;
      this.handleCurrentChange(1);
      this.$nextTick(function() {
        this.paginationShow = true;
      });
    },
    reset() {
      this.formInline = {
        year:"",
        source: "20"
      }
      this.search()
    },
    /**
     * 新增按钮  managementSave
     */
    add(){
      this.$router.push({
        path:"/accountability/managementSave"
      })
    },
    //复制评分表
    copyClick(){
       this.$router.push({
        path:"/accountability/managementCopy"
      })
    },
    //上传
    uploadClick(){
       this.$router.push({
        path:"/accountability/managementUpload"
      })
    },
    /**
     * 修改
     */
    handleModify(row){
      //  isDisabled: 1 查询  2 编辑
      this.$router.push({
        path:"/accountability/managementEdit",
        query:{
          id:row.id,
          year:row.year,
          isDisabled:"2",
          markYear: this.formInline.year
        }
      })
    },


    /**
     * 发布
     */
    handlePublish(row,ind){
        this.tableList[ind].isDisabled = true
        let pubObject = {
          id:row.id,
          url:'/accountability/taskAudit',
          itemUrlHaveDone: '/accountability/taskAudit',
          itemName:`${row.year}年度${row.pfbmc}自评`,
          // itemQuery: JSON.stringify({idKey:'id'})
        }
        management.responsibilityPublish(pubObject).then(res=>{
          if (res.infoCode==200) {
            this.$message({
              type: 'success',
              message: '发布成功',
              duration: 2000
            });
            this.search()
          } else {
            this.$message.error(res.msg)
            this.tableList[ind].isDisabled = false
          }
        })
    },
    //导出
    exportClick(row) {
      window.location.href=`${this.curServer}examinationExcel/exportKhpfbExcel/${row.id}`
    },
    /**
     * 下载模板
     */
    excelDownload(){
      // if(this.formInline.year) {
      //   window.location.href=`${this.curServer}examinationExcel/downloadKhpfbExcel/${this.formInline.year}`
      // }else {
      //   this.$message({
      //     message: '请选择考核年度',
      //     type: "warning",
      //     duration: 2000
      //   });
      // }
      window.location.href=`${this.curServer}examinationExcel/downloadKhpfbExcel`
    },
    /**
     * 导入按钮
     */
    theImport(){
         this.$router.push({ path: "/accountability/managementAdd" });
    },
    /**
     * 表格行内点击事件
     */
    tableRowClick(row, column, event) {
      if (event.label != "操作") {
        this.handleSee(row);
      }
    },
    /**
     * 表格查看按钮
     */
    handleSee(row) {
      //  isDisabled: 1 查询  2 编辑
        this.$router.push({
            path:"/accountability/managementSee",
            query:{
                id:row.id,
                year:row.year,
                isDisabled:"1",
                markYear: this.formInline.year
            }
        })
    },

    /**
     * 表格删除按钮
     */
    handleDetail(row){
            this.$confirm("您确定要删除吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              management.responsibilityDeletes(row.id).then(res => {
              if (res.infoCode == 200) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 2000
              });
            this.getTabList();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
                duration: 2000
              });
            }
          });
        })
    },
      /**
       * 查询可选年分
       */
      findByYear(type){
        management.responsibilityFindByYear(type).then(res => {
            if (res.infoCode == 200) {
              this.dateyear = res.data;
            }else {
              this.$message({
                message: res.msg,
                type: "error",
                duration: 2000
              });
            }
        });
      },

    /**
     * 分页大小
     */
    handleSizeChange(val) {
      this.page.size = val;
      this.getTabList()
    },

    /**
     * 分页第几页
     */
    handleCurrentChange(val) {
      this.page.page = val;
      this.getTabList()
    },
    /**
     * 表格数据查询接口
     */
    getTabList(type) {
      if (type != "start") {//非初始化调用：查询、分页
          sessionStorage.setItem('searchData', JSON.stringify(this.formInline))
          sessionStorage.setItem('pageData', JSON.stringify(this.page))
      } else {//初始化分两种情况，详情页等返回后的初始化，以及第一次进入此菜单的初始化
          if(JSON.parse(sessionStorage.getItem('searchData'))){//详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
              this.formInline = JSON.parse(sessionStorage.getItem('searchData'))
              this.page = JSON.parse(sessionStorage.getItem('pageData'))
          }else {//第一次进去此菜单时，保存上方默认的data
              sessionStorage.setItem('searchData', JSON.stringify(this.formInline))
              sessionStorage.setItem('pageData', JSON.stringify(this.page))
          }
      }
      this.loading = true;
      management.responsibilityFindByPage(this.formInline, this.page)
        .then(res => {
            if (res.infoCode==200) {
                this.tableList=res.data.list
                this.page.total=res.data.total
            } else {
              this.$message({
                type: 'error',
                message: res.msg,
                duration: 2000
              })
            }
            this.loading = false;
        });
    },
    getButtonList(menuCode,userCode) {
      let codeData = {
          resourceCode:menuCode,
          userId:userCode,
      }
      authorityControl(codeData).then(res => {
         let buttonNameList = []
         res.data.forEach(item => {
             this.buttonNameList.push(item.resourceName)
         })
      })
    },
  },
  created() {
    this.getButtonList(sessionStorage.getItem("resourceCode"),sessionStorage.getItem("userCode")) 
    if(this.$route.query.markYear) {
      this.formInline.year = this.$route.query.markYear
    }
    this.findByYear('page');
    this.getTabList('start')
  }
};
</script>
<style lang="scss">
.managementList {
  .el-select {
    width: 100%;
  }
}
</style>
