<template>
<el-row class="query">
  <el-row >
    <el-col :span="21">
      <el-button v-auth="50300010105" type="primary" @click="toAdd">新增</el-button>
      <el-button v-auth="50300010106" type="primary" @click="e => operate(e, 'delete')" needconfirm>删除</el-button>
    </el-col>
    <el-col :span="3">
      <el-col :span="24">
      <el-button v-auth="50300010102" class="query__item" type="primary" @click="moreQuery">更多</el-button>
      <el-button v-auth="50300010101" icon="el-icon-refresh" class="query__item" type="primary" @click="handleRefresh" plain circle></el-button>
      <el-button v-auth="50100030101" icon="el-icon-download" v-if="false" type="primary" @click="handleExcel" plain circle></el-button>
      </el-col>
    </el-col>
  </el-row>
  <div class="inputBox">
    <el-form inline :model="query" ref="query" label-position="left">
      <el-form-item label="质检项组编码" prop="itemGroupCode">
        <el-input v-model="query.itemGroupCode" @keyup.enter.native="handleSearch" placeholder="快捷查询(质检项组编码)"></el-input>
      </el-form-item>
      <el-form-item label="质检项组名称" prop="itemGroupName">
        <el-input v-model="query.itemGroupName" @keyup.enter.native="handleSearch" ></el-input>
      </el-form-item>
    <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>
  </div>
  <MoreQuery
      :queryProp="query"
      :showMore="showMore"
      @close="showMore = false"
      @toGetList="getQueryList"
    ></MoreQuery>
</el-row>
</template>

<script>
import * as shortcuts from '@/config/shortcuts'
import MoreQuery from './MoreQuery'
export default {
  components: { MoreQuery },
  props: ['selection'],
  created () {
  },
  mounted () {
  },

  data () {
    return {
      query: {}, // 查询条件
      showMore: false, // 显示更多查询
      fileList: [],
      file: [],
      publicQueries: [
        { label: '创建人', valueField: 'createLoginName', cfgKey: 'bdWmsOperator', rowData: {} },
        { label: '修改人', valueField: 'modifyLoginName', cfgKey: 'bdWmsOperator', rowData: {} },
      ],
      datePickers: [
        { label: '创建时间', fm: 'createTimeFrom', to: 'createTimeTo', result: [] },
        { label: '修改时间', fm: 'modifyTimeFrom', to: 'modifyTimeTo', result: [] }
      ],
      pickerOptions: {
        shortcuts: { ...shortcuts }
      },
      moreData: {}
    }
  },
  methods: {
    // 检查是否有勾选
    checkSelections () {
      if (!this.selection.length) {
        this.$message.error('请选择记录')
        return false
      } else {
        return true
      }
    },
    dateTimeChange (item, val) {
      this.$set(item, 'result', val) // 深层修改页面渲染值对象
      Object.assign(this.query, { [item.fm]: val ? val[0] : '', [item.to]: val ? val[1] : '' }) // 以接口规定的传值格式合并query
      this.handleSearch()
    },
    // 新增按钮
    toAdd () {
      this.$router.push({
        name: 'qcItemsCreator',
        params: {
          mode: 'create',
        }
      })
    },
    // 对质检项任务进行操作, 更多按钮也在这里
    operateConfirm (type) {
      const url = this.$API.warehouseBasic.qcItems[type]
      const param = {
        qcHeaders: JSON.stringify(this.selection)
      }
      this.$https.post(url, param)
        .then(data => {
          console.log('操作成功', data)
          this.toGetList()
        })
    },

    handleRefresh () {
      this.$refs.query.resetFields()
      this.$emit('getList')
    },
    // 搜索
    handleSearch () {
      const query = { ...this.query, itemGroupCode: `%${this.query.itemGroupCode || ''}%` }
      this.$emit('getList', query)
    },
    // 更多查询数据
    getQueryList (query) {
      this.showMore = false
      this.moreData = query
      this.$emit('getList', query)
    },
    // 点击更多查询
    moreQuery () {
      this.showMore = true
    },
    handleExcel () {
      const url = this.$API.warehouseBasic.qcItems.getList
      const options = this.moreData
      this.$exportExcel(url, options)
    },
  },
}
</script>

<style lang="scss" scoped>
.inputBox {
      width: 100%;
      border: 1px solid #d1d5db;
      margin-top: 10px;
      margin-bottom: 10px;
      border-radius: 10px;
      padding-bottom: 10px;
      padding-left: 10px;
      .el-form-item {
        margin-bottom: 0px;
        margin-top: 10px;
      }
    }
  .query {
    &__operations {
      float: left;
    }
    &__retrieve {
      float: right;
      .el-input {
        max-width: 180px;
      }
    }
    &__item {
      margin-left: 10px;
    }
  }
</style>
