<template>
  <div class="container-accountability managementList">
    <el-form :model="formInline" :inline="true" class="search-bm-boder">
      <el-form-item label="考核年度:" label-width="72px">
<!--        <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 label="评分标准:" label-width="72px">
        <el-input v-model="formInline.taskName" clearable class="inputCompatible"></el-input>
      </el-form-item>
      <el-form-item label="状态:" label-width="44px">
        <el-select placeholder="请选择状态"  v-model="formInline.status" clearable class="inputCompatible">
          <el-option :key="item.index" :label="item.label" :value="item.value" v-for="item in type"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <div class="search-button">
          <span class="search-line"></span>
          <div class="search-select" @click="searchShowMoreBool = !searchShowMoreBool">
            更多条件
            <i class="el-icon-arrow-up" v-show="searchShowMoreBool"></i>
            <i class="el-icon-arrow-down" v-show="!searchShowMoreBool"></i>
          </div>
          <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-row v-show="searchShowMoreBool">
        <el-form-item label="牵头部门:" label-width="72px">
          <!-- <el-input v-model="formInline.qtbm" clearable class="inputCompatible"></el-input> -->
          <el-select placeholder="请选择" v-model="formInline.qtbm" clearable class="inputCompatible">
            <el-option v-for="item in zpbmList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配合部门:" label-width="72px">
          <!-- <el-input v-model="formInline.phbm" clearable class="inputCompatible"></el-input> -->
          <el-select placeholder="请选择" v-model="formInline.phbm" clearable class="inputCompatible">
            <el-option :key="item.id" :label="item.name" :value="item.id" v-for="item in zpbmList"></el-option>
          </el-select>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row class="addBtnBg">
        <el-button @click="releaseAll" type="primary" size="small" class="iconfont iconpiliangfabu">批量发布</el-button>
    </el-row>
      <!-- 考核任务总数：3
      已发布：3
      未发布：0
      上报省级：0 -->
    <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="考核年度" :show-overflow-tooltip="true" sortable min-width='90'>
            <template slot-scope="scope">
              <span>{{scope.row.year}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="评分标准" :show-overflow-tooltip="true" min-width="100" sortable>
            <template slot-scope="scope">
              <span>{{scope.row.khrwpfbz}}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" label="考核指标" :show-overflow-tooltip="true" sortable>
            <template slot-scope="scope">
              <span>{{scope.row.khzb}}</span>
            </template>
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="nyfz"
            sortable
            label="农业分值"
            :show-overflow-tooltip="true"
            min-width='95'
          ></el-table-column>
          <el-table-column
            align="center"
            prop="fnyfz"
            sortable
            label="非农业分值"
            min-width='105'
            :show-overflow-tooltip="true"
          ></el-table-column>
          <!-- <el-table-column
            align="center"
            prop="khfs"
            sortable
            label="考核方式"
            :show-overflow-tooltip="true"
            min-width='95'
          ></el-table-column> -->
          <el-table-column align="center" label="区牵头部门" :show-overflow-tooltip="true" sortable min-width='105'>
            <template slot-scope="scope">
              <span>{{scope.row.qtbm}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="区配合部门" :show-overflow-tooltip="true" sortable min-width='105'>
            <template slot-scope="scope">
              <span>{{scope.row.phbm}}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" label="更新时间" :show-overflow-tooltip="true" sortable>
            <template slot-scope="scope">
              <span>{{scope.row.updateTime}}</span>
            </template>
          </el-table-column> -->
          <el-table-column prop='statusValue' align="center" label="状态" :show-overflow-tooltip="true" sortable></el-table-column>
          <el-table-column label="操作" width="115" 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 == '2' || scope.row.status == '3')">
                <el-button
                  type="primary"
                  size="small"
                  class="iconfont iconxiugai1 operate_edit_btn"
                  @click.stop="handleUpdate(scope.row)"
                >
                </el-button>
              </el-tooltip>
              <el-tooltip class="item" effect="light" content="发布" placement="top" v-if="buttonNameList.indexOf('发布') > -1 && scope.row.status == '1'">
                <el-button
                  type="primary"
                  size="small"
                  class="iconfont iconfabu operate_issued_btn"
                  :disabled='scope.row.isDisabled'
                  @click="handleModify(scope.row,scope.$index)"
                >
                </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 managementNew from "@/api/headResponsibility/taskAudit";
import { isPeportFun,searchBranch } from "@/api/headResponsibility/selfAudit/index.js";
export default {
  data() {
    return {
      searchShowMoreBool: false,
      type: [
        { label: "未分配", value: "0" },
        { label: "已分配", value: "1" },
        { label: "已发布", value: "2" },
        { label: "上报市级", value: "3" }
      ],
      formInline: {
        year: "",
        // areaCode: sessionStorage.getItem("areaCode"),
        status:"",
        taskName:"",
        qtbm: '',
        phbm: ''
      },
      dateyear: [],
      tableList: [],
      loading: false,
      listLoading: false,
      paginationShow: true,
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      zpbmList: [],
      buttonNameList: []
    };
  },
  methods: {
    getButtonList(menuCode,userCode) {
      let codeData = {
          resourceCode:menuCode,
          userId:userCode,
      }
      authorityControl(codeData).then(res => {
         let buttonNameList = []
         res.data.forEach(item => {
             this.buttonNameList.push(item.resourceName)
         })
      })
    },
    /**
     * 查询按钮
     */
    search(type) {
      this.paginationShow = false;
      // this.handleCurrentChange(1);
      this.getTabList(type)
      this.$nextTick(function() {
        this.paginationShow = true;
      });
    },
    reset() {
      this.formInline = {
        year: "",
        // areaCode: sessionStorage.getItem("areaCode"),
        status:"",
        taskName:"",
        qtbm: '',
        phbm: ''
      }
      this.search()
    },
    getBmList() {
      searchBranch("0",localStorage.getItem('countyCode')).then(res => {
        if (res.infoCode == 200) {
          this.zpbmList = res.data;
        }else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 2000
          })
        }
      });
    },
    /**
     * 修改按钮
     */
    handleUpdate(row) {
      this.$router.push({
        path: "/accountability/taskAuditEdit",
        query: {
          id: row.id,
          type:"edit",
          markYear: this.formInline.year
        }
      });
    },

    /**
     * 表格行内点击事件
     */
    tableRowClick(row, column, event) {
      if (column.label != "操作") {
        this.handleSee(row);
      }
    },
    judgeReportFun() {//判断数据是否已批量发布
      const needData = {
        type: '80',
        year: this.formInline.year
      }
      managementNew.publishAll({year: this.formInline.year}).then(res => {
        if (res.infoCode == 200) {
          this.$message({
            type: 'success',
            message: '数据都已发布',
            duration: 2000
          });
          this.search();
        } else {
          this.$message.error(res.msg);
        }
      });
      // isPeportFun(needData)
      //   .then(res => {
      //     if (res.infoCode == 200) {
      //       if(res.data == 'yes') {//可以批量发布
      //         managementNew.publishAll({year: this.formInline.year}).then(res => {
      //           if (res.infoCode == 200) {
      //             this.$message({
      //               type: 'success',
      //               message: '数据都已发布',
      //               duration: 2000
      //             });
      //             this.search();
      //           } else {
      //             this.$message.error(res.msg);
      //           }
      //         });
      //       }else if(res.data == 'no'){
      //         this.$message({
      //           type: 'warning',
      //           message: '没有要发布的数据',
      //           duration: 2000
      //         })
      //       }
      //     }else {
      //       this.$message({
      //         type: 'error',
      //         message: res.msg,
      //         duration: 2000
      //       })
      //     }
      //   })
    },
    //批量发布
    releaseAll(){
      this.judgeReportFun()
    },
    /**
     * 表格查看按钮
     */
    handleSee(row) {
      this.$router.push({
        path: "/accountability/taskAuditEdit",
        query: {
          id: row.id,
          type:"see",
          markYear: this.formInline.year
        }
      });
    },

    /**
     * 表格删除按钮
     */
    handleDetail(row) {
      this.$confirm("您确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          management.scoringWeightDelete(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
              });
            }
          });
        })
    },

    /**
     * 表格发布按钮
     */
    handleModify(row,ind) {
      this.tableList[ind].isDisabled = true
      managementNew.publish(row.id).then(res => {
        if (res.infoCode == 200) {
          this.$message({
            message: "发布成功",
            type: "success",
            duration: 2000
          });
          this.search();
        } else {
          this.$message.error(res.msg);
          this.tableList[ind].isDisabled = false
        }
      });
    },

    /**
     * 分页大小
     */
    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
      managementNew.taskRAuditList(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;
      });
    }
  },
  created() {
    this.getButtonList(sessionStorage.getItem("resourceCode"),sessionStorage.getItem("userCode")) 
    let date = new Date();
    this.year = date.getFullYear();
    for (var i = 0; i < 10; i++) {
      this.dateyear.push(this.year);
      this.year = this.year - 1;
    }
    if(this.$route.query.markYear) {
      this.formInline.year = this.$route.query.markYear
    }
    // this.formInline.year = date.getFullYear();
    this.getBmList()
    this.search('start');
  }
};
</script>
<style lang="scss">
.managementList {
  // .itemStyle .el-form-item__label{
  //     line-height: 20px;
  // }
  .titleUseStyle {
    // position: absolute;
    // top: 0;
    font-size: 14px;
    width: 100%;
    height: 30px;
    line-height: 30px;
    color: #fff;
    font-weight: 400;
    background: #47a8b7;
    padding-left: 8px;
  }
}
</style>
