<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <button-bar view-title=" 公司银行账号管理" ref="btnbar" :btndisabled="btndisabled" @btnhandle="btnHandle"/>
      <div class="main-content">
        <div class="searchcon">
          <el-button size="small" class="searchbtn" @click="clicksearchShow">{{ searchxianshitit }}</el-button>
          <div v-show="isSearchShow" class="search">
            <el-form ref="listQueryform" :inline="true" :model="listQuery" label-width="110px" class="tab-header">
              <el-form-item label="账户类型">
                <el-form-item prop="dueBank"> <!--dueBankKey   dueBankValue-->
                  <el-select v-model="listQuery.params.dueBankKey" clearable class="addinputw" placeholder="">
                    <el-option v-for="item in receiptBank_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"/>
                  </el-select>
                </el-form-item>
              </el-form-item>
              <el-form-item label="开户银行">
                <el-select v-model="listQuery.params.depositBankKey" clearable class="addinputw" placeholder="">
                  <el-option v-for="item in depositBank_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"/>
                </el-select>
              </el-form-item>
            </el-form>
            <div class="btn">
              <el-button type="primary" icon="el-icon-search" size="small" @click="handleFilter">查询</el-button>
              <el-button type="primary" icon="el-icon-refresh" size="small" @click="handleReset">重置</el-button>
            </div>
          </div>
        </div>
        <div class="listtop">
          <div class="tit">公司银行账号列表</div>
          <!-- 翻页分页 -->
          <pageye v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
        </div>
        <div>
          <el-table :key="tableKey" v-loading="listLoading" :data="list" border style="width: 100%;" @selection-change="handleSelectionChange">
            <el-table-column type="selection" align="center" width="50"/>
            <el-table-column label="序号" fixed type="index" width="80" :index="indexMethod" align="center"/>
            <el-table-column label="操作" fixed align="center" width="160" class-name="small-padding fixed-width">
              <template slot-scope="{row}">
                <el-button size="mini" type="primary" @click="handleEdit(row)">编辑</el-button>
                <el-button size="mini" type="primary" @click="handleCheck(row)">查看</el-button>
              </template>
            </el-table-column>
            <el-table-column prop="dueBankValue" width="140" label="账户类型" align="center"/>
            <el-table-column prop="depositBank" label="开户银行" align="center"/>
            <el-table-column prop="bankAccount" width="270" label="银行账号" align="center"/>
            <el-table-column prop="accountName" label="账户名称" align="center"/>
          </el-table>
        </div>
        <div class="pages">
          <!-- 翻页 -->
          <pagination v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
        </div>
      </div>
    </div>
    <finaccountnum-add v-show="viewState == 2 || viewState == 3" ref="divadd" @doback="resetState" @reloadlist="handleFilter"/>
    <finaccountnum-info v-show="viewState == 4" ref="divinfo" @doback="resetState"/>
  </div>
</template>

<script>
import FinaccountnumAdd from '@/views/anruifinmanagement/finaccountnum/finaccountnumAdd'
import FinaccountnumInfo from '@/views/anruifinmanagement/finaccountnum/finaccountnumInfo'
import Pagination from '@/components/pagination'
import pageye from '@/components/pagination/pageye'
import ButtonBar from '@/components/ButtonBar'
import { getPathSidByUserSid, typeValues } from '@/api/jichuxinxi/dictcommons'
import { listPage, delBySids } from '@/api/anruifinmanagement/finaccountnum'

