<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 label="地区:">
        <!-- <el-input v-model="formInline.areaCodeName" placeholder="请输入地区"></el-input> -->
        <el-select v-model="formInline.areaCode" placeholder="请选择地区" clearable class="inputCompatible">
            <el-option :key="item.dictCode" :label="item.dictName" :value="item.dictCode" v-for="item in areaCodeOption"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="类型:">      
          <el-select v-model="formInline.areaCodeType" placeholder="请选择类型" clearable class="inputCompatible">
            <el-option :key="item.value" :label="item.label" :value="item.value" v-for="item in areaCodeTypeOption"></el-option>
          </el-select>
      </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" v-if="buttonNameList.indexOf('发布') > -1">
        <el-button type="primary" size="small" class="iconfont iconfabu" @click="fabuIssue" >批量发布</el-button>
    </el-row>
    <!-- @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 prop='year' align="center" label="考核年度" sortable :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop='areaCodeName' align="center" label="地区" sortable :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="center" label="类型" sortable :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{scope.row.areaCodeType == 'fnyq' ? '非农业区' : '农业区'}}</span>
        </template>
      </el-table-column>
      <el-table-column  align="center" label="总分" sortable :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{scope.row.areaCodeType == 'fnyq' ? scope.row.fnyfz : scope.row.nyfz}}</span>
        </template>
      </el-table-column>
      <el-table-column prop='zhdf' align="center" label="综合得分" sortable :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop='pj' align="center" label="评级" sortable :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop='qj_zpdf' align="center" label="自评得分" sortable :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop='sj_khdf' align="center" label="考核得分" sortable :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop='sj_ccdf' align="center" label="抽查得分" sortable :show-overflow-tooltip="true"></el-table-column>
      <el-table-column  align="center" label="发布状态" sortable :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{scope.row.resultstatus == '1' ? '已发布' : '未发布'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" 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="scope.row.resultstatus !== 1 && buttonNameList.indexOf('发布') > -1">
            <el-button
              type="primary"
              size="small"
              class="iconfont iconfabu operate_edit_btn"
              @click.stop="handleSend(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>
  </div>
</template>
<script>
import * as management from "@/api/headResponsibility/assessmentTaskQuery";
import { authorityControl } from "@/api/login/login"; 
import { userAreaCodeTree } from "@/api/login/login"
export default {
  data() {
    return {
      selList: [],
      formInline: {
        year:"",
        // type:"2",
        // source: "20",
        areaCodeName: '', 
        areaCodeType: ''
      },
      dateyear: [],
      tableList: [],
      loading: false,
      listLoading: false,
      paginationShow: true,
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      buttonNameList:[], 
      areaCodeOption: [],
      areaCodeTypeOption: [
        {
          value: 'nyq',
          label: '农业区'
        },{
          value: 'fnyq',
          label: '非农业区'
        }
      ]
    };
  },
  methods: {
    // 时间格式化函数
    dateFormat(time) {
      if (time) {
        var day = time.split("T");
        //获取日期部分的年月日
        var days = day[0].split("-");
        return days[0] + "-" + days[1] + "-" + days[2];
      } else {
        return "";
      }
    },
    /**
     * 查询按钮
     */
    search(type) {
      this.paginationShow = false;
      // this.handleCurrentChange(1);
      this.getTabList(type)
      this.$nextTick(function() {
        this.paginationShow = true;
      });
    },
    reset() {
      this.formInline = {
        year:"",
        // type:"2",
        // source: "20",
        areaCode:'',
        areaCodeType:''
      }
      this.search()
    },
    /**
     * 表格行内点击事件
     */
    tableRowClick(row, column, event) {
      if (event.label != "操作") {
        this.handleSee(row);
      }
    },
    /**
     * 表格查看按钮
     */
    handleSee(row) {
        this.$router.push({
            path:"/accountability/assessmentQuerySee",
            query:{
                bilityid:row.bilityid,
                areaCode: row.areaCode,
                year: row.year,
                markYear: this.formInline.year,
                isSelected: row.sj_ccdf,
                from: 'cityLook'
            }
        })
    },
    handleSend(row) {
      this.$confirm("是否确认发布？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let data = {
          "areaCode": row.areaCode,
          "resultStatus": 1,
          "year": row.year,
        }
        management.assessmentTaskQuerySend(data)
          .then(response => {
            if (response.infoCode == 200) {
              this.getTabList();
              this.$message({
                showClose: true,
                message: "发布成功！",
                type: "success"
              });
            }
          })
          .catch(function(error) {
            console.log(error);
          });
      });
    },
    fabuIssue() {
      this.$confirm("是否确认发布？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let data = {          
          "resultStatus": 1,
        }
        management.assessmentTaskQuerySend(data)
          .then(response => {
            if (response.infoCode == 200) {
              this.getTabList();
              this.$message({
                showClose: true,
                message: "发布成功！",
                type: "success"
              });
            }
          })
          .catch(function(error) {
            console.log(error);
          });
      });
    },
    getButtonList(menuCode,userCode) {
        let codeData = {
            resourceCode:menuCode,
            userId:userCode,
        }
        authorityControl(codeData).then(res => {
           let buttonNameList = []
           res.data.forEach(item => {
               this.buttonNameList.push(item.resourceName)
           })
        })
      },
    /**
     * 分页大小
     */
    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
      if(!this.formInline.areaCodeType){
        this.formInline.areaCodeType = null
      }
      management.collectList(this.formInline, this.page)
        .then(res => {
            if (res.infoCode==200) {
                this.tableList=res.data.list
                this.page.total=res.data.total
            } else {
              this.$message({
                message: res.msg,
                type: "error",
                duration: 2000
              });
            }
            this.loading = false;
        });
    },

      /**
       * 查询可选年分
       */
      findByYear(type){
        management.responsibilityFindByYear(type).then(res => {
            if (res.infoCode == 200) {
              this.dateyear = res.data;
              // if(!this.$route.query.markYear && this.$route.query.markYear !='') {//没这个参数时
              //   if(this.dateyear !=null){
              //     // this.formInline.year= this.dateyear[0]
              //   }
              // }
            }else {
              this.$message({
                message: res.msg,
                type: "error",
                duration: 2000
              });
            }
        });
      }
  },
  created() {
    // if(this.$route.query.markYear || this.$route.query.markYear == '') {
    //   this.formInline.year = this.$route.query.markYear
    // }
    userAreaCodeTree().then(res => {
      this.areaCodeOption = res.data.subDataDictionarys
    })
    this.getButtonList(sessionStorage.getItem("resourceCode"),sessionStorage.getItem("userCode")) 
    this.search('start');
    this.findByYear('page');
  }
};
</script>
<style lang="scss">

</style>
