<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 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.valueField" :label="item.label" :prop="item.valueField">
        <PublicQuery
          :title="item.label"
          :cfgKey="item.cfgKey"
          :baseUrl="baseUrl"
          :valueField.sync="query[item.valueField]"
          :textField.sync="query[item.textField]"
        ></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.stockOut[selector.prop]"
            :key="item.id"
            :label="item.name"
            :value="item.key"
          ></el-option>
        </el-select>
      </el-form-item>
    </template>
    <el-form-item label="订单时间">
      <el-date-picker
        v-model="dateTime"
        type="datetimerange"
        :picker-options="pickerOptions"
        :default-time="['00:00:00', '23:59:59']"
        format="yyyy-MM-dd HH:mm"
        value-format="yyyy-MM-dd HH:mm:ss"
        @change="dateTimeChange"
      ></el-date-picker>
    </el-form-item>
  </el-form>
  <div class="more-query__btn">
    <el-button v-auth="50940040103" type="primary" @click="submitForm">查询</el-button>
    <el-button v-auth="50940040104" @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'],
  created () {
  },
  mounted () {
    this.setDefaultTime()
  },

  data () {
    return {
      query: {}, // 查询条件
      baseUrl: '/enp_web/enpweb',
      dateTime: [],
      pickerOptions: {
        shortcuts: { ...shortcuts }
      },
      inputs: [
        { label: '出库单号', prop: 'soNo' },
        { label: '物流单号', prop: 'logisticNo' },
      ],
      publicQueries: [
        { label: '仓库', valueField: 'projectIds', textField: 'cdwhName', cfgKey: 'enpWmsShareWareHouse', rowData: {} },
        { label: '货主', valueField: 'ownerCode', textField: 'ownerName', cfgKey: 'enpWmsCustomer', rowData: {} },
        { label: '商品', valueField: 'skuCode', textField: 'skuName', cfgKey: 'enpWmsSkuCode', rowData: {} },
        { label: '收货人', valueField: 'consigneeCode', textField: 'consigneeName', cfgKey: 'enpWmsConsigneeCode', rowData: {} },
      ],
      selectors: [
        { label: '状态', prop: 'status', result: [] },
        { label: '出库单类型', prop: 'soType', result: [] },
      ],
    }
  },
  methods: {
    setDefaultTime () {
      // 设置默认时间
      const startTime = new Date().getTime() - 6 * 24 * 60 * 60 * 1000
      this.dateTime = [ time.formatDate(new Date(startTime)) + ' 00:00:00', time.formatDate(new Date()) + ' 23:59:59' ]
      this.query.orderTimeForm = this.dateTime[0]
      this.query.orderTimeTo = this.dateTime[1]
    },
    // 关闭弹窗
    close () {
      this.$emit('close')
    },
    // 提交表单
    submitForm () {
      this.$emit('toGetList', this.query)
      this.close()
    },
    // 重置表单
    reset () {
      this.dateTime = []
      this.query.orderTimeForm = ''
      this.query.orderTimeTo = ''
      for (const item of this.selectors) {
        this.query[item.prop] = undefined
        item.result = []
      }
      this.$refs.queryForm.resetFields()
    },
    // 时间变化
    dateTimeChange (val) {
      this.query.orderTimeForm = val ? val[0] : ''
      this.query.orderTimeTo = val ? val[1] : ''
    },
    // 多选变化
    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>
