<template>
    <form-option title="执法对象">
        <el-form :inline="true" :model="query" ref="queryForm" label-width="80px">
            <el-form-item label="单位名称:" prop="unitName">
                <el-input v-model="query.unitName" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="单位法人:" prop="unitOwner">
                <el-input v-model="query.unitOwner" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="单位规模:" prop="unitSize">
                <el-select v-model="query.unitSize" clearable placeholder="请选择">
                    <el-option value="" label="全部"></el-option>
                    <el-option v-for="item in unitSize" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    <el-option value="5" label="九小场所"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="场所类型:" prop="place_type">
                <el-select v-model="query.place_type" clearable placeholder="请选择">
                    <el-option value="" label="全部"></el-option>
                    <el-option v-for="item in placeType" :key="item.code" :label="item.name" :value="item.code"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="单位标签:" prop="tag">
                <el-cascader
                    v-model="query.tag"
                    :props="tagProps"
                    :options="tagOptions">
                </el-cascader>
            </el-form-item>
            <el-form-item label="所属辖区:" prop="org">
                <el-cascader
                        v-model="query.org"
                        :props="orgProps"
                        :options="orgOptions">
                </el-cascader>
            </el-form-item>
            <el-form-item label="建筑安全:" prop="safe">
                <el-select v-model="query.safe" clearable placeholder="请选择">
                    <el-option value="" label="全部"></el-option>
                    <el-option v-for="item in responsibilityType" :key="item.id" :value="item.id"
                               :label="item.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="近期检查:" prop="check">
                <el-select v-model="query.check" clearable placeholder="请选择">
                    <el-option value="" label="全部"></el-option>
                    <el-option v-for="item in statusList2" :key="item.id" :value="item.id"
                               :label="item.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="经营状态:" prop="status">
                <el-select v-model="query.status" clearable placeholder="请选择">
                    <el-option value="" label="全部"></el-option>
                    <el-option v-for="item in operateStatus" :key="item.id" :value="item.id"
                               :label="item.name"></el-option>
                </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-form>
    </form-option>
</template>

<script>
    import FormOption from '@/component/FormOption'
    import {getPlaceTypes} from '@/api/resource'
    import {resourceMixin} from '@/mixin/resource'
    import {getTags} from "@/api/random";
    import { getStr } from '@/utils/tool';
    import { orgMixin } from '@/mixin/org';

    export default {
        components: {
            FormOption
        },
        mixins: [resourceMixin, orgMixin],
        data() {
            return {
                query: {
                    unitName: '',
                    unitOwner: '',
                    unitSize: '',
                    org: '',
                    safe: '',
                    check: '',
                    place_type:'',
                    tag: '',
                    status: ''
                },
                tagOptions: [],
                tagProps: {
                    checkStrictly: true,
                    value: 'id',
                    label: 'tag_name',
                    children: 'sub_tags'
                },
                placeType: [],
            }
        },
        mounted() {
            this.getPlaceTypesAll();
            this.loadOrgTree()
            this.loadTags()
        },
        methods: {
            async getPlaceTypesAll() {
                const list = await getPlaceTypes();
                if (list && list.length > 0) {
                    this.placeType=list;
                }
            },
            loadTags() {
                getTags({page: 1, size: 1000, query: 'level==1'}).then(res => {
                    if (res) {
                        this.tagOptions = res.data
                    }
                })
            },
            resetForm() {
                this.$refs.queryForm.resetFields()
                this.$emit('reset')
            },
            handleQuery() {
                let res = [];
                const name = getStr(this.query.unitName);
                if (name) {
                    res.push(`enforce_obj_name=="*${name}*"`);
                }
                const place = getStr(this.query.place_type);
                if (place) {
                    res.push(`place_type==${place}`);
                }
                const person = getStr(this.query.unitOwner);
                if (person) {
                    res.push(`legal_person==*${person}*`);
                }
                const size = getStr(this.query.unitSize);
                if (size) {
                    res.push(`enforce_obj_type==${size}`);
                }
                const safe = getStr(this.query.safe);
                if (safe) {
                    res.push(`hole_responsibility==${safe}`);
                }
                const check = getStr(this.query.check);
                if (check) {
                    res.push(`last_enforce_status==${check}`);
                }
                if (this.query.org && this.query.org.length > 0) {
                    const org = this.query.org[this.query.org.length - 1];
                    res.push(`enforce_org_id==${org}`);
                }
                if (this.query.status && this.query.status >= 0) {
                  res.push(`manage_status==${this.query.status}`)
                }
                const query = res.join(';');
                let tag = '';
                if (this.query.tag && this.query.tag.length > 0) {
                    tag = this.query.tag[this.query.tag.length - 1];
                }
                this.$emit('query', query, tag);
            }
        }
    }
</script>
