<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="adzh">
        <el-input
          v-model="queryParams.adzh"
          placeholder="请输入AD账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <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="dwdm">
        <el-input
          v-model="queryParams.dwdm"
          placeholder="请输入单位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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 label="二级单位编码" prop="ejdwdm">
        <el-input
          v-model="queryParams.ejdwdm"
          placeholder="请输入二级单位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级单位名称" prop="ejdwmc">
        <el-input
          v-model="queryParams.ejdwmc"
          placeholder="请输入二级单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留1" prop="yl1">
        <el-input
          v-model="queryParams.yl1"
          placeholder="请输入预留1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留2" prop="yl2">
        <el-input
          v-model="queryParams.yl2"
          placeholder="请输入预留2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留3" prop="yl3">
        <el-input
          v-model="queryParams.yl3"
          placeholder="请输入预留3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留4" prop="yl4">
        <el-input
          v-model="queryParams.yl4"
          placeholder="请输入预留4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留5" prop="yl5">
        <el-input
          v-model="queryParams.yl5"
          placeholder="请输入预留5"
          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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tpRyInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="AD账号" align="center" prop="adzh" />
      <el-table-column label="统一账号" align="center" prop="username" />
      <el-table-column label="单位编码" align="center" prop="dwdm" />
      <el-table-column label="单位名称" align="center" prop="dwmc" />
      <el-table-column label="完整单位编码" align="center" prop="wzdwdm" />
      <el-table-column label="完整单位名称" align="center" prop="wzdwmc" />
      <el-table-column label="二级单位编码" align="center" prop="ejdwdm" />
      <el-table-column label="二级单位名称" align="center" prop="ejdwmc" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="预留1" align="center" prop="yl1" />
      <el-table-column label="预留2" align="center" prop="yl2" />
      <el-table-column label="预留3" align="center" prop="yl3" />
      <el-table-column label="预留4" align="center" prop="yl4" />
      <el-table-column label="预留5" align="center" prop="yl5" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改人员投票信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="AD账号" prop="adzh">
          <el-input v-model="form.adzh" placeholder="请输入AD账号" />
        </el-form-item>
        <el-form-item label="统一账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入统一账号" />
        </el-form-item>
        <el-form-item label="单位编码" prop="dwdm">
          <el-input v-model="form.dwdm" placeholder="请输入单位编码" />
        </el-form-item>
        <el-form-item label="单位名称" prop="dwmc">
          <el-input v-model="form.dwmc" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="完整单位编码" prop="wzdwdm">
          <el-input v-model="form.wzdwdm" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="完整单位名称" prop="wzdwmc">
          <el-input v-model="form.wzdwmc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="二级单位编码" prop="ejdwdm">
          <el-input v-model="form.ejdwdm" placeholder="请输入二级单位编码" />
        </el-form-item>
        <el-form-item label="二级单位名称" prop="ejdwmc">
          <el-input v-model="form.ejdwmc" placeholder="请输入二级单位名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预留1" prop="yl1">
          <el-input v-model="form.yl1" placeholder="请输入预留1" />
        </el-form-item>
        <el-form-item label="预留2" prop="yl2">
          <el-input v-model="form.yl2" placeholder="请输入预留2" />
        </el-form-item>
        <el-form-item label="预留3" prop="yl3">
          <el-input v-model="form.yl3" placeholder="请输入预留3" />
        </el-form-item>
        <el-form-item label="预留4" prop="yl4">
          <el-input v-model="form.yl4" placeholder="请输入预留4" />
        </el-form-item>
        <el-form-item label="预留5" prop="yl5">
          <el-input v-model="form.yl5" placeholder="请输入预留5" />
        </el-form-item>
        <el-divider content-position="center">人员投票信息从信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTpRyInfoCb">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTpRyInfoCb">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="tpRyInfoCbList" :row-class-name="rowTpRyInfoCbIndex" @selection-change="handleTpRyInfoCbSelectionChange" ref="tpRyInfoCb">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="所属板块(安全、生产、设备、企管、党建)" prop="ssbk" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.ssbk" placeholder="请输入所属板块(安全、生产、设备、企管、党建)" />
            </template>
          </el-table-column>
          <el-table-column label="投票人员id" prop="tpryid" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.tpryid" placeholder="请输入投票人员id" />
            </template>
          </el-table-column>
          <el-table-column label="投票人员名称" prop="tprymc" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.tprymc" placeholder="请输入投票人员名称" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <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 { listTpRyInfo, getTpRyInfo, delTpRyInfo, addTpRyInfo, updateTpRyInfo } from "@/api/rytp/tpRyInfo";

export default {
  name: "TpRyInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTpRyInfoCb: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员投票信息表格数据
      tpRyInfoList: [],
      // 人员投票信息从表格数据
      tpRyInfoCbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        adzh: null,
        username: null,
        dwdm: null,
        dwmc: null,
        wzdwdm: null,
        wzdwmc: null,
        ejdwdm: null,
        ejdwmc: 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;
      listTpRyInfo(this.queryParams).then(response => {
        this.tpRyInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        adzh: null,
        username: null,
        dwdm: null,
        dwmc: null,
        wzdwdm: null,
        wzdwmc: null,
        ejdwdm: null,
        ejdwmc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        yl1: null,
        yl2: null,
        yl3: null,
        yl4: null,
        yl5: null
      };
      this.tpRyInfoCbList = [];
      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
      getTpRyInfo(id).then(response => {
        this.form = response.data;
        this.tpRyInfoCbList = response.data.tpRyInfoCbList;
        this.open = true;
        this.title = "修改人员投票信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tpRyInfoCbList = this.tpRyInfoCbList;
          if (this.form.id != null) {
            updateTpRyInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTpRyInfo(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 delTpRyInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 人员投票信息从序号 */
    rowTpRyInfoCbIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 人员投票信息从添加按钮操作 */
    handleAddTpRyInfoCb() {
      let obj = {};
      obj.ssbk = "";
      obj.tpryid = "";
      obj.tprymc = "";
      this.tpRyInfoCbList.push(obj);
    },
    /** 人员投票信息从删除按钮操作 */
    handleDeleteTpRyInfoCb() {
      if (this.checkedTpRyInfoCb.length == 0) {
        this.$modal.msgError("请先选择要删除的人员投票信息从数据");
      } else {
        const tpRyInfoCbList = this.tpRyInfoCbList;
        const checkedTpRyInfoCb = this.checkedTpRyInfoCb;
        this.tpRyInfoCbList = tpRyInfoCbList.filter(function(item) {
          return checkedTpRyInfoCb.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTpRyInfoCbSelectionChange(selection) {
      this.checkedTpRyInfoCb = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tpRyInfo/export', {
        ...this.queryParams
      }, `tpRyInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
