<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="查询次数" prop="queryNumber">
        <el-input v-model="queryParams.queryNumber" placeholder="请输入查询次数" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="查询主体" prop="queryType">
        <el-select v-model="queryParams.queryType" placeholder="请选择查询主体" clearable size="small">
          <el-option v-for="dict in dict.type.query_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="查询方式" prop="queryMode">
        <el-select v-model="queryParams.queryMode" placeholder="请选择查询方式" clearable size="small">
          <el-option v-for="dict in dict.type.query_mode" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:queryConfig:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:queryConfig:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:queryConfig:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="queryConfigList" @selection-change="handleSelectionChange"
      :max-height="tableMaxHeight">
      <el-table-column  type="selection" width="55" align="center" />
      <el-table-column  label="查询次数" align="center" prop="queryNumber"  />
      <el-table-column label="二维码失效时间" align="center" prop="qrTime"  />
      <el-table-column label="二维码失效单位" align="center" prop="qrUnit" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.qr_unit" :value="scope.row.qrUnit" />
        </template>
      </el-table-column>
      <el-table-column label="查询主体" align="center" prop="queryType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.query_type" :value="scope.row.queryType" />
        </template>
      </el-table-column>
      <el-table-column label="查询方式" align="center" prop="queryMode" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.query_mode" :value="scope.row.queryMode" />
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"  />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:queryConfig:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:queryConfig:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改查询配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
       <el-row>
         <el-col :span="12">
           <el-form-item label="查询方式" prop="queryMode">
          <el-select style="width: 100%;" @change="ndhiwed" v-model="form.queryMode" placeholder="请选择查询方式">
            <el-option  v-for="dict in dict.type.query_mode" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item></el-col>
         <el-col :span="12"><el-form-item label="查询主体" prop="queryType">
          <el-select style="width: 100%;" v-model="form.queryType" placeholder="请选择查询主体">
            <el-option v-for="dict in dict.type.query_type" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        </el-col>
         <el-col :span="12"><el-form-item label="查询次数" prop="queryNumber" v-if="!shouede">
          <el-input v-model="form.queryNumber" placeholder="请输入查询次数" />
        </el-form-item></el-col>
        <el-col :span="12">
           <el-form-item label="二维码失效时间" prop="qrTime" v-if="shouede">
          <el-input v-model="form.qrTime" placeholder="请输入二维码失效时间" />
        </el-form-item>
         </el-col>
         <el-col :span="12"><el-form-item label="二维码失效单位" prop="qrUnit" v-if="shouede">
          <el-select style="width: 100%;" v-model="form.qrUnit" placeholder="请选择二维码失效单位">
            <el-option v-for="dict in dict.type.qr_unit" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item></el-col>

        <el-col :span="12">
          <el-form-item label="是否启用" prop="status">
            <el-select style="width: 100%;" v-model="form.status" placeholder="请选择是否启用">
              <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
       </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listQueryConfig,
    getQueryConfig,
    delQueryConfig,
    addQueryConfig,
    updateQueryConfig
  } from "@/api/system/queryConfig";

  export default {
    name: "QueryConfig",
    dicts: ['qr_unit', 'query_type', 'query_mode', 'sys_yes_no', 'is_del'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 查询配置表格数据
        queryConfigList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          queryNumber: null,
          queryType: null,
          queryMode: null,
          status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          queryType: [{
            required: true,
            message: "查询主体不能为空",
            trigger: "change"
          }],
          queryMode: [{
            required: true,
            message: "查询方式不能为空",
            trigger: "change"
          }],
          status: [{
            required: true,
            message: "是否启用不能为空",
            trigger: "change"
          }],
        },
        shouede: false,
        tableMaxHeight: 300
      };
    },
    created() {
      this.getList();
      window.onresize = () => {
        this.changeTableMaxHeight()
      }
      this.changeTableMaxHeight()
    },
    destroyed() {
      window.onresize = null
    },
    mounted() {
      window.onresize = () => {
        this.changeTableMaxHeight()
      }
      this.changeTableMaxHeight()
    },
    methods: {
      /** 查询查询配置列表 */
      getList() {
        this.loading = true;
        listQueryConfig(this.queryParams).then(response => {
          this.queryConfigList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          configId: null,
          queryNumber: null,
          qrUnit: null,
          qrTime: null,
          queryType: null,
          queryMode: null,
          status: null,
          isDel: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      ndhiwed(){
      // this.queryParams.pageNum = 1;
      if (this.form.queryMode !== undefined) {
        if (this.form.queryMode !== null) {
          if (this.form.queryMode !== "") {
            if (this.form.queryMode == 0) {
              // 二维码查询
              this.shouede = true
            } else {
              // 其余方式查询
              this.shouede = false
            }
          }
        }
      }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        // this.queryParams.pageNum = 1;
        // if (this.queryParams.queryMode !== undefined) {
        //   if (this.queryParams.queryMode !== null) {
        //     if (this.queryParams.queryMode !== "") {
        //       if (this.queryParams.queryMode == 0) {
        //         // 二维码查询
        //         this.shouede = true
        //       } else {
        //         // 其余方式查询
        //         this.shouede = false
        //       }
        //     }
        //   }
        // }
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.configId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加查询配置";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const configId = row.configId || this.ids
        getQueryConfig(configId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改查询配置";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.configId != null) {
              updateQueryConfig(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addQueryConfig(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const configIds = row.configId || this.ids;
        this.$modal.confirm('是否确认删除查询配置编号为"' + configIds + '"的数据项？').then(function() {
          return delQueryConfig(configIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/queryConfig/export', {
          ...this.queryParams
        }, `queryConfig_${new Date().getTime()}.xlsx`)
      },
      // 获取屏幕高度
      showFilterForm() {
        this.filterActive = !this.filterActive
        this.changeTableMaxHeight()
      },

      changeTableMaxHeight() {
        let height = document.body.offsetHeight // 网页可视区域高度
        // if (this.filterActive) {
        //   this.tableMaxHeight = height - 320
        // } else {
        this.tableMaxHeight = height - 250
        // }
        console.log(height)
      }
    }
  };
</script>
