<script>
import SearchItem from './SearchItem'

export default {
    props: {
        data: Array,
        responseData: Object
    },
    components: {
        SearchItem
    },
    data () {
        return {
        }
    },
    methods: {
        onSearch () {
            let rows = []
            for (let key in this.$refs) {
                const {name, value, operation} = this.$refs[key].getData()
                if (value) {
                    const item = [name, value]
                    if (operation) item.push(operation)
        
                    rows.push(item.join(':'))
                }
            }
            const search = rows.join(';')
            if (search) {
                this.$emit('search', search)
            }
        },
        reset () {
            for (let k in this.$refs) {
                this.$refs[k].reset()
            }
            this.$emit('reset')
        }
    },
    render () {
        const formItems = this.data.map(item => {
            if (item.options && this.responseData && this.responseData[item.options]) {
                return <search-item
                    ref={item.name}
                    title={item.title}
                    name={item.name}
                    type={item.type}
                    operation={item.operation}
                    options={this.responseData[item.options]}
                />
            }
            return <search-item
                    ref={item.name}
                    title={item.title}
                    name={item.name}
                    type={item.type}
                    operation={item.operation}
                />
        })
        return (
            <el-form inline={true} model={this.search} label-position="left">
                {formItems}
                <el-form-item>
                    <el-button type="primary" onClick={this.onSearch}>查询</el-button>
                    <el-button onClick={this.reset}>重置</el-button>
                </el-form-item>
            </el-form>
        )
    }
}
</script>