<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="ssbk">
        <el-select v-model="queryParams.ssbk"  clearable>
          <el-option
            v-for="dict in dict.type.tp_ssbk"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单位名称" prop="dwmc">
        <el-input
          v-model="queryParams.dwmc"
          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 v-loading="loading" border :data="tpDeptList" @selection-change="handleSelectionChange">
      <el-table-column label="所属板块" min-width="180px" align="center" prop="ssbk" />
      <el-table-column label="单位名称"   min-width="180px" align="center" prop="dwmc" />
      <el-table-column label="投票数量"  min-width="180px" align="center" prop="sl" />

    </el-table>

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

<script>
import { listTpDept,listTpDeptjg, getTpDept, delTpDept, addTpDept, updateTpDept } from "@/api/tp/tpDept";

export default {
  name: "TpDept",
  dicts: [ 'tp_ssbk'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投票单位表格数据
      tpDeptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ssbk: null,
        dwmc: null,
        yl1: null,
        yl2: null,
        yl3: null,
        yl4: null,
        yl5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询投票单位列表 */
    getList() {
      this.loading = true;
      listTpDeptjg(this.queryParams).then(response => {
        this.tpDeptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ssbk: null,
        dwmc: null,
        remark: null,
        yl1: null,
        yl2: null,
        yl3: null,
        yl4: null,
        yl5: 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
      getTpDept(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) {
            updateTpDept(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTpDept(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 delTpDept(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tpDept/export', {
        ...this.queryParams
      }, `tpDept_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
