<template>
<el-dialog
  width="70%"
  class="more-query"
  title="高级查询"
  :visible="showMore"
  @close="close"
>
  <el-form
    inline
    label-position="top"
    :model="query"
    ref="queryForm"
  >
    <!--公用查询框-->
    <template v-for="item in publicQueries">
      <el-form-item :key="item.valueField" :label="item.label" :prop="item.valueField">
        <PublicQuery
          single
          :title="item.label"
          :cfgKey="item.cfgKey"
          :baseUrl="baseUrl"
          :valueField.sync="query[item.valueField]"
        ></PublicQuery>
      </el-form-item>
    </template>

    <!--选择框-->
    <template v-for="selector in selectors">
      <el-form-item
        :key="selector.prop"
        :label="selector.label"
        :prop="selector.prop"
      >
        <el-select multiple :value="selector.result" @change="val => selectChange(selector, val)">
          <el-option
            v-for="item in $keys.warehouseBasic[selector.keyName || selector.prop]"
            :key="item.id"
            :label="item.name"
            :value="item.key"
          ></el-option>
        </el-select>
      </el-form-item>
    </template>
  </el-form>
  <div class="more-query__btn">
    <el-button type="primary" @click="submitForm">查询</el-button>
    <el-button @click="reset">重置</el-button>
  </div>
</el-dialog>
</template>

<script>
import { time } from 'apec-fe-utils'
import * as shortcuts from '@/config/shortcuts'
export default {
  props: ['showMore', 'queryProp'],
  created () {
  },
  mounted () {
  },

  data () {
    return {
      query: { ...this.queryProp }, // 查询条件
      baseUrl: '/rep_web/repweb',
      publicQueries: [
        { label: '区域', valueField: 'areaCode', cfgKey: 'repWmsArea', rowData: {} },
        { label: '库区', valueField: 'zoneCode', cfgKey: 'repWmsZone', rowData: {} },
        { label: '库位编码', valueField: 'locCode', cfgKey: 'repWmsLoc', rowData: {} },
      ],
      selectors: [
        { label: '库位类型', prop: 'locUseType', result: [] },
        { label: '库位种类', prop: 'category', keyName: 'cdwlCategory', result: [] }, // keyName为在keys中的字段名, 与本业务页面提交时的prop不一致时使用此属性
      ]
    }
  },
  methods: {
    // 关闭弹窗
    close () {
      this.$emit('close')
    },
    // 提交表单
    submitForm () {
      this.$emit('toGetList', this.query)
      this.close()
    },
    // 重置表单
    reset () {
      this.dateTime = []
      this.query.tranTimeFrom = ''
      this.query.tranTimeTo = ''
      for (const item of this.selectors) {
        item.result = []
      }
      this.$refs.queryForm.resetFields()
    },
    // 多选变化
    selectChange (selector, val) {
      const copy = [...val]
      this.$set(selector, 'result', val) // 深层修改页面渲染值对象
      Object.assign(this.query, { [selector.prop]: copy.join(',') }) // 以接口规定的传值格式合并query
    },
  },
}
</script>

<style lang="scss" scoped>
</style>
