<template>
  <div class="app-container" ref="appPanel">
    <div ref="toolPanel">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:COLUMN:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:COLUMN:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
        <el-button
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:COLUMN:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="multiple"
            @click="handleEnable"
            v-hasPermi="['system:COLUMN:edit']"
          >启用</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDisabled"
            v-hasPermi="['system:COLUMN:remove']"
          >禁用</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:COLUMN:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            icon="el-icon-refresh"
            size="mini"
            @click="resetQuery"
            >刷新</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" showColumnsType="transfer" :columns="columns"></right-toolbar>
      </el-row>
    </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="userName">
                    <el-input
                        v-model="queryParams.userName"
                        placeholder="请输入用户账号"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="业务类型" prop="businessCode">
                    <el-input
                        v-model="queryParams.businessCode"
                        placeholder="请输入业务类型"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="数据表序号" prop="tableNo">
                    <el-input
                        v-model="queryParams.tableNo"
                        placeholder="请输入数据表序号"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="删除标志" prop="delFlag">
                    <el-input
                        v-model="queryParams.delFlag"
                        placeholder="请输入删除标志"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="最后登录IP" prop="loginIp">
                    <el-input
                        v-model="queryParams.loginIp"
                        placeholder="请输入最后登录IP"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="最后登录时间" prop="loginDate">
                    <el-date-picker clearable
                                    v-model="queryParams.loginDate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择最后登录时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="创建者" prop="createBy">
                    <el-input
                        v-model="queryParams.createBy"
                        placeholder="请输入创建者"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="创建时间" prop="createTime">
                    <el-date-picker clearable
                                    v-model="queryParams.createTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择创建时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="更新者" prop="updateBy">
                    <el-input
                        v-model="queryParams.updateBy"
                        placeholder="请输入更新者"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="更新时间" prop="updateTime">
                    <el-date-picker clearable
                                    v-model="queryParams.updateTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择更新时间">
                    </el-date-picker>
                  </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="COLUMNList"
        @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="ID" align="center" prop="ID" v-if="columns[0].visible"/>
              <el-table-column label="用户账号" align="center" prop="userName" v-if="columns[1].visible"/>
              <el-table-column label="业务类型" align="center" prop="businessCode" v-if="columns[2].visible"/>
              <el-table-column label="数据表序号" align="center" prop="tableNo" v-if="columns[3].visible"/>
              <el-table-column label="显示字段" align="center" prop="columnShow" v-if="columns[4].visible"/>
              <el-table-column label="隐藏字段" align="center" prop="columnHide" v-if="columns[5].visible"/>
              <el-table-column label="删除标志" align="center" prop="delFlag" v-if="columns[6].visible"/>
              <el-table-column label="最后登录IP" align="center" prop="loginIp" v-if="columns[7].visible"/>
              <el-table-column label="最后登录时间" align="center" prop="loginDate" width="180" v-if="columns[8].visible">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="创建者" align="center" prop="createBy" v-if="columns[9].visible"/>
              <el-table-column label="创建时间" align="center" prop="createTime" width="180" v-if="columns[10].visible">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}-{m}-{s}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="更新者" align="center" prop="updateBy" v-if="columns[11].visible"/>
              <el-table-column label="更新时间" align="center" prop="updateTime" width="180" v-if="columns[12].visible">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}-{m}-{s}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="备注" align="center" prop="remark" v-if="columns[13].visible"/>
        </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>

    <!-- 添加或修改用户展示字段定义管理对话框 -->
    <el-dialog id="dialog-container" :title="title" :visible.sync="open" width="600px" append-to-body>
      <div class="edit-container">
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
                        <el-row>
                      <el-col :span="12">
                        <el-form-item label="用户账号" prop="userName">
                          <el-input v-model="form.userName" placeholder="请输入用户账号" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="业务类型" prop="businessCode">
                          <el-input v-model="form.businessCode" placeholder="请输入业务类型" />
                        </el-form-item>
                      </el-col>
                        </el-row>
                        <el-row>
                      <el-col :span="12">
                        <el-form-item label="数据表序号" prop="tableNo">
                          <el-input v-model="form.tableNo" placeholder="请输入数据表序号" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="显示字段" prop="columnShow">
                          <el-input v-model="form.columnShow" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                      </el-col>
                        </el-row>
                        <el-row>
                      <el-col :span="12">
                        <el-form-item label="隐藏字段" prop="columnHide">
                          <el-input v-model="form.columnHide" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="删除标志" prop="delFlag">
                          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
                        </el-form-item>
                      </el-col>
                        </el-row>
                        <el-row>
                      <el-col :span="12">
                        <el-form-item label="最后登录IP" prop="loginIp">
                          <el-input v-model="form.loginIp" placeholder="请输入最后登录IP" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="最后登录时间" prop="loginDate">
                          <el-date-picker clearable
                                          v-model="form.loginDate"
                                          type="date"
                                          value-format="yyyy-MM-dd"
                                          placeholder="请选择最后登录时间">
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                        </el-row>
                        <el-row>
                      <el-col :span="12">
                        <el-form-item label="备注" prop="remark">
                          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                      </el-col>
          </el-row>
      </el-form>
      </div>
      <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 { addCOLUMN, changeStatus, delCOLUMN, disabled, enabled, getCOLUMN, listCOLUMN, updateCOLUMN } from "@/api/system/column";

  export default {
    name: "column",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户展示字段定义管理表格数据
              COLUMNList: [],
        // 弹出层标题
        title: "",
        mainTableHeight:"0px",
        mainTableDivHeight:"0px",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 100,
          userName: null,
          businessCode: null,
          tableNo: null,
          columnShow: null,
          columnHide: null,
          delFlag: null,
          loginIp: null,
          loginDate: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
                        userName: [
                    { required: true, message: "用户账号不能为空", trigger: "blur" }
                  ],
                        businessCode: [
                    { required: true, message: "业务类型不能为空", trigger: "blur" }
                  ],
                        tableNo: [
                    { required: true, message: "数据表序号不能为空", trigger: "blur" }
                  ],
                        columnShow: [
                    { required: true, message: "显示字段不能为空", trigger: "blur" }
                  ],
                        columnHide: [
                    { required: true, message: "隐藏字段不能为空", trigger: "blur" }
                  ],
        },
        columns: [
            { key: 0, label: `$comment`, visible: true },
            { key: 1, label: `用户账号`, visible: true },
            { key: 2, label: `业务类型`, visible: true },
            { key: 3, label: `数据表序号`, visible: true },
            { key: 4, label: `显示字段`, visible: true },
            { key: 5, label: `隐藏字段`, visible: true },
            { key: 6, label: `删除标志`, visible: true },
            { key: 7, label: `最后登录IP`, visible: true },
            { key: 8, label: `最后登录时间`, visible: true },
            { key: 9, label: `创建者`, visible: true },
            { key: 10, label: `创建时间`, visible: true },
            { key: 11, label: `更新者`, visible: true },
            { key: 12, label: `更新时间`, visible: true },
            { key: 13, label: `备注`, visible: true },
        ]
      };
    },
    created() {
      this.getList();
    },
    mounted() {
      this.computedTableHeight();
    },
    watch:{
      'showSearch': function() {
        this.computedTableHeight();
      }
    },
    methods: {
      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;
        listCOLUMN(this.queryParams).then(response => {
          this.COLUMNList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
                        ID: null,
                        userName: null,
                        businessCode: null,
                        tableNo: null,
                        columnShow: null,
                        columnHide: null,
                        delFlag: null,
                        loginIp: null,
                        loginDate: null,
                        createBy: null,
                        createTime: null,
                        updateBy: null,
                        updateTime: null,
                        remark: 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
        getCOLUMN(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) {
              updateCOLUMN(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCOLUMN(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const IDs = row.ID || this.ids;
        this.$modal.confirm('是否确认删除用户展示字段定义管理编号为"' + IDs + '"的数据项？').then(function() {
          return delCOLUMN(IDs);
        }).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/COLUMN/export', {
      ...this.queryParams
    }, `COLUMN_${new Date().getTime()}.xlsx`)
  }
  }
  };
</script>
<style scoped>
 .app-container {
  height: calc(100vh - 84px);
 }
</style>
