<template>
  <a-form :model="queryParams" layout="inline" ref="searchForm" style="row-gap: 8px;width: 100%;">
    <a-form-item label="行政区划" v-if="showZgXianOrgId">
      <a-tree-select
        v-model:value="queryParams.zgXianOrgId"
        :tree-data="rectifyUnits"
        show-search
        placeholder="请选择行政区划"
        search-placeholder="请输入行政区划"
        :fieldNames="{children: 'children', label: 'label', key: 'id', value: 'id'}"
        treeNodeFilterProp="label"
        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
        allow-clear />
    </a-form-item>
    <a-form-item label="月报时间" v-if="showZgXianOrgId">
      <a-month-picker v-model:value="ym" placeholder="选择年月" />
    </a-form-item>
    <a-form-item label="当月上报情况" v-if="showZgXianOrgId">
      <a-select style="margin-left: 1%;min-width: 100px;" v-model:value="queryParams.isContent" placeholder="请选择" allow-clear>
        <a-select-option value="1">已报</a-select-option>
        <a-select-option value="0">未报</a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item style="display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap;width: 150%;">
      <a-button v-if="isDuty" @click="batchUpdateDate" type="primary">批量修改预计完成整改日期</a-button>
      <a-button style="margin-left: 1%;" ref="queryButton" type="primary" :loading="loading" @click="exportFileMonthly">导出</a-button>
      <a-button style="margin-left: 1%;" ref="queryButton" type="primary" :loading="loading" @click="queryByParams">查询</a-button>
    </a-form-item>
  </a-form>
</template>

<script>
import request from '../../../utils/gw_ajax_request';
import { exportModel } from '../../../utils/gw_exportModel';
import {
  getDuChaType,
  getDuChaYears,
  getOrgTreeJson,
  getRectifyProcStates,
} from '../../PblmRectify/Common/rectify_api';
export default {
  props: {
    ywcs: {
      type: String,
      default: '',
    },
    showRectifyStatus: {
      type: Boolean,
      default: true,
    },
    showZgXianOrgId: {
      type: Boolean,
      default: true,
    },
    showInspPblmCate: {
      type: Boolean,
      default: false,
    },
    loading: {
      type: Boolean,
      default: false,
    },
    isDuty: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ym: '',
      queryParams: {
        ym: '',
        hasVedio: '',
        adCode: '',
        pblmStat: '',
        inspPblmCate: '',
        ifCasePblm: '',
        nm: '',
        rsName: '',
        adFullName: '',
        startTime: '',
        endTime: '',
        objType: '', // 为空才能不区分督查类别
        inspPblmName: '',
        dataStat: '2',
        persId: localStorage.getItem('userid'),
        villType: '0',
        pageSize: 20,
        pageNum: 1,
        pblmTotal: 0,
        pTypeCode: '', // 督查类别
        pYear: '', // 年度
        zgXianOrgId: null, // 接收单位
        zgWh: '', // 整改文号
        workflowStat: '', // 整改状态
        ywsc: this.ywcs,
        isContent: '',
      },
      rectifyProcStates: getRectifyProcStates(), // 整改流程所有流程状态
      duChaTypes: {},
      DuChaYears: getDuChaYears(),
      rectifyUnits: [],
    };
  },
  created() {
    this.getDuChaTypes();
    this.rectifyUnits = this.getOrgTreeJson();
  },
  methods: {
    batchUpdateDate() {
      this.$emit('showUpdateDate');
    },
    // 导出月报
    async exportFileMonthly() {
      const datas = JSON.stringify(this.queryParams);
      const url = '/pdcApi/dc/insp/pblm/page/reverToFXls';
      const type = 'post';
      const exportName = '月报.xls';
      const result = await exportModel(type, url, exportName, datas);
    },
    getDuChaTypes() {
      getDuChaType(this.queryParams.persId).then((res) => {
        this.duChaTypes = res.data;
      });
    },
    getOrgTreeJson() {
      getOrgTreeJson().then((res) => {
        const jsonArray = [];
        const jsonStr = JSON.stringify(res.data);
        jsonArray.push(res.data);
        this.rectifyUnits = jsonArray;
      });
    },
    async queryByParams() {
      this.queryParams.ym = Date.parse(new Date(this.ym));
      await this.$emit('queryByParams', {
        queryParams: this.queryParams,
      });
    },
    async exportFile() {
      const datas = JSON.stringify(this.queryParams);
      const url = '/pdcApi/dc/insp/pblm/page/rever/export';
      // let url = "/pdcApi/dc/insp/pblm/page/rever/exportxian";
      const type = 'post';
      const exportName = '问题查看.xls';
      const result = await exportModel(type, url, exportName, datas);
      /*  request.post( '/pdcApi/dc/insp/pblm/page/rever/export', this.queryParams).then(res => {
              let blob = new Blob([res], {type: "application/vnd.ms-excel"});
              let objectUrl = URL.createObjectURL(blob);
                // window.location.href = objectUrl;
               const link = document.createElement('a');
                link.href = objectUrl;
                link.download = '问题查看.xls'; // 文件名
                link.click(); // 下载文件
                URL.revokeObjectURL(objectUrl); // 释放内存
        }); */
      // return request({
      //     url:
      //     method: 'POST',
      //     data: data,
      //     headers:{
      //         responseType: 'blob'
      //     }
      // })
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children,
      };
    },
  },
};
</script>

<style lang='less' scoped>

</style>
