<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模版名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入模版名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="文件地址" prop="url">
        <el-input v-model="queryParams.url" 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-table :border="Global.tableShowBorder" v-loading="loading" :data="agreementTplList" ref="tables" @select="select" @selection-change="handleSelectionChange" @row-click="rowClick">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模版名称" align="center" prop="name" />
      <el-table-column label="文件地址" align="center" prop="url" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

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

  </div>
</template>

<script>
import { listAgreementTpl, getAgreementTpl, delAgreementTpl, addAgreementTpl, updateAgreementTpl } from "@/api/ct/agreementTpl";
export default {
  name: "AgreementTpl",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 协议模版表格数据
      agreementTplList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: null,
        url: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    /** 查询协议模版列表 */
    getList() {
      this.loading = true;
      listAgreementTpl(this.queryParams).then(response => {
        this.agreementTplList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (selection && selection.length > 0) {
        console.log("选中一行");
        console.log(selection[0].id);
        this.$emit("selectedAgreementTpl", selection[0])
      }
    },
    select(selection, row) {
      // 清除 所有勾选项
      this.$refs.tables.clearSelection()
      // 当表格数据都没有被勾选的时候 就返回
      // 主要用于将当前勾选的表格状态清除
      if (selection.length == 0) return
      this.$refs.tables.toggleRowSelection(row, true);
    },
    rowClick(row, column) {
      const ids = this.ids
      this.$refs.tables.clearSelection()
      if (ids.length == 1) {
        ids.forEach(item => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item == row) {
            this.$refs.tables.toggleRowSelection(row, false);
          }
          // 不然就让当前的一行勾选
          else {
            this.$refs.tables.toggleRowSelection(row, true);
          }
        })
      }
      else {
        this.$refs.tables.toggleRowSelection(row, true);
      }
    },
    // 隐藏表头中的全选框
    cellClass(row) {
      console.log(row);
      if (row.columnIndex === 0) {
        return 'disabledCheckAll'
      }
    },
  }
};
</script>

<style scoped lang="scss">
/* 去掉全选按钮 */
::v-deep .el-table .disabledCheckAll .cell .el-checkbox__inner {
  display: none !important;
}

::v-deep .el-table .disabledCheckAll .cell::before {
  content: '文本';
  text-align: center;
  line-height: 37px;
}
</style>
