<template>
<el-dialog
  class="more-query"
  title="高级查询"
  width="70%"
  :visible="showMore"
  @close="close"
>
  <el-form
    inline
    label-position="top"
    :model="query"
    ref="queryForm"
  >
    <el-form-item label="冻结ID" prop="holdId">
      <el-input v-model="query.holdId"></el-input>
    </el-form-item>
    <!--筛选框-->
    <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.inventory.block[selector.prop]"
            :key="item.id"
            :label="item.name"
            :value="item.key"
          ></el-option>
        </el-select>
      </el-form-item>
    </template>

    <!--输入框-->
    <template v-for="item in inputs">
      <el-form-item
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
      >
        <el-input v-model="query[item.prop]"></el-input>
      </el-form-item>
    </template>

    <!--公用查询框-->
    <template v-for="item in publicQueries">
      <el-form-item :key="item.cfgKey" :label="item.label" :prop="item.valueField">
        <PublicQuery
          :title="item.label"
          :cfgKey="item.cfgKey"
          :baseUrl="baseUrl"
          :valueField.sync="query[item.valueField]"
          :textField.sync="item.textField === undefined ? false : query[item.textField]"
        ></PublicQuery>
      </el-form-item>
    </template>

  </el-form>
  <div class="more-query__btn">
    <el-button v-auth="50300120103" type="primary" @click="submitForm">查询</el-button>
    <el-button v-auth="50300120104" @click="resetForm('queryForm')">重置</el-button>
  </div>
</el-dialog>
</template>

<script>
export default {
  props: ['showMore', 'queryProp'],
  created () {
  },
  mounted () {
  },

  data () {
    return {
      query: { ...this.queryProp }, // 查询条件
      baseUrl: '/inv_web/invweb', // 多条件基础URL
      inputs: [
        { label: '原因描述', prop: 'reason' },
        { label: '批次号', prop: 'lotNum' },
        { label: '跟踪号', prop: 'traceId' },
      ],
      selectors: [
        { label: '冻结方式', prop: 'holdType', result: [] },
        { label: '冻结原因', prop: 'reasonCode', result: [] },
      ],
      publicQueries: [
        { label: '货主', valueField: 'ownerCode', textField: 'ownerName', cfgKey: 'invWmsCustomer' },
        { label: '商品', valueField: 'skuCode', textField: 'skuName', cfgKey: 'invWmsSku' },
        { label: '库位', valueField: 'locCode', cfgKey: 'invWmsLoc' },
      ],
    }
  },
  methods: {
    // 关闭弹窗
    close () {
      this.$emit('close')
    },
    // 提交表单
    submitForm () {
      this.$emit('toGetList', this.query)
      this.close()
    },
    // 重置表单
    resetForm (formName) {
      for (const item of this.selectors) {
        // 因选择框是另外操作的逻辑, 单独重置选择框
        item.result = []
        this.query[item.prop] = undefined
      }
      this.$refs[formName].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>
