<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 jobStatusV2" :key="item.k" :value="item.k" :label="item.v"/>
                    </el-select>
                </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="account">
                    <el-select v-model="formData.account" clearable placeholder="选择检查人员">
                        <el-option value="" label="全部"/>
                        <el-option v-for="item in orgUsers" :key="item.uid" :value="item.uid"
                                   :label="item.name"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="检查时间:" prop="checkDate">
                    <el-date-picker v-model="formData.checkDate" range-separator="至"
                                    type="daterange" start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="被检查单位:" prop="companyName">
                    <el-select v-model="formData.companyName" placeholder="选择被检查单位"
                               @focus="showChooseResource=true"></el-select>
                </el-form-item>
                <el-form-item label="检查期限:" prop="limit">
                    <el-date-picker v-model="formData.limit" range-separator="至"
                                    type="daterange" start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="创建时间:" prop="createDate">
                    <el-date-picker v-model="formData.createDate" range-separator="至"
                                    type="daterange" start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="检查结果:" prop="result">
                    <el-select v-model="formData.result" clearable placeholder="选择检查结果">
                        <el-option value="" label="全部"/>
                        <el-option v-for="item in statusList" :key="item.id" :value="item.id" :label="item.name"/>
                    </el-select>
                </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-button type="primary" icon="el-icon-circle-plus-outline" @click="addData">新增</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 {getUserByOrg} from '@/api/base'
    import {jobMixin} from '@/mixin/job'
    import {resourceMixin} from '@/mixin/resource'
    import {DateTime} from "luxon";
    import { orgMixin } from '@/mixin/org';

    export default {
        components: {
            FormOption,
            ChooseResource
        },
        mixins: [jobMixin, resourceMixin, orgMixin],
        data() {
            return {
                statusList: [
                    {
                        id: 'PASS',
                        name: '合格'
                    },
                    {
                        id: 'NOT_PASS',
                        name: '不合格'
                    },
                    {
                        id: 'OVERPASS',
                        name: '不检查'
                    }
                ],
                formData: {
                    status: '',
                    org: '',
                    orgs: '',
                    account: '',
                    checkDate: '',
                    companyName: '',
                    company: '',
                    limit: '',
                    createDate: '',
                    result: ''
                },
                orgUsers: [],
                showChooseResource: false,
                jobStatusV2: [
                    // {
                    //     k: 'AUDIT',
                    //     v: '审核中'
                    // },
                    // {
                    //     k: 'REJECT',
                    //     v: '已驳回'
                    // },
                    {
                        k: 'TODO',
                        v: '待执行'
                    },
                    {
                        k: 'DOING',
                        v: '执行中'
                    },
                    {
                        k: 'ARCHIVED',
                        v: '已归档'
                    }
                ],
            }
        },
        mounted() {
            this.loadOrgTree()
        },
        methods: {
            getOrgUsers() {
                if (this.formData.org) {
                    getUserByOrg(1, 10000, this.formData.org).then(res => {
                        if (res && res.data) {
                            this.orgUsers = res.data
                        }
                    })
                } else {
                    this.$message.warning('请先选择执法机构')
                }
            },
            orgChanged(data) {
                this.formData.org = data.length > 0 ? data[data.length - 1] : null
                this.orgUsers = []
                this.getOrgUsers()
            },
            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.$parent.resetForm()
                this.orgUsers = []
            },
            addData() {
                this.$emit("changeDialogFormVisible")
            },
            handleQuery() {
                let queryString = ""
                let expressions = []
                if (this.formData.status) {
                    expressions.push("job_status" + "==" + "'" + this.formData.status + "'")
                }
                if (this.formData.org) {
                    expressions.push("enforce_org_id" + "==" + "'" + this.formData.org + "'")
                }
                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.checkDate && this.formData.checkDate.length === 2) {
                    expressions.push('start_time' + ">" + "'" +
                        DateTime.fromJSDate(this.formData.checkDate[0]).toFormat("yyyy-LL-dd HH:mm:ss") + "'")
                    expressions.push('start_time' + "<" + "'" +
                        DateTime.fromJSDate(this.formData.checkDate[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 HH:mm:ss") + "'")
                    expressions.push('last_enforce_date' + "<" + "'" + DateTime.fromJSDate(this.formData.limit[1]).toFormat("yyyy-LL-dd") + " 23:59:59'")
                }

                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 HH:mm:ss") + "'")
                }

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