<template>
<el-dialog
  width="70%"
  class="more-query"
  title="高级查询"
  :visible="showMore"
  @close="close"
>
  <el-form
    inline
    label-position="top"
    :model="query"
    ref="queryForm"
  >
    <el-form-item label="规则编码" prop="ruleCode">
      <el-input v-model="query.ruleCode"></el-input>
    </el-form-item>
    <el-form-item label="规则名称" prop="ruleName">
      <el-input v-model="query.ruleName"></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.strategy.qcRules[selector.prop]"
            :key="item.id"
            :label="item.name"
            :value="item.key"
          ></el-option>
        </el-select>
      </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="item.textField === undefined ? false : query[item.textField]"
        ></PublicQuery>
      </el-form-item>
    </template>

    <!--时间框-->
    <template v-for="item in datePickers">
      <el-form-item :key="item.prop" :label="item.label">
        <el-date-picker
          v-model="item.result"
          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="val => dateTimeChange(item, val)"
        ></el-date-picker>
      </el-form-item>
    </template>

    <el-form-item label="备注" prop="remark">
      <el-input v-model="query.remark"></el-input>
    </el-form-item>
  </el-form>
  <div class="more-query__btn">
    <el-button type="primary" @click="submitForm">查询</el-button>
    <el-button @click="resetForm('queryForm')">重置</el-button>
  </div>
</el-dialog>
</template>

<script>
import * as shortcuts from '@/config/shortcuts'

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

  data () {
    return {
      query: { ...this.queryProp }, // 查询条件
      baseUrl: '/bd_web/bdweb', // 多条件基础URL
      selectors: [
        { label: '质检类型', prop: 'qcType', result: [] },
      ],
      publicQueries: [
        { label: '创建人', valueField: 'creator', textField: 'createUserNameCn', cfgKey: 'bdWmsOperator', rowData: {} },
        { label: '修改人', valueField: 'modifier', textField: 'modifyUserNameCn', cfgKey: 'bdWmsOperator', rowData: {} },
      ],
      datePickers: [
        { label: '创建时间', fm: 'createTimeFrom', to: 'createTimeTo', result: [] },
        { label: '修改时间', fm: 'modifyTimeFrom', to: 'modifyTimeTo', result: [] }
      ],
      pickerOptions: {
        shortcuts: { ...shortcuts }
      },
    }
  },
  methods: {
    // 关闭弹窗
    close () {
      this.$emit('close')
    },
    // 提交表单
    submitForm () {
      this.$emit('toGetList', this.query)
      this.close()
    },
    // 重置表单
    resetForm (formName) {
      // 时间
      for (const item of this.datePickers) {
        item.result = []
      }
      this.query.createTimeFrom = ''
      this.query.createTimeTo = ''
      this.query.modifyTimeFrom = ''
      this.query.modifyTimeTo = ''
      // 筛选框
      for (const item of this.selectors) {
        item.result = []
      }
      this.$refs[formName].resetFields()
    },
    // 多选变化
    selectChange (selector, val) {
      const copy = [...val]
      this.$set(selector, 'result', val) // 深层修改页面渲染值对象
      Object.assign(this.query, { [selector.prop]: copy.join(',') }) // 以接口规定的传值格式合并query
    },
    // 时间变化
    dateTimeChange (item, val) {
      this.$set(item, 'result', val) // 深层修改页面渲染值对象
      Object.assign(this.query, { [item.fm]: val ? val[0] : '', [item.to]: val ? val[1] : '' }) // 以接口规定的传值格式合并query
    }
  },
}
</script>

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