<template>
    <div>
        <form-option title="专项检查规则记录">
            <el-form :inline="true" :model="formData" ref="form" label-width="100px">
                <el-form-item label="发布状态:" prop="status">
                    <el-select v-model="formData.status" clearable placeholder="选择发布状态">
                        <el-option value="" label="全部" />
                        <el-option v-for="item in task_status" :key="item.k" :value="item.k" :label="item.v" />
                    </el-select>
                </el-form-item>
                <el-form-item label="生成月份:" prop="task_month">
                    <el-date-picker
                        v-model="formData.task_month"
                        type="month"
                        placeholder="选择任务生成月份">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="执法机构:" prop="orgs">
                    <el-cascader
                        class="custom-cascader"
                        v-model="formData.orgs"
                        :props="orgProps"
                        :options="orgOptions"
                        @change="orgChanged">
                    </el-cascader>
                </el-form-item>
                <el-form-item label="专项检查名称:" prop="task_name">
                    <el-input v-model="formData.task_name" placeholder="请输入专项检查名称"></el-input>
                </el-form-item>
                <el-button type="default" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button type="default" icon="el-icon-reset" @click="resetForm">重置</el-button>
            </el-form>
        </form-option>

        <el-dialog title="选择被检查单位" :visible.sync="showChooseResource" class="custom-dialog">
            <choose-resource @changeResource="changeResource"></choose-resource>
        </el-dialog>
    </div>
</template>

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

import { jobMixin } from '@/mixin/job'
import { resourceMixin } from '@/mixin/resource'
import {dateFormat} from "@/utils/tool";
import { orgMixin } from '@/mixin/org';

export default {
    components: {
        FormOption,
        ChooseResource
    },
    mixins: [jobMixin, resourceMixin, orgMixin],
    data () {
        return {
            formData: {
                status: '',
                task_month: '',
                org: '',
                orgs: null,
                task_name: '',
            },
            showChooseResource: false
        }
    },
    mounted () {
        this.loadOrgTree();
    },
    methods: {
        orgChanged(data) {
            this.formData.org = data.length > 0 ? data[data.length - 1] : null
        },
        changeResource(data) {
            if (data) {
                this.formData.company = data.id
                this.formData.companyName = data.enforce_obj_name
            }
            this.showChooseResource = false
        },
        resetForm() {
            this.$refs.form.resetFields()
            this.$parent.resetForm()
        },
        handleQuery() {
          let queryString = ""
          let expressions = []
          if (this.formData.status) {
            if (this.formData.status === 'TODO') {
              expressions.push('start_time' + "=gt=" + "'" + dateFormat("YYYY-mm-ddTHH:MM:SS", new Date()) + "'")
            } else if (this.formData.status === 'DOING') {
              expressions.push('start_time' + "=le=" + "'" + dateFormat("YYYY-mm-ddTHH:MM:SS", new Date()) + "'")
              expressions.push('end_time' + "=ge=" + "'" + dateFormat("YYYY-mm-ddTHH:MM:SS", new Date()) + "'")
            } else if (this.formData.status === 'FINISHED') {
              expressions.push('end_time' + "=lt=" + "'" + dateFormat("YYYY-mm-ddTHH:MM:SS", new Date()) + "'")
            }
          }
          if (this.formData.orgs&&this.formData.orgs[this.formData.orgs.length-1]!=null&&this.formData.orgs[this.formData.orgs.length-1]!='') {
            var orgsArr = this.formData.orgs;
            expressions.push("enforce_org_id" + "==" + "'" + orgsArr[orgsArr.length-1] + "'")
          }

          if (this.formData.task_month) {
            expressions.push("task_month" + "==" + "'" + dateFormat('YYYY-mm', new Date(this.formData.task_month)) + "*'")
          }

          if (this.formData.task_name) {
            expressions.push("notify_name" + "==" + "'*" + this.formData.task_name + "*'")
          }

          if (expressions.length !== 0) {
            queryString = expressions.reduce(function(previousValue, currentValue, index, array) {
              return previousValue + ";" + currentValue
            })
          }
          this.$emit('query', queryString)
        },
        addSpecialNotice() {
            this.$router.push({
                path: '/random/special/notice/form'
            })
        }
    }
}
</script>
