<template>
  <el-container>
    <el-aside width="40%">
      <el-container>
        <el-header>
          <div class="left-panel">
            <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'dataset.create'"></el-button>
          </div>
          <div class="right-panel">
            <div class="right-panel-search">
              <el-input v-model="query.search" placeholder="名称" clearable @keyup.enter="handleQuery"></el-input>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery"></el-button>
            </div>
          </div>
        </el-header>
        <el-main class="nopadding">
          <scTable ref="table" :apiObj="apiObj" row-key="id" stripe>
            <el-table-column label="#" type="index" width="50"></el-table-column>
            <el-table-column label="名称" prop="name" width="240" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column label="代号" prop="code" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column label="操作" fixed="right" align="left" width="200">
              <template #default="scope">
                <el-button type="primary" plain size="small" @click="handleShow(scope.row)"
                  v-auth="'dataset.exec'">预览</el-button>
                <el-button plain type="warning" size="small" @click="table_edit(scope.row, scope.$index)"
                  v-auth="'dataset.update'">编辑</el-button>
                <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                  <template #reference>
                    <el-button plain type="danger" size="small" v-auth="'dataset.delete'">删除</el-button>
                  </template>
                </el-popconfirm>
              </template>
            </el-table-column>
          </scTable>
        </el-main>
      </el-container>
    </el-aside>
    <el-main>
      <el-row>
        <div style="width: 100%;background-color: white;font-size: 14px; margin-top:2px"
          v-for="(value, key, index) in bidata" :key="index">
          <table class="custom-table">
            <thead>
              <tr>
                <th v-for="(header, index) in value[0]" :key="index">{{ header }}</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(row, rowIndex) in value.slice(1)" :key="rowIndex">
                <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </el-row>
      <div style="height:8px"></div>
      <el-row>
        <div style="width: 100%;background-color: white;">
          <scEcharts height="360px" :option="myOption" v-if="chartShow"></scEcharts>
        </div>
      </el-row>
    </el-main>
  </el-container>
  <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess"
    @closed="dialog.save = false"></save-dialog>
</template>
<style scoped>
.custom-table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border: 1px solid #ddd;
  padding: 2px;
  text-align: left;
}

th {
  background-color: #f2f2f2;
}
</style>
<script>
import scEcharts from "@/components/scEcharts";
import saveDialog from "./dataset_form.vue";
export default {
  name: "dataset",
  components: {
    saveDialog,
    scEcharts,
  },
  data() {
    return {
      chartShow: false,
      myOption: null,
      dialog: {
        save: false,
      },
      adminform: {
        admin: null,
        username: "",
      },
      apiObj: this.$API.bi.dataset.list,
      query: {},
      selection: [],
      search: {
        keyword: null,
      },
      datasetID: "",
      adminvisible: false,
      bidata: [],
      //验证规则
      rules: {
        phone: [
          { required: true, message: '请输入' }
        ],
        name: [
          { required: true, message: '请输入' }
        ],
        username: [
          { required: true, message: '请输入' }
        ],
      },
    };
  },
  methods: {
    Func(func, res) {
      var _this = "_this";
      var dom = new Function(`return ${func}`)();
      console.log(dom, 'dom');
      dom && dom(_this, res);
    },
    handleShow(row) {
      // var func = 'function test(_this,res){res.name="sj";console.log(res,"===",_this)}';
      // var res = {name:'***',age:'21',sex:1,list:[1,2,3]};
      // debugger;
      // console.log(func)
      // this.Func(func,res);
      //   this.$router.push({
      //   path: "graph",
      //   query: {
      //     datasetId: row.id,
      //   },
      // });
      this.chartShow = false;
      this.$API.bi.dataset.exec
        .req(row.id, { is_test: true })
        .then((res) => {
          this.bidata = res.data
          if (res.echart_options) {
            this.myOption = JSON.parse(res.echart_options);
            this.chartShow = true;
          }

        });
    },
    //添加
    add() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("add");
      });
    },
    getAdmin(data) {
      this.adminform.admin = data.id;
      this.adminform.admin_name = data.name;
      this.adminform.name = data.name;
      this.adminform.phone = data.phone;
      this.adminform.username = data.username;
    },
    //编辑
    table_edit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    //查看
    table_show(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("show").setData(row);
      });
    },
    //删除
    async table_del(row) {
      this.$API.bi.dataset.delete.req(row.id)
        .then((res) => {
          this.$message.success("删除成功");
          this.$refs.table.refresh();
          return res;
        })
        .catch((err) => {
          return err;
        });
    },

    //本地更新数据
    handleSaveSuccess(data, mode) {
      if (mode == "add") {
        this.$refs.table.refresh();
      } else if (mode == "edit") {
        this.$refs.table.refresh();
      }
    },
    handleQuery() {
      this.$refs.table.queryData(this.query)
    },
    resetQuery() {
      this.query = {};
    },
  },
};
</script>