<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="org">
                <el-cascader
                  class="custom-cascader"
                  v-model="query.org"
                  :props="orgPropsNow"
                  :options="orgOptions"
                  @change="orgChanged">
                </el-cascader>
            </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 v-for="item in checkResult" :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-form>
    </form-option>
</template>

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

    export default {
        components: {
            FormOption
        },
        mixins: [resourceMixin, orgMixin],
        data() {
            return {
                query: {
                    unitName: '',
                    unitOwner: '',
                    unitSize: '',
                    org: '',
                    safe: '',
                    check: '',
                    place_type:'',
                    tag: '',
                },
                // orgOptions: [],
                orgPropsNow: {
                  checkStrictly: true,
                  value: 'enforce_org_name',
                  label: 'enforce_org_name',
                  children: 'sub_orgs',
                },
                tagOptions: [],
                tagProps: {
                    checkStrictly: true,
                    value: 'id',
                    label: 'tag_name',
                    children: 'sub_tags'
                },
                placeType: [],
              checkResult: [
                {
                  id: '合格',
                  name: '合格'
                },
                {
                  id: '责令整改',
                  name: '责令整改'
                },
                {
                  id: '责令限期整改',
                  name: '责令限期整改'
                },
                {
                  id: '不检查（歇业）',
                  name: '不检查（歇业）'
                },
                {
                  id: '不检查（停业）',
                  name: '不检查（停业）'
                },
                {
                  id: '不检查（关门）',
                  name: '不检查（关门）'
                },
                {
                  id: '不检查（其他）',
                  name: '不检查（其他）'
                },
              ]
            }
        },
        mounted() {
            this.getPlaceTypesAll();
            // this.getOrgs()
            this.loadTags()
          this.loadOrgTree()
        },
        methods: {
          orgChanged(value) {
            console.log(value)
            // this.query.org = data.length > 0 ? data[data.length - 1] : null
            // this.orgUsers = []
            // this.getOrgUsers()
          },
          getOrgUsers() {
            if (this.formData.org) {
              getUserByOrg(1, 10000, this.formData.org).then(res => {
                if (res && res.status === 200 && res.data) {
                  this.orgUsers = res.data
                }
              })
            }
            else {
              this.$message.warning('请先选择执法机构')
            }
          },
            async getPlaceTypesAll() {
                const list = await getPlaceTypes();
                if (list && list.length > 0) {
                    this.placeType=list;
                }
            },
            checkOrgLeaf(arr) {
                if (arr && arr.length > 0) {
                    const vm = this
                    arr.map(item => {
                        if (!item.children || item.children.length === 0) {
                            item.children = undefined
                        } else {
                            vm.checkOrgLeaf(item.children)
                        }
                    })
                }
            },
            // getOrgs() {
            //     getOrg().then(res => {
            //       console.log(res)
            //         if (res.data) {
            //             const data = res.data
            //             this.checkOrgLeaf(data)
            //             this.orgOptions = data
            //         }
            //     })
            // },
            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(`dwmc=="*${name}*"`);
                }
                const person = getStr(this.query.unitOwner);
                if (person) {
                    res.push(`(jcr_z==*${person}*,jcr_x==*${person}*)`);
                }
                if (this.query.org && this.query.org.length > 0 && this.query.org[0]) {
                    const org = this.query.org[this.query.org.length - 1];
                    res.push(`sjjg==${org}`);
                }
                const place_type = getStr(this.query.place_type)
                if (place_type) {
                  res.push(`jcjgzt==${place_type}`);
                }
                const query = res.join(';');
                this.$emit('query', query, '');
            }
        }
    }
</script>