export default {
  name: 'finaccountnum',
  components: {
    FinaccountnumInfo,
    FinaccountnumAdd,
    ButtonBar,
    Pagination,
    pageye
  },
  data() {
    return {
      btndisabled: false,
      btnList: [
        {
          type: 'primary',
          size: 'small',
          icon: 'plus',
          btnKey: 'toAdd',
          btnLabel: '新增'
        },
        {
          type: 'danger',
          size: 'small',
          icon: 'del',
          btnKey: 'doDel',
          btnLabel: '删除'
        },
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      viewState: 1,
      isSearchShow: false,
      searchxianshitit: '显示查询条件',
      sids: [],
      receiptBank_list: [],
      depositBank_list: [],
      useOrg: '', // 当前登录人的分公司sid
      // 查询 -----------
      tableKey: 0,
      list: [],
      listLoading: false,
      listQuery: {
        params: {
          dueBankKey: '',
          depositBankKey: '',
          userSid: ''
        },
        current: 1,
        size: 5,
        total: 0
      },
      rules: {}
      // ------------------------------------
    }
  },
  mounted() {
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  created() {
    this.getList()
    this.getType()
    this.getPathSid()
  },
  methods: {
    resetState() {
      this.viewState = 1
    },
    btnHandle(btnKey) {
      console.log('XXXXXXXXXXXXXXX    ' + btnKey)
      switch (btnKey) {
        case 'toAdd':
          this.toAdd()
          break
        case 'doDel':
          this.doDel()
          break
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    // 搜索条件效果
    clicksearchShow() {
      this.isSearchShow = !this.isSearchShow
      if (this.isSearchShow) {
        this.searchxianshitit = '隐藏查询条件'
      } else {
        this.searchxianshitit = '显示查询条件'
      }
    },
    getType() {
      typeValues({ type: 'accountType' }).then((res) => {
        if (res.success) {
          this.receiptBank_list = res.data
        }
      })
      typeValues({ type: 'bank' }).then((res) => {
        if (res.success) {
          this.depositBank_list = res.data
        }
      })
    },
    getPathSid() {
      const userSid = window.sessionStorage.getItem('userSid')
      getPathSidByUserSid({userSid: userSid}).then((res) => {
        if (res.success) {
          this.useOrg = res.data
        }
      })
    },
    // 序号
    indexMethod(index) {
      var pagestart = (this.listQuery.current - 1) * this.listQuery.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    // 查询列表信息
    getList() {
      this.listLoading = true
      this.listQuery.params.userSid = window.sessionStorage.getItem('userSid')
      listPage(this.listQuery).then((response) => {
        this.listLoading = false
        if (response.code === '200' && response.data && response.data.total > 0) {
          this.list = response.data.records
          this.listQuery.total = response.data.total
        } else {
          this.list = []
          this.listQuery.total = 0
        }
      })
    },
    // 查询按钮
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.listQuery = {
        params: {
          dueBankKey: '',
          depositBankKey: '',
          userSid: ''
        },
        current: 1,
        size: 5
      }
      this.getList()
    },
    // ----------------------------------方法--------------------
    // 打开添加对话框
    toAdd() {
      this.viewState = 2
      this.$refs['divadd'].showAdd()
    },
    // 勾选
    handleSelectionChange(row) {
      const aa = []
      row.forEach((element) => {
        aa.push(element.sid)
      })
      this.sids = aa
    },
    // 根据本行ID删除数据
    doDel() {
      if (this.sids.length > 0) {
        const _this = this
        const tip = '请确认是否删除所选 ' + this.sids.length + ' 条记录?'
        this.$confirm(tip, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            const loading = this.$loading({
              lock: true,
              text: 'Loading',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            })
            delBySids(this.sids).then(resp => {
                if (resp.success) {
                  loading.close()
                  _this.$message({ type: 'success', message: resp.msg, showClose: true })
                  _this.getList()
                } else {
                  loading.close()
                }
              }).catch(e => {
                loading.close()
              })
          }).catch(() => {
          })
      } else {
        this.$message({type: 'error', message: '没有选择厂商!!', showClose: true})
        return
      }
    },
    // 修改
    handleEdit(row) {
      this.viewState = 3
      this.$refs['divadd'].showEdit(row.sid)
    },
    // 打开查看
    handleCheck(row) {
      this.viewState = 4
      this.$refs['divinfo'].showInfo(row.sid)
    },
    // 关闭
    doClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>

</style>
