<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年份" prop="year">
        <el-select v-model="queryParams.year" placeholder="请选择年份" >
          <el-option
            v-for="year in yearList"
            :key="year"
            :label="year"
            :value="year"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="月份" prop="month">
        <el-select v-model="queryParams.month" placeholder="请选择月份" clearable>
          <el-option label="01" value="01"></el-option>
          <el-option label="02" value="02"></el-option>
          <el-option label="03" value="03"></el-option>
          <el-option label="04" value="04"></el-option>
          <el-option label="05" value="05"></el-option>
          <el-option label="06" value="06"></el-option>
          <el-option label="07" value="07"></el-option>
          <el-option label="08" value="08"></el-option>
          <el-option label="09" value="09"></el-option>
          <el-option label="10" value="10"></el-option>
          <el-option label="11" value="11"></el-option>
          <el-option label="12" value="12"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="州市" prop="cityCode">
        <el-select v-model="queryParams.cityCode" placeholder="请选择州市" clearable>
          <el-option
            v-for="region in regionOptions"
            :key="region.cityShortName"
            :label="region.cityShortName"
            :value="region.code"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="高价值类型" label-width="100px"  >
        <el-checkbox-group v-model="checkedHighValues" @change="handleCheckedCitiesChange">
          <el-checkbox v-for="t in highValueList" :label="t.id" :key="t.id">{{t.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item label="申请人类型"  label-width="100px"  >
        <template>
          <el-checkbox-group v-model="checkedApplicantTypes"  >
            <el-checkbox v-for="t in applicantTypeList" :label="t.id" :key="t.id" >{{t.name}}</el-checkbox>
          </el-checkbox-group>
        </template>
      </el-form-item>
      <el-form-item style="margin-left: 20px;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置/刷新</el-button>
        <el-button type="primary"  size="mini" @click="handleExport"
          >导出列表</el-button
        >
      </el-form-item>
    </el-form>

    <div id="scrollTable">
      <el-table v-loading="loading" :data="rzgClassifiedStatisticsInfoList" :max-height="maxHeight" :header-cell-style="{background:'#2a8eed',color:'#fafcfe',}">
        <el-table-column label="地区" align="center" width="100" fixed="left">
          <template slot-scope="scope">
            <span>{{scope.row.areaName}}</span>
          </template>
        </el-table-column>
        <el-table-column :label="hearderTitle" align="center">
          <el-table-column   v-for="(item1,index1) in hearderTitle3" :label="item1.name" align="center" :key="index1"  v-if="showApplicantTypeObj[item1.code]"   >
            <el-table-column  label="战略性新兴产业" align="center" v-if="showHighValueTypeObj.hvSeisPress">
              <template slot-scope="scope">
                <span>{{scope.row.items[item1.code].hvSeisPress}}</span>
              </template>
            </el-table-column>

            <el-table-column  label="维持年限超10年" align="center"  v-if="showHighValueTypeObj.hvKeep10Press">
              <template slot-scope="scope">
                <span>{{scope.row.items[item1.code].hvKeep10Press}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="在海外有同族专利" align="center" v-if="showHighValueTypeObj.hvHaveOverseasPress">
              <template slot-scope="scope">
                <span>{{scope.row.items[item1.code].hvHaveOverseasPress}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="存在质押记录" align="center"  v-if="showHighValueTypeObj.hvHighPledgePress" >
              <template slot-scope="scope">
                <span>{{scope.row.items[item1.code].hvHighPledgePress}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="获得国家科技奖或者中国专利奖" align="center"  v-if="showHighValueTypeObj.hvTechnologyAwardPress">
              <template slot-scope="scope">
                <span>{{scope.row.items[item1.code].hvTechnologyAwardPress}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="高价值发明专利有效量合计" align="center" v-if="showHighValueTypeObj.totalCnt">
              <template slot-scope="scope">
                <span>{{scope.row.items[item1.code].totalCnt}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="同比增长" align="center" >
              <template slot-scope="scope">
                <span>{{scope.row.items[item1.code].growth}}</span>
              </template>
            </el-table-column>
          </el-table-column>






        </el-table-column>
      </el-table>
    </div>


  </div>
</template>

<script>
import { listRzgClassifiedStatisticsInfo, getRzgClassifiedStatisticsInfo, delRzgClassifiedStatisticsInfo, addRzgClassifiedStatisticsInfo, updateRzgClassifiedStatisticsInfo ,getHeaderList,getYearList,getRegionList } from "@/api/highval/rzgClassifiedStatisticsInfo";
import {getHighValues,getApplicantTypes,getAllHighValueIdStr,getAllApplicantTypeIdStr} from "@/api/staticdata";
export default {
  name: "RzgClassifiedStatisticsInfo",
  data() {
    return {

      maxHeight: 0,

      checkedHighValues: [],
      highValueList:[],

      showHighValueTypeObj:{
        hvSeisPress:true,hvKeep10Press:true,hvHaveOverseasPress:true,hvHighPledgePress:true,hvTechnologyAwardPress:true,totalCnt:true
      },

      checkedApplicantTypes: [],
      applicantTypeList:[],
      showApplicantTypeObj:{
        UC:true,SRI:true,IME:true,OT:true,PE:true
      },

      regionOptions: [],
      headerList: [],
      // 查询年份数据
      yearList: [],
      hearderTitle:'',
      hearderTitle2:'专利类型-全部',

      hearderTitle3:[],


      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 高价值专利-分类统计表格数据
      rzgClassifiedStatisticsInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
          pageNum: 1,
          pageSize: 10,
        cityCode: null,
        cityName: null,
        year:'2022',
        month: '',
        sort: null,
        industryEmergIndustry: null,
        overseasCongenerPatent: null,
        maintenanceTenYear: null,
        pledgeRecord: null,
        nationalScienceAward: null,
        effectiveTotal: null,
        growth: null,
        applicantType: null,
        highValueType: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.highValueList=this.getHighValues3();
    this.applicantTypeList=getApplicantTypes();
    this.hearderTitle3=getApplicantTypes();

    this.checkedHighValues=[];
    this.checkedApplicantTypes=[];

    for(let i=0;i<this.highValueList.length;i++){
      //console.log('add:'+this.HighValueList[i].name);
      this.checkedHighValues.push(this.highValueList[i].id);
    }
    for(let i=0;i<this.applicantTypeList.length;i++){
      this.checkedApplicantTypes.push(this.applicantTypeList[i].id);
    }

    this.showApplicantTypeTitle();
    this.showHighValueTitle();

    this.getRegion();
    this.getHeader(getAllHighValueIdStr(),getAllApplicantTypeIdStr());
    this.getYear();
    this.getList();
    this.showApplicantTypeTitle();
    this.$nextTick(()=>{
      this.maxHeight = window.innerHeight - 200;//300是页面中除了表格外其他组件的高度
    })
  },
  methods: {

    getHighValues3() {
      let types = [{
        id: '1',
        name: '战略性新兴产业',code:'hvSeisPress'
      },{id: '2',
        name: '维持年限超10年',code:'hvKeep10Press'
      },{
        id: '3',
        name: '在海外有同族专利权',code:'hvHaveOverseasPress'
      },{
        id: '4',
        name: '获得较高质押融资金额',code:'hvHighPledgePress'
      },{
        id: '5',
        name: '获得国家科技奖或中国专利奖',code:'hvTechnologyAwardPress'
      }];
      return types;
    },

    getHighValues2() {
      let types = [{
        id: '1',
        name: '战略性新兴产业',code:'hvSeisPress'
      },{id: '2',
        name: '维持年限超10年',code:'hvKeep10Press'
      },{
        id: '3',
        name: '在海外有同族专利权',code:'hvHaveOverseasPress'
      },{
        id: '4',
        name: '获得较高质押融资金额',code:'hvHighPledgePress'
      },{
        id: '5',
        name: '获得国家科技奖或中国专利奖',code:'hvTechnologyAwardPress'
      },{
        id: '6',
        name: '高价值发明专利有效量合计',code:'totalCnt'
      },{
        id: '7',
        name: '同比增长',code:'growth'
      }];
      return types;
    },




    /** 获取省市列表 */
    getRegion() {
      let params = {};
      getRegionList(params).then(response => {
        this.regionOptions = response.data;
      });
    },
    getCheckedHighValueIdStr(){
      let ids='';
      //console.log('lenss:'+this.checkedHighValues.length);
      for(let i=0;i<this.checkedHighValues.length;i++){
        if(i>0){
          ids=ids+',';
        }
        ids=ids+this.checkedHighValues[i];
      }
      //console.log('idsgetCheckedHighValueIdStr:'+ids);
      return ids;
    },
    getCheckedApplicantTypeIdStr(){
      let ids='';
      for(let i=0;i<this.checkedApplicantTypes.length;i++){
        if(i>0){
          ids=ids+',';
        }
        ids=ids+this.checkedApplicantTypes[i];
      }
      return ids;
    },
    allHighValueSelect(){
      this.highValueList=this.getHighValues3();
      this.checkedHighValues=[];
      for(let i=0;i<this.highValueList.length;i++){
        this.checkedHighValues.push(this.highValueList[i].id);
      }
    },

    showHighValueTitle(){
      //如果一个都被选就自动全选
      if(this.checkedHighValues.length==0){
        this.allHighValueSelect();
      }
      let allTypes=this.getHighValues2();
      for(let i=0;i<allTypes.length;i++){
        let id=allTypes[i].id;
        if(this.checkedHighValues.indexOf(id)>=0){
          this.showHighValueTypeObj[allTypes[i].code]=true;
        }else{
          this.showHighValueTypeObj[allTypes[i].code]=false;
        }
      }
      if(this.checkedHighValues.length==1){
        this.showHighValueTypeObj.totalCnt=false;//如果只选择一个，就不显示合计
      }else{
        this.showHighValueTypeObj.totalCnt=true;
      }
    },

    allApplicantTypeSelect(){
      this.applicantTypeList=getApplicantTypes();
      this.checkedApplicantTypes=[];
      for(let i=0;i<this.applicantTypeList.length;i++){
        this.checkedApplicantTypes.push(this.applicantTypeList[i].id);
      }
    },

    showApplicantTypeTitle(){
      //如果一个都被选就自动全选
      if(this.checkedApplicantTypes.length==0){
        this.allApplicantTypeSelect();
      }
      let allTypes=getApplicantTypes();
      for(let i=0;i<allTypes.length;i++){
        let id=allTypes[i].id;
        if(this.checkedApplicantTypes.indexOf(id)>=0){
          this.showApplicantTypeObj[allTypes[i].code]=true;
        }else{
          this.showApplicantTypeObj[allTypes[i].code]=false;
        }
      }
    },
    handerMethod ({ row, column, rowIndex, columnIndex }) {
      // if (rowIndex === 0) {
      //     if (column.label == '地区' || column.label == '专利类型') {
      //         this.$nextTick(() => {
      //             var _c = document.getElementsByClassName(column.id);
      //             document.getElementsByClassName(column.id)[0].setAttribute('rowSpan', 2);
      //         });
      //         return column;
      //     }
      // } else if (rowIndex === 1) {
      //     if (column.label == '地区' || column.label == '专利类型') {
      //         return { display: 'none' };
      //     }
      // }
      // if (column[0].level == 1) {
      //     column[0].rowSpan = 2
      //     column[1].rowSpan = 0
      //     if (rowIndex === 1) {
      //         return { display: 'none' }
      //     }
      // }
    },


    /** 获取动态表头 */
    getHeader(highValues,applicantTypes) {
      let head='统计时期'+this.queryParams.year;
      if(this.queryParams.month){
        head=head+'-'+this.queryParams.month;
      }
      this.hearderTitle=head;
      this.queryParams.startDate = this.queryParams.year;
      getHeaderList(highValues,applicantTypes).then(response => {
        this.headerList = response.data;
      });
    },
    /** 获取查询年份列表 */
    getYear() {
      getYearList().then(response => {
        this.yearList = response.data;
      });
    },


    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      //console.log('length:'+checkedCount);
    },

    /** 查询高价值专利-分类统计列表 */
    getList() {
      this.loading = true;
      this.queryParams.highValueTypes=this.getCheckedHighValueIdStr();
      this.queryParams.applicantTypes=this.getCheckedApplicantTypeIdStr();
      listRzgClassifiedStatisticsInfo(this.queryParams).then(response => {
        this.rzgClassifiedStatisticsInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cityCode: null,
        cityName: null,
        year: null,
        month: null,
        sort: null,
        industryEmergIndustry: null,
        overseasCongenerPatent: null,
        maintenanceTenYear: null,
        pledgeRecord: null,
        nationalScienceAward: null,
        effectiveTotal: null,
        growth: null,
        applicantType: null,
        highValueType: null,
        remark: null,
        delFlag: null,
        status: 0,
        createTime: null,
        createBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getHeader(this.getCheckedHighValueIdStr(),this.getCheckedApplicantTypeIdStr());
      this.queryParams.pageNum = 1;
      this.getList();
      this.showApplicantTypeTitle();
      this.showHighValueTitle();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加高价值专利-分类统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRzgClassifiedStatisticsInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改高价值专利-分类统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRzgClassifiedStatisticsInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRzgClassifiedStatisticsInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除高价值专利-分类统计编号为"' + ids + '"的数据项？').then(function() {
        return delRzgClassifiedStatisticsInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('highval/rzgClassifiedStatisticsInfo/export', {
        ...this.queryParams
      }, `rzgClassifiedStatisticsInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
