<template>
  <div class="app-container">
    <div v-if="isAdmin">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="false" v-show="showSearch" label-width="96px">
      <el-form-item label="字符串" prop="str">
        <el-input
          v-model="queryParams.str"
          placeholder="请输入字符串"
          clearable
          @keyup.enter.native="handleQuery"
          type="textarea"
        />
      </el-form-item>
      <el-form-item label="Type" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rows" border>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <!--<el-table-column label="ID" align="center" prop="id" min-width="60" />-->
      <el-table-column v-for="(item, index) in columns" width="auto" resizable :label="item.name" header-align="center" align="left" :prop="item.name" />
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleLook(scope.row)" v-hasPermi="['score:farmerEvalMonth:query']">查看</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"
    />

    </div>

    <el-dialog title="详情" :visible.sync="open" width="1200px" append-to-body>
      <el-descriptions :column="descColumn" border :labelStyle="{width: `${descLabelWidth}%`}" :contentStyle="{width: `${(100 / descColumn) - descLabelWidth}%`}">
        <el-descriptions-item v-for="(val, key) in form" :key="key" :label="key">{{ val }}</el-descriptions-item>
      </el-descriptions>

      <!-- 弹框操作按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import request from "@/utils/request";
import {IS_STABLE} from "@/utils/dev";

const SQL_API = '/qwertyasd123456/zhao';

export default {
  name: "SQLTesting",
  components: {
  },
  props: [],
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 默认隐藏搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        str: null,
        type: '',
      },
      // 表单参数
      form: {},
      // 详情组件列数
      descColumn: 2,
      // 详情组件Label所占百分比, 最大={100 / descColumn}. 内容所占百分比={100 / descColumn - descLabelWidth}
      descLabelWidth: 15,
      // 对话框显示只读的详情
      viewOpen: false,
      rows: [],
      columns: [],
    };
  },
  created() {
  },
  methods: {
    /** 查询农户评价村委月汇列表 */
    getList() {
      this.loading = true;
      let fd = new FormData;
      fd.set("str", this.queryParams.str);
      fd.set("type", this.queryParams.type);
      request({
        url: `${SQL_API}/exec`,
        method: 'post',
        data: fd,
      }).then((resp) => {
        this.columns = resp.data.columns;
        this.rows = resp.data.rows;
      }).finally(() => {
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleLook(row) {
      this.form = row;
      this.open = true;
    },
  },
  watch: {
  },
  computed: {
    isAdmin() {
      return /*!IS_STABLE() && */this.$store.getters.roles[0] === 'admin';
    }
  }
};
</script>
