<template>
  <div class="app-container" ref="appPanel">
    <div ref="toolPanel">
    </div>
    <div class="search-container" ref="searchPanel">
<!--
      <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="" prop="msg">
          <el-input
            v-model="queryParams.msg"
            placeholder="请输入"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="" prop="orders">
          <el-input
            v-model="queryParams.orders"
            placeholder="请输入"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-form>
-->
    </div>
    <div class="table-container">
      <div ref="mainTablePanel" :style="{height: mainTableDivHeight}">
        <el-table v-loading="loading" border size="mini" :height="mainTableHeight" :data="logList"
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="行号" align="center" prop="id" v-if="columns[0].visible"/>
          <el-table-column label="错误代码" align="center" prop="code" v-if="columns[1].visible"/>
          <el-table-column label="错误信息" align="center" prop="msg" v-if="columns[2].visible"/>
          <el-table-column label="出库单号" align="center" prop="orders" v-if="columns[3].visible"/>
          <el-table-column label="推送参数" align="center" prop="obj" v-if="columns[4].visible"/>

          <!-- New Actions Column -->
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="handleDelete(scope.row)">操作按钮</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagePanel">
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>
    </div>

    <!-- 添加或修改【请填写功能名称】对话框 -->
  </div>
</template>

<script>
import { listLog, push, delLog, addLog, updateLog, disabled, enabled, changeStatus } from "@/api/system/log";
import { listCOLUMN } from "@/api/system/column";

export default {
  name: "Log",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      logList: [],
      // 弹出层标题
      title: "",
      mainTableHeight:"0px",
      mainTableDivHeight:"0px",
      businessCodeTrk: "log",
      rightToolbarShow: false,
      tableNo: 0,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        code: null,
        msg: null,
        orders: null,
        obj: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      columns: [
        { key: 0, label: `行号`, visible: true, columnName: "id" },
        { key: 1, label: `错误代码`, visible: true, columnName: "code" },
        { key: 2, label: `错误信息`, visible: true, columnName: "msg" },
        { key: 3, label: `出库单号`, visible: true, columnName: "orders" },
        { key: 4, label: `参数`, visible: true, columnName: "obj" },
      ]
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.computedTableHeight();
  },
  watch:{
    'showSearch': function() {
      this.computedTableHeight();
    }
  },
  methods: {
    /** 查询用户展示字段定义管理列表 */
    getHideColumn() {
      var queryHideColumnParams = {
        pageNum: 1,
        pageSize: 100,
        businessCode: this.businessCodeTrk,
        tableNo: this.tableNo
      };
      listCOLUMN(queryHideColumnParams).then(response => {
        if(response.rows.length > 0) {
          var row = response.rows[0];
          var columnHide = row.columnHide.split(",");
          if (columnHide.length > 0) {
            for (let item in this.columns) {
              if (columnHide.indexOf(this.columns[item].columnName) > -1) {
                this.columns[item].visible = false;
              }
            }
          }
        }
        this.rightToolbarShow = true;
      });
    },
    computedTableHeight(){
      this.$nextTick(() => {
        var totalHeight = this.$refs.appPanel.clientHeight;
        var toolHeight = this.$refs.toolPanel.clientHeight;
        var searchHeight = this.$refs.searchPanel.clientHeight;

        var tablePanelHeight = totalHeight - toolHeight -searchHeight - 14 - 5;

        this.mainTableDivHeight = tablePanelHeight + "px";
        this.mainTableHeight = (tablePanelHeight - 25) + "px";

      })
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listLog(this.queryParams).then(response => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.getHideColumn();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        msg: null,
        orders: null,
        obj: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */


    handleDelete(row) {
      this.$modal.confirm('是否确认重新推送"' + row.id + '"的数据项？').then(function() {
        return push(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("推送成功");
      }).catch(() => {});
    },

    /*/!** 用户状态修改 *!/
    handleStatusChange (row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '"id为"' + row.id + '"数据项？').then(function () {
        return changeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /!** 启用按钮操作 *!/
    handleEnable (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认启用【请填写功能名称】编号为"' + ids + '"的数据项？').then(function () {
        return enabled(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("启用成功");
      }).catch(() => { });
    },
    /!** 禁用按钮操作 *!/
    handleDisabled (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认禁用【请填写功能名称】编号为"' + ids + '"的数据项？').then(function () {
        return disabled(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("禁用成功");
      }).catch(() => { });
    },

    /!** 导出按钮操作 *!/
    handleExport() {
      this.download('system/log/export', {
        ...this.queryParams
      }, `log_${new Date().getTime()}.xlsx`)
    }*/
  }
};
</script>
<style scoped>
.app-container {
  height: calc(100vh - 84px);
}
</style>
