<template>
  <div class="app-container">
    <div class="flexBet searchTop">
      <!-- <div class="title flex m20">
        <div class="bar"></div>
        <div class="text">银行管理</div>
      </div> -->
    </div>

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="86px"
        class="flex form" style="position: relative">
        <el-form-item label="银行名称" prop="bankName">
          <el-input size="medium" v-model="queryParams.bankName" placeholder="请输入银行名称" @keyup.enter.native="handleQuery"
            style="width: 180px; border-color: #e6f1ff" />
        </el-form-item>

        <el-form-item>
          <div class="searchTop flexBet" style="align-items: center">
            <el-row :gutter="10" class="mb8">
              <el-button type="primary" icon="" size="medium" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
              <el-button type="primary" plain icon="" size="medium" @click="handleAdd()">添加银行</el-button>
            </el-row>

           
          </div>
        </el-form-item>
      </el-form>

      <!-- <div class="searchTop flexBet">
        <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain icon="" size="medium" @click="handleAdd()">添加银行</el-button>
              </el-col>
            </el-row>
        <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      
      </div> -->

    <div class="bg">
    

      <el-table stripe :data="infoList" v-loading="loading">
        <el-table-column label="银行名称" align="center" prop="bankName" />
        <el-table-column label="简称" align="center" prop="shortName" />
        <el-table-column label="机构" align="center" prop="organizationName" />
        <el-table-column label="机构代码" align="center" prop="organizationCode" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="handleUpdate(scope.row)" style="color: #006eff">编辑</el-button>
            <el-button size="small" type="text" @click="handleDelete(scope.row)" style="color: #006eff">删除</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">
      </pagination>
    </div>

    <!-- 添加或修改商户对话框 -->
    <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="bankName">
          <el-input v-model="form.bankName" placeholder="请输入银行名称" />
        </el-form-item>
        <el-form-item label="简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入简称" />
        </el-form-item>
        <el-form-item label="机构" prop="organizationName">
          <el-input v-model="form.organizationName" placeholder="请输入机构" />
        </el-form-item>
        <el-form-item label="机构代码" prop="organizationCode">
          <span>{{ form.organizationCode }}</span>
        </el-form-item>
      </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 { listBank, getBank, delBank, addBank, updateBank, exportBank } from "@/api/platform/bank";

export default {
  name: "Bank",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 银行表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bankName: null,
        organizationName: null,
        organizationCode: null,
        shortName: null
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        shortName: [
          { required: true, message: "简称不能为空", trigger: "blur" }
        ],
        bankName: [
          { required: true, message: "银行名称不能为空", trigger: "blur" }
        ],
        organizationName: [
          { required: true, message: "组织名称不能为空", trigger: "blur" }
        ],
        organizationCode: [
          { required: true, message: "组织code码不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "添加时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  $autoRefresh: {
    // 刷新间隔时间
    interval: 3000 * 10,
    // 立即执行一次
    immediate: false,
    // 被回调的函数, 不保证顺序执行
    handles: ["getList"],
    /**
     * 调用 handles 后的回调函数
     * 这个字段为数组类型, 数组中的每个元素都是一个函数, 在对应索引的 handle 执行后被调用
     */
    callbacks: [],
    // 是否启用调试模式
    debug: true,
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询银行列表 */
    getList() {
      this.loading = true;
      listBank(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = Number(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bankName: null,
        organizationName: null,
        organizationCode: null,
        createTime: null,
        shortName: 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 = "添加银行";
      this.form.organizationCode = 'UNION'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBank(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) {
            updateBank(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBank(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除银行编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delBank(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有银行数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportBank(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
