<template>
    <div class="oOrgan controller" :class="[preview ? '' : 'o-unit']" v-if="widget.type == 'organ'">
        <van-field
            :name="widget.model"
            v-model="dataModel"
            :label="widget.name || '文本框'"
            :required="fileCheck ? false : options.required || false"
            :disabled="options.disabled || false"
            clearable
            readonly
            :placeholder="options.placeholder || '请输入'"
            :rules="rule"
            clear-trigger="always"
            @click="onClick"
            v-if="!preview"
        >
            <template #right-icon>
                <i class="van-icon van-icon-arrow van-cell__right-icon"></i>
            </template>
        </van-field>

        <van-cell class="preview" :border="false" :title="widget.name || '文本框'" :value="dataModel" v-else></van-cell>

        <!-- 处理显示问题 -->
        <div class="valueShow" v-if="showDataModel && !preview">{{ showDataModel }}</div>
        <div class="valueShow valuePreview" v-if="showDataModel && preview">
            {{ showDataModel }}
            <input type="hidden" :name="widget.model" :value="dataModel">
        </div>

        <van-popup v-model="showSelect" position="bottom">
            <van-picker
                show-toolbar
                :columns="columns"
                @confirm="onConfirm"
                visible-item-count="4"
                @cancel="onCancel"
            />
        </van-popup>
    </div>
</template>
<script>
// import { queryEmployeeOrgan } from '@/views/payAppeal/api'
export default {
    name: 'oOrgan',
    props: ['widget', 'preview', 'value', 'fileCheck'],
    data() {
        return {
            dataModel: this.widget.options?.defaultValue || null,
            showDataModel: null,
            showSelect: false,
            columns: []
        }
    },
    async mounted() {
        // 初始化参数
        await this.$store.dispatch('setAllBranch')
        await this.getList()
        if(this.value) {
            const widget = this.widget
            const model = widget.originModel || widget.model
            for(let v in this.value) {
                if(v == model) {
                    this.dataModel = this.value[v]
                }
            }
            // 反显分部
            // console.log(this.dataModel)
            // console.log(this.columns)
            this.columns.forEach(item => {
                if(this.dataModel == item.value) {
                    this.showDataModel = item.text
                }
            })
        }
    },
    methods: {
        onClick() {
            this.showSelect = true
        },
        onConfirm(val) {
            this.dataModel = val.value
            this.showDataModel = val.text
            this.showSelect = false
        },
        onCancel() {
            this.showSelect = false
        },
        async getList() {
            // 获取所有分部
            try {
                this.columns = []
                // let res = await queryEmployeeOrgan()
                let res = this.selects.allBranch
                // console.log(res)
                let tempRes = res || []
                tempRes.forEach(item => {
                    this.columns.push({
                        text: item.name,
                        value: item.id
                    })
                })
            } catch {
                //
            }
        }
    },
    computed: {
        options() {
            return this.widget.options || {}
        },
        rule() {
            let rules = this.widget.rules || []
            if(rules && rules.length > 0) {
                rules.forEach(item => {
                    if(item.pattern) {
                        item.pattern = eval(item.pattern)
                    }
                    // 判断是否上传文件
                    if(this.fileCheck) {
                        item.required = false
                    }
                });
            }
            return rules
        }
    },
    watch: {
        // dataModel: {
        //     deep: true,
        //     handler(newValue) {
        //         if (newValue !== undefined && newValue !== null) {
        //         }
        //     }
        // },
    }
}
</script>

<style lang='less' scoped>
@import url('./controlCommon.less');
.oOrgan {
    position: relative;
    .valueShow {
        position: absolute;
        bottom: 12px;
        left: 16px;
        line-height: 24px;
        padding: .03rem 0;
        background: #fff;
        min-width: 120px;
    }
    .valuePreview {
        bottom: 2px;
    }
}
</style>