<template>
    <div>
        <form-option title="举报投诉审批">
            <el-form :inline="true" :model="formData" ref="form" label-width="90px">
                <el-form-item label="审核状态:" prop="status">
                    <el-select v-model="formData.status" clearable placeholder="选择任务状态">
                        <el-option value="" label="全部"/>
                        <el-option v-for="item in jobStatusLo" :key="item.k" :value="item.k" :label="item.v"/>
                    </el-select>
                </el-form-item>

                <el-form-item label="举报人:" prop="report_user_name">
                    <el-input v-model="formData.report_user_name" />
                </el-form-item>
                <el-form-item label="联系电话:" prop="report_user_phone">
                    <el-input v-model="formData.report_user_phone" />
                </el-form-item>
                <el-form-item label="举报概要:" prop="job_desc">
                    <el-input v-model="formData.job_desc" />
                </el-form-item>

                <el-form-item label="举报时间:" prop="create_time">
                    <el-date-picker v-model="formData.create_time" range-separator="至"
                                    type="daterange" start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                    ></el-date-picker>
                </el-form-item>

                <el-button class="ml" icon="el-icon-refresh-right" @click="resetForm">重置</el-button>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</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 {DateTime} from "luxon";

    export default {
        components: {
            FormOption,
            ChooseResource
        },
        mixins: [jobMixin, resourceMixin],
        data() {
            return {
                formData: {
                    status: '',
                    org: '',
                    orgs: '',
                    account: '',
                    create_time: '',
                    companyName: '',
                    company: '',
                    limit: '',
                    createDate: '',
                    result: '',
                    report_user_name:'',
                    report_user_phone:'',
                    job_desc:'',
                },
                showChooseResource: false,
              jobStatusLo:[
                {
                  k: 'AUDIT',
                  v: '审核中'
                },
                {
                  k: 'REJECT',
                  v: '已驳回'
                },
                {
                  k: 'ACCEPT',
                  v: '已通过'
                }],
            }
        },
        methods: {
            changeResource(data) {
                if (data) {
                    this.formData.company = data.id
                    this.formData.companyName = data.enforce_obj_name
                }
                this.showChooseResource = false
            },
            resetForm() {
                this.formData.company = ''
                this.$refs.form.resetFields()
                this.formData.company = ''
                this.$parent.resetForm()
            },
            handleQuery() {
                let queryString = ""
                let expressions = []
                if (this.formData.status) {
                  if("ACCEPT" === this.formData.status){
                    expressions.push("job_status" + "=in=('TODO','DOING','ARCHIVED')")
                  }else {
                    expressions.push("job_status" + "==" + "'" + this.formData.status + "'")
                  }
                }
                // 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.account) {
                //     expressions.push("executor_ids" + "==" + "'*" + this.formData.account + "*'")
                // }
                // if (this.formData.company) {
                //     expressions.push("enforce_obj_id" + "==" + "'" + this.formData.company + "'")
                // }
                // if (this.formData.result) {
                //     expressions.push("enforce_result" + "==" + "'" + this.formData.result + "'")
                // }
                if (this.formData.report_user_name) {
                    expressions.push("report_user_name" + "==" + "'*" + this.formData.report_user_name + "*'")
                }
                if (this.formData.report_user_phone) {
                    expressions.push("report_user_phone" + "==" + "'*" + this.formData.report_user_phone + "*'")
                }
                if (this.formData.job_desc) {
                    expressions.push("job_desc" + "==" + "'*" + this.formData.job_desc + "*'")
                }
                // report_user_name:'',
                //     report_user_phone:'',
                //     job_desc:'',

                if (this.formData.create_time && this.formData.create_time.length === 2) {
                    expressions.push('create_time' + ">" + "'" +
                        DateTime.fromJSDate(this.formData.create_time[0]).toFormat("yyyy-LL-dd HH:mm:ss") + "'")
                    expressions.push('create_time' + "<" + "'" +
                        DateTime.fromJSDate(this.formData.create_time[1]).toFormat("yyyy-LL-dd HH:mm:ss") + "'")
                }

                if (this.formData.limit && this.formData.limit.length === 2) {
                    expressions.push('last_enforce_date' + ">=" + "'" +
                        DateTime.fromJSDate(this.formData.limit[0]).toFormat("yyyy-LL-dd") + "'")
                    expressions.push('last_enforce_date' + "<=" + "'" +
                        DateTime.fromJSDate(this.formData.limit[1]).toFormat("yyyy-LL-dd") + "'")
                }

                if (this.formData.createDate && this.formData.createDate.length === 2) {
                    expressions.push('create_time' + ">" + "'" +
                        DateTime.fromJSDate(this.formData.createDate[0]).toFormat("yyyy-LL-dd HH:mm:ss") + "'")
                    expressions.push('create_time' + "<" + "'" +
                        DateTime.fromJSDate(this.formData.createDate[1]).toFormat("yyyy-LL-dd") + " 23:59:59'")
                }

                if (expressions.length !== 0) {
                    queryString = expressions.reduce(function (previousValue, currentValue, index, array) {
                        return previousValue + ";" + currentValue
                    })
                }
                this.$emit('query', queryString)
            }
        }
    }
</script>
