<template>
  <el-card shadow="never"
           v-loading="loading"
           class="aui-card--fill">
    <div class="mod-news__news}">
      <el-form :inline="true"
               :model="dataForm"
               :rules="dataRule"
               ref="dataForm"
               :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
        <el-row>
          <el-form-item prop="allDeptIdsShow"
                        label="所属机构">
            <el-cascader v-model="dataForm.allDeptIdsShow"
                         :options="options"
                         :props="{ multiple: true }"
                         clearable
                         collapse-tags></el-cascader>
          </el-form-item>
        </el-row>
        <el-form-item label="时间筛选类型">
          <template>
            <el-radio v-model="timeFlag"
                      label="1">有效时间</el-radio>
            <el-radio v-model="timeFlag"
                      label="0">截止时间</el-radio>
          </template>
        </el-form-item>
        <el-row>
          <el-form-item prop="newsReleaseStartTime"
                        v-if="this.timeFlag==1"
                        label="有效时间">
            <el-date-picker @change='setRegistTime'
                            v-model="time"
                            value-format="yyyy-MM-dd"
                            type="daterange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="endTime"
                        v-if="this.timeFlag==0"
                        label="截止时间">
            <el-date-picker v-model="dataForm.endTime"
                            type="date"
                            placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-row>
        <p>导出内容包括：</p>
        居民数(待认证居民+认证失败+认证通过+初次扫码进入网格但未认证)&nbsp;&nbsp;
        党员数(已认证党员数)&nbsp;&nbsp;
        新闻发布数 议题数 项目数 项目结案数
        社群数 社群成员数 社群话题数
        <br>
        <br>
        <el-row>
          <el-form-item label-width="500px">
            <el-button type="primary"
                       @click="this.exportHandlee">{{ $t('export') }}</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </el-card>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
export default {
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/analysis/user/page/dataRank',
        getDataListIsPage: true,
        deleteURL: '/news/news',
        deleteIsBatch: true,
        exportURL: '/analysis/user/export/exportOperationData'
      },
      dataForm: {
        id: '',
        operationStartTime: '',
        operationEndTime: '',
        allDeptIdsShow: [],
        endTime: ''
      },
      time: [],
      streetList: [],
      communityList: [],
      gridList: [],
      options: [],
      categorys: [],
      loading: false,
      timeFlag: '1'
    }
  },
  activated () {
  },
  created () {
    this.getDeptInfoList()
  },
  computed: {
    dataRule () {
      return {
        allDeptIdsShow: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        newsTitle: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        newsCateroryId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    setRegistTime () {
      this.dataForm.operationStartTime = this.time[0]
      this.dataForm.operationEndTime = this.time[1]
    },
    getDeptInfoList () {
      this.$http.get(`/sys/user/deptOptions/getStreetByLoginUser`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      }).catch(() => { })
    },
    export () {
      this.$http['post'](`/analysis/user/export/exportOperationData`, this.dataForm).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      }).catch(() => { })
    },
    exportHandlee () {
      this.loading = true
      this.$http.post('/analysis/user/export/exportOperationData', this.dataForm, { responseType: 'arraybuffer' }).then((res) => {
        this.loading = false
        // if (res.code !== 0) {
        //   return this.$message.error(res.msg)
        // }
        let fileName = '运营数据报表.xls'
        let blob = new Blob([res.data], { type: 'application/x-xls' })
        if (window.navigator.msSaveOrOpenBlob) {
          navigator.msSaveBlob(blob, fileName)
        } else {
          var link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob)
          link.download = fileName
          link.click()
          window.URL.revokeObjectURL(link.href)
        }
      })
    }
  },
  components: {
  },
  mixins: [mixinViewModule],
  watch: {
    'timeFlag': function (val) {
      if (val == 0) {
        this.dataForm.operationStartTime = ''
        this.dataForm.operationEndTime = ''
        this.time = []
      } else {
        this.dataForm.endTime = ''
      }
    }
  }
}
</script>
