<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item :label="$t('cardlist.银行卡号')" prop="number">
        <el-input
          v-model="queryParams.number"
          :placeholder="$t('cardlist.请输入银行卡号')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('cardlist.开户银行')" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          :placeholder="$t('cardlist.请输入开户银行')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('cardlist.账户名称')" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          :placeholder="$t('cardlist.请输入账户名称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('cardlist.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('cardlist.重置')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:card:export']"
        >{{$t('cardlist.导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column :label="$t('cardlist.用户id')" align="center" prop="userId" />
      <el-table-column :label="$t('cardlist.银行卡号')" align="center" prop="number" />
      <el-table-column :label="$t('cardlist.开户银行')" align="center" prop="bankName" />
      <el-table-column :label="$t('cardlist.账户名称')" align="center" prop="accountName" />
      <el-table-column :label="$t('cardlist.卡类型')" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type==0" type="info" effect="dark">{{generateTitle('储蓄卡')}}</el-tag>
          <el-tag v-if="scope.row.type==1" type="primary" effect="dark">{{generateTitle('信用卡')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('cardlist.图标')" align="center" prop="img" />
      <el-table-column :label="$t('cardlist.操作')" 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)"
            v-hasPermi="['system:card:edit']"
          >{{$t('cardlist.详情')}}</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"
    />

    <!-- 添加或修改BankCard对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :label-width="$t('message.language')=='语言'?'80px':$t('message.language')=='language'?'165px':'165px'">
        <el-form-item :label="$t('cardlist.用户id')" prop="userId">
          <div>{{form.userId}}</div>
        </el-form-item>
        <el-form-item :label="$t('cardlist.银行卡号')" prop="number">
          <div>{{form.number}}</div>
        </el-form-item>
        <el-form-item :label="$t('cardlist.开户银行')" prop="bankName">
          <div>{{form.bankName}}</div>
        </el-form-item>
        <el-form-item :label="$t('cardlist.账户名称')" prop="accountName">
          <div>{{form.accountName}}</div>
        </el-form-item>
        <el-form-item :label="$t('cardlist.卡类型')" prop="type">
          <div>{{form.type==1?generateTitle('信用卡'):generateTitle('储蓄卡')}}</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">{{$t('cardlist.确定')}}</el-button>
        <el-button @click="cancel">{{$t('cardlist.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCard, getCard, delCard, addCard, updateCard } from "@/api/system/card";
import {generateTitle} from '../../../utils/i18.js'
export default {
  name: "Card",
  dicts: ['sys_bank_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // BankCard表格数据
      cardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: null,
        bankName: null,
        accountName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    generateTitle,
    /** 查询BankCard列表 */
    getList() {
      this.loading = true;
      listCard(this.queryParams).then(response => {
        this.cardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        number: null,
        bankName: null,
        accountName: null,
        type: null,
        img: 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 = "添加BankCard";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCard(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('cardlist.详请');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除BankCard编号为"' + ids + '"的数据项？').then(function() {
        return delCard(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/card/export', {
        ...this.queryParams
      }, `card_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
