<template>
    <div>
        <form-option>
            <el-row style="padding-bottom: 15px;">
              <el-col :span="12"><el-button type="primary" @click="handleSetting">规则设置</el-button></el-col>
              <el-col :span="12" style="text-align: right;">
                <el-date-picker
                    v-model="year"
                    type="year"
                    placeholder="选择年">
                </el-date-picker>
                <el-button type="primary" @click="handleQuery" style="margin-left: 5px;">查询</el-button>
                <el-button type="success" @click="handleExport" style="margin-left: 5px;">导出</el-button>
              </el-col>
            </el-row>
        </form-option>
    </div>
</template>

<script>
import FormOption from '@/component/FormOption'
import ChooseResource from '@/component/ChooseResource'

import { getOrg, getUserByOrg } from '@/api/base'
import { jobMixin } from '@/mixin/job'
import { resourceMixin } from '@/mixin/resource'
import {DateTime} from "luxon";
import {dateFormat} from "@/utils/tool";

export default {
    components: {
        FormOption,
    },
    data () {
        return {
            year: ''
        }
    },
    props: {
        enforce_org: {
            type: Object
        }
    },
    mounted () {
    },
    methods: {
        handleQuery() {
          let queryString = ""
          let expressions = []
          if (this.year) {
            expressions.push("task_month" + "==" + "'" + dateFormat('YYYY', new Date(this.year)) + "*'")
          }
          if (this.enforce_org.enforce_org_id) {
            expressions.push("enforce_org_id" + "==" + "'" + this.enforce_org.enforce_org_id + "'")
          }
          if (expressions.length !== 0) {
            queryString = expressions.reduce(function(previousValue, currentValue, index, array) {
              return previousValue + ";" + currentValue
            })
          }
          this.$emit('query', queryString)
        },
        handleExport() {
          let queryString = ""
          let expressions = []
          if (this.year) {
            expressions.push("task_month" + "==" + "'" + dateFormat('YYYY', new Date(this.year)) + "*'")
          }
          if (this.enforce_org.enforce_org_id) {
            expressions.push("enforce_org_id" + "==" + "'" + this.enforce_org.enforce_org_id + "'")
          }
          if (expressions.length !== 0) {
            queryString = expressions.reduce(function(previousValue, currentValue, index, array) {
              return previousValue + ";" + currentValue
            })
          }
          this.$emit('export', queryString)
        },
        handleSetting() {
          this.$router.push({
            path: '/random/random/rule/setting/form'
          })
        },
        clearYear() {
          this.year = ''
        }
    }
}
</script>
