<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">
          <el-form-item v-show="false" label="所属用户" prop="user.id" hidden="hidden">
            <el-input v-model="form.user.id" />
          </el-form-item>
          <el-form-item label="所属用户" prop="user.nickName">
            <el-input v-model="form.user.nickName" disabled />
          </el-form-item>
          <el-form-item label="银行卡号" prop="bankNo">
            <el-input v-model="form.bankNo" style="" />
          </el-form-item>
          <el-form-item label="银行名称" prop="bankName">
            <el-input v-model="form.bankName" />
          </el-form-item>
          <el-form-item label="是否常用账号">
            <el-radio-group v-model="form.bankUse">
              <el-radio label="是">是</el-radio>
              <el-radio label="否">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="" />
          </el-form-item>
          <el-form-item label="开户行">
            <el-input v-model="form.bankLocation" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="user.nickName" label="所属用户" />
        <el-table-column prop="bankNo" label="银行卡号" />
        <el-table-column prop="bankName" label="银行名称" />
        <el-table-column prop="bankLocation" label="开户行" />
        <el-table-column prop="bankUse" label="是否常用账号" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column v-if="checkPer(['admin','userBank:edit','userBank:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudUserBank from '@/api/userBank'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'

const defaultForm = { user: {}, bankId: null, userId: null, bankNo: null, bankName: null, bankUse: null, remark: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'UserBank',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '银行卡信息', url: 'api/userBank', idField: 'bankId', sort: 'bankId,desc', crudMethod: { ...crudUserBank }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'userBank:add'],
        edit: ['admin', 'userBank:edit'],
        del: ['admin', 'userBank:del']
      },
      rules: {
        userId: [
          { required: true, message: '所属用户不能为空', trigger: 'blur' }
        ],
        bankNo: [
          { required: true, message: '银行卡号不能为空', trigger: 'blur' }
        ],
        bankName: [
          { required: true, message: '银行卡所属支行不能为空', trigger: 'blur' }
        ]
      }}
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToAdd]() {
      this.form.user.nickName = this.user.nickName
      this.form.user.id = this.user.id
    }
  }
}
</script>

<style scoped>

</style>
