<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="请选择月份" >
          <el-option label="1" value="1"></el-option>
          <el-option label="2" value="2"></el-option>
          <el-option label="3" value="3"></el-option>
          <el-option label="4" value="4"></el-option>
          <el-option label="5" value="5"></el-option>
          <el-option label="6" value="6"></el-option>
          <el-option label="7" value="7"></el-option>
          <el-option label="8" value="8"></el-option>
          <el-option label="9" value="9"></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="请选择州市" >
          <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="专利类型"  >
        <el-checkbox-group v-model="checkedPatentTypes" >
          <el-checkbox v-for="t in patentTypeList" :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 label="IPC" prop="queryIpc">
        <el-select v-model="queryParams.queryIpc" placeholder="请选择IPC" >
          <el-option label="部" value="ipc"></el-option>
          <el-option label="大类" value="subIpc"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <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-form-item>
    </el-form>


    <div id="scrollTable">
      <el-table v-loading="loading" :data="rzlIpcStatisticsInfoList" :max-height="maxHeight" :header-cell-style="{background:'#2a8eed',color:'#fafcfe',}">
        <el-table-column :label="bigType" align="center" width="100" fixed="left">
          <template slot-scope="scope">
            <span>{{scope.row.type}}</span>
          </template>
        </el-table-column>
        <el-table-column :label="smallType" align="center" width="100" fixed="left">
          <template slot-scope="scope">
            <span>{{scope.row.typeName}}</span>
          </template>
        </el-table-column>

        <el-table-column  :label="hearderTitle" align="center" >

        <el-table-column  label="发明" align="center" v-if="this.showPatentTypeObj.invention">

          <el-table-column  label="数量" align="center" >
            <template slot-scope="scope">
              <span>{{scope.row.datas['invention'].number}}</span>
            </template>
          </el-table-column>
          <el-table-column  label="占比" align="center" >
            <template slot-scope="scope">
              <span>{{scope.row.datas['invention'].proportion}}</span>
            </template>
          </el-table-column>
          <el-table-column  label="大专院校" align="center"  v-if="this.showApplicantTypeObj.UC">
            <template slot-scope="scope">
              <span>{{scope.row.datas['invention'].higherSchool}}</span>
            </template>
          </el-table-column>
          <el-table-column  label="科研机构" align="center"  v-if="this.showApplicantTypeObj.SRI">
            <template slot-scope="scope">
              <span>{{scope.row.datas['invention'].researchInstitute}}</span>
            </template>
          </el-table-column>
          <el-table-column  label="企业" align="center"  v-if="this.showApplicantTypeObj.IME">
            <template slot-scope="scope">
              <span>{{scope.row.datas['invention'].enterprise}}</span>
            </template>
          </el-table-column>
          <el-table-column  label="机关团体" align="center"  v-if="this.showApplicantTypeObj.OT">
            <template slot-scope="scope">
              <span>{{scope.row.datas['invention'].institutions}}</span>
            </template>
          </el-table-column>
          <el-table-column  label="个人" align="center"  v-if="this.showApplicantTypeObj.PE" >
            <template slot-scope="scope">
              <span>{{scope.row.datas['invention'].individual}}</span>
            </template>
          </el-table-column>
        </el-table-column>

          <el-table-column  label="实用新型" align="center" v-if="this.showPatentTypeObj.modelUtility" >

            <el-table-column  label="数量" align="center" >
              <template slot-scope="scope">
                <span>{{scope.row.datas['modelUtility'].number}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="占比" align="center" >
              <template slot-scope="scope">
                <span>{{scope.row.datas['modelUtility'].proportion}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="大专院校" align="center" v-if="this.showApplicantTypeObj.UC" >
              <template slot-scope="scope">
                <span>{{scope.row.datas['modelUtility'].higherSchool}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="科研机构" align="center" v-if="this.showApplicantTypeObj.SRI">
              <template slot-scope="scope">
                <span>{{scope.row.datas['modelUtility'].researchInstitute}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="企业" align="center" v-if="this.showApplicantTypeObj.IME">
              <template slot-scope="scope">
                <span>{{scope.row.datas['modelUtility'].enterprise}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="机关团体" align="center" v-if="this.showApplicantTypeObj.OT">
              <template slot-scope="scope">
                <span>{{scope.row.datas['modelUtility'].institutions}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="个人" align="center" v-if="this.showApplicantTypeObj.PE">
              <template slot-scope="scope">
                <span>{{scope.row.datas['modelUtility'].individual}}</span>
              </template>
            </el-table-column>
          </el-table-column>

        </el-table-column>


      </el-table>
    </div>




  </div>
</template>

<script>
import { listRzlIpcStatisticsInfo, getRzlIpcStatisticsInfo, delRzlIpcStatisticsInfo, addRzlIpcStatisticsInfo, updateRzlIpcStatisticsInfo,getHeaderList,getHeaderList2,getYearList,getRegionList  } from "@/api/report/rzlIpcStatisticsInfo";
import {getPatentTypes,getPatentType,getApplicantTypes,getAllPatentTypeIdStr,getAllApplicantTypeIdStr} from "@/api/staticdata";

export default {
  name: "RzlIpcStatisticsInfo",
  data() {
    return {

      maxHeight: 0,
      checkedPatentTypes: [],
      patentTypeList:[],
      showPatentTypeObj:{
        invention:true,modelUtility:true,appearance:true
      },
      showApplicantTypeObj:{
        UC:true,SRI:true,IME:true,OT:true,PE:true
      },
      checkedApplicantTypes: [],
      applicantTypeList:[],
      regionOptions: [],
      headerList: [],
      headerList2:[],
      // 查询年份数据
      yearList: [],
      hearderTitle:'',
      hearderTitle2:'专利类型-全部',

      bigType:'部类',
      smallType:'部类名',

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专利有效-主IPC统计表格数据
      rzlIpcStatisticsInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
          pageNum: 1,
          pageSize: 10,
        cityCode: null,
        cityName: null,
        year: '',
        month: '4',
        patentType: null,
        applicantType: null,
        higherSchool: null,
        researchInstitute: null,
        enterprise: null,
        institutions: null,
        individual: null,
        number: null,
        proportion: null,
        categoryNumber: null,
        categoryName: null,
        departClass: null,
        queryIpc:'ipc',
        departClassName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    //this.patentTypeList=getPatentTypes();

    this.patentTypeList= [{
      id: '1',
      name: '发明', code:'invention'
    },{id: '2',
      name: '实用新型',code:'modelUtility'
    }];

    this.allApplicantTypeSelect();
    this.allPatentTypeSelect();

    this.showTypeName();

    // this.applicantTypeList=getApplicantTypes();
    // this.checkedPatentTypes=[];
    // this.checkedApplicantTypes=[];

    // for(let i=0;i<this.patentTypeList.length;i++){
    //   this.checkedPatentTypes.push(this.patentTypeList[i].id);
    // }
    // for(let i=0;i<this.applicantTypeList.length;i++){
    //   this.checkedApplicantTypes.push(this.applicantTypeList[i].id);
    // }

    this.getRegion();
    this.getHeader(getAllPatentTypeIdStr(),getAllApplicantTypeIdStr());
    this.getYear();
    this.getList();

    this.showPatentTypeTitle();
    this.showApplicantTypeTitle();

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


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

    },

    showTypeName(){
      if(this.queryParams.departClass=='2'){
        this.bigType='大类号';
        this.smallType='大类名';
      }else{
        this.bigType='部类';
        this.smallType='部类名';
      }
    },

    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;
        }
      }
    },
    showPatentTypeTitle(){
      // hearderTitle2
      let title='专利类型-';

      if(this.checkedPatentTypes.length==0){
        this.allPatentTypeSelect();
      }




      let allTypes=this.patentTypeList;
      for(let i=0;i<allTypes.length;i++){
        let id=allTypes[i].id;
        if(this.checkedPatentTypes.indexOf(id)>=0){
          this.showPatentTypeObj[allTypes[i].code]=true;
        }else{
          this.showPatentTypeObj[allTypes[i].code]=false;
        }
      }
      if(this.patentTypeList.length==this.checkedPatentTypes.length){
        title=title+'全部';
        this.hearderTitle2=title;
        return;
      }

      for(let i=0;i<this.checkedPatentTypes.length;i++){
        if(i>0){
          title=title+',';
        }
        title=title+getPatentType(this.checkedPatentTypes[i]);
      }
      this.hearderTitle2=title;
      return;

    },


    /** 获取省市列表 */
    getRegion() {
      let params = {};
      getRegionList(params).then(response => {
        this.regionOptions = response.data;
      });
    },
    getCheckedPatentTypeIdStr(){
      let ids='';
      for(let i=0;i<this.checkedPatentTypes.length;i++){
        if(i>0){
          ids=ids+',';
        }
        ids=ids+this.checkedPatentTypes[i];
      }
      //console.log('idsgetCheckedPatentTypeIdStr:'+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;
    },


    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(patentTypes,applicantTypes) {
      let head='统计时期';
      if(this.queryParams.year){
        head=head+'-'+this.queryParams.year;
      }
      if(this.queryParams.month){
        head=head+'-'+this.queryParams.month;
      }
      this.hearderTitle=head;
      this.queryParams.startDate = this.queryParams.year;
      //getHeaderList(patentTypes,applicantTypes).then(response => {
      //  this.headerList = response.data;
      //});
      this.headerList2=getHeaderList2(patentTypes,applicantTypes);
    },
    /** 获取查询年份列表 */
    getYear() {
      getYearList().then(response => {
        this.yearList = response.data;
      });
    },

    /** 查询专利有效-主IPC统计列表 */
    getList() {
      this.loading = true;
      listRzlIpcStatisticsInfo(this.queryParams).then(response => {
        this.rzlIpcStatisticsInfoList = 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,
        patentType: null,
        applicantType: null,
        higherSchool: null,
        researchInstitute: null,
        enterprise: null,
        institutions: null,
        individual: null,
        number: null,
        proportion: null,
        categoryNumber: null,
        categoryName: null,
        departClass: null,
        departClassName: null,
        remark: null,
        delFlag: null,
        status: 0,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getHeader(this.getCheckedPatentTypeIdStr(),this.getCheckedApplicantTypeIdStr());
      this.queryParams.pageNum = 1;
      this.getList();
      this.showTypeName();
      this.showPatentTypeTitle();
      this.showApplicantTypeTitle();
    },
    /** 重置按钮操作 */
    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 = "添加专利有效-主IPC统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRzlIpcStatisticsInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改专利有效-主IPC统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRzlIpcStatisticsInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRzlIpcStatisticsInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除专利有效-主IPC统计编号为"' + ids + '"的数据项？').then(function() {
        return delRzlIpcStatisticsInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('report/rzlIpcStatisticsInfo/export', {
        ...this.queryParams
      }, `rzlIpcStatisticsInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
