<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="AD账号" prop="ipdz">
        <el-input
          v-model="queryParams.ipdz"
          placeholder="请输入AD账号"
          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 v-loading="loading"  border :data="tpInfoList" @selection-change="handleSelectionChange">
      <el-table-column label="姓名" align="center" width="100px" prop="name" />
      <el-table-column label="AD账号" align="center" prop="ipdz" width="120px"  />
      <el-table-column label="投票时间" align="center" prop="createTime" width="155px"  />
      <el-table-column prop="dwmc" label="投票单位" align="left" >
<!--        <template slot-scope="scope">-->
<!--          <el-tooltip popper-class="table-tooltip" :content="scope.row.dwmc" placement="top">-->
<!--            <div class="long_title">-->
<!--              <span>{{scope.row.dwmc}}</span>-->
<!--            </div>-->
<!--          </el-tooltip>-->
<!--        </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 { listTpInfo, listTpInfoJg,getTpInfo, delTpInfo, addTpInfo, updateTpInfo } from "@/api/tp/tpInfo";

export default {
  name: "TpInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTpInfoCb: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投票信息表格数据
      tpInfoList: [],
      // 投票信息从表格数据
      tpInfoCbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sfzh: null,
        ipdz: null,
        yl1: null,
        yl2: null,
        yl3: null,
        yl4: null,
        yl5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "录入时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询投票信息列表 */
    getList() {
      this.loading = true;
      listTpInfoJg(this.queryParams).then(response => {
        this.tpInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sfzh: null,
        ipdz: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        yl1: null,
        yl2: null,
        yl3: null,
        yl4: null,
        yl5: null
      };
      this.tpInfoCbList = [];
      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
      getTpInfo(id).then(response => {
        this.form = response.data;
        this.tpInfoCbList = response.data.tpInfoCbList;
        this.open = true;
        this.title = "修改投票信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tpInfoCbList = this.tpInfoCbList;
          if (this.form.id != null) {
            updateTpInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTpInfo(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 delTpInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 投票信息从序号 */
    rowTpInfoCbIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 投票信息从添加按钮操作 */
    handleAddTpInfoCb() {
      let obj = {};
      obj.ssbk = "";
      obj.dwid = "";
      obj.dwmc = "";
      this.tpInfoCbList.push(obj);
    },
    /** 投票信息从删除按钮操作 */
    handleDeleteTpInfoCb() {
      if (this.checkedTpInfoCb.length == 0) {
        this.$modal.msgError("请先选择要删除的投票信息从数据");
      } else {
        const tpInfoCbList = this.tpInfoCbList;
        const checkedTpInfoCb = this.checkedTpInfoCb;
        this.tpInfoCbList = tpInfoCbList.filter(function(item) {
          return checkedTpInfoCb.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTpInfoCbSelectionChange(selection) {
      this.checkedTpInfoCb = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tpInfo/export', {
        ...this.queryParams
      }, `tpInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.table-tooltip {
  max-width: 500px;
}

.long_title{
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}


</style>
