<template>
  <div class="funAccountList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" v-if="$hasPermissions('B_fundAccountEnable')" class="funAccountList-page-search-wrapper">
        <div>
          资金账户功能：
          <a-radio-group
            size="large"
            v-model="enableFundAccount"
            :options="[
              { label: '启用', value: '1' },
              { label: '禁用', value: '0' },
            ]"
            @change="enableFundAccountChange" />
        </div>
        <div>
          开启资金账户功能后，在进行财务收款和付款时，需要从下面列表选择一个账户。
        </div>
      </div>

      <a-card size="small" :bordered="false">
        <!-- 操作按钮 -->
        <div class="table-operator">
          <a-button id="funAccountList-add" v-if="$hasPermissions('B_FA_New')" type="primary" class="button-error" @click="handleEdit()">新增</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+84.5+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="(record.state == 'ENABLE'||record.state == 'DISABLE')&&$hasPermissions('B_FA_QYJY')"
              class="button-primary"
              @click="handleEnable(record)"
              id="foundAcount-enable-btn">
              {{ record.state == 'ENABLE'?'禁用':'' }}
              {{ record.state == 'DISABLE'?'启用':'' }}
            </a-button>
            <a-button
              size="small"
              type="link"
              v-if="(record.state == 'ENABLE'||record.state == 'DISABLE')&&$hasPermissions('M_fundAccount_detail')"
              class="button-primary"
              @click="handleDetail(record)"
              id="foundAcount-detail-btn">资金明细</a-button>
            <a-button
              size="small"
              type="link"
              v-if="record.state == 'WAIT'&&$hasPermissions('B_FA_Audit')"
              class="button-primary"
              @click="handleAudit(record)"
              id="foundAcount-audit-btn">审核</a-button>
            <a-button
              size="small"
              type="link"
              v-if="record.state == 'WAIT'&&$hasPermissions('B_FA_Edit')"
              class="button-primary"
              @click="handleEdit(record)"
              id="foundAcount-edit-btn">编辑</a-button>
            <a-button
              size="small"
              type="link"
              v-if="record.state == 'WAIT'&&$hasPermissions('B_FA_delete')"
              class="button-error"
              @click="handleDel(record)"
              id="foundAcount-del-btn">删除</a-button>
          </template>
        </s-table>
      </a-card>

    </a-spin>
    <!-- 新增编辑 -->
    <addModal ref="addModal" :openModal="openModal" @ok="addSuccess" @close="closeAddModal" />
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { mapActions } from 'vuex'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import addModal from './addModal.vue'
import { settleAccountList, settleAccountEnable, settleAccountDelete, settleAccountAudit } from '@/api/settleAcount'
export default {
  name: 'FundAccountManagementList',
  components: { STable, VSelect, addModal, rangeDate },
  mixins: [commonMixin],
  data () {
    const _this = this
    return {
      spinning: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      advanced: true, //  高级搜索 展开/关闭
      enableFundAccount: '', // 结算管理开启状态
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '账户类型', dataIndex: 'settleStyleName', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '账户名称', dataIndex: 'accountName', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '账户号码', dataIndex: 'accountNo', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收入金额', dataIndex: 'totalIncomeAmount', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '支出金额', dataIndex: 'totalExpendAmount', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '状态', dataIndex: 'stateDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return settleAccountList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      openModal: false, //  查看详情  弹框
      itemId: '' //  当前id
    }
  },
  methods: {
    ...mapActions(['UpdateSettleAccount', 'GetSettleAccountState']),
    enableFundAccountChange (v) {
      this.UpdateSettleAccount({ enableFlag: v.target.value }).then(res => {
        if (res.status == 200) {
          this.GetSettleAccountState()
        }
        this.$message.info(res.message)
      })
    },
    // 关闭弹框
    closeAddModal () {
      this.openModal = false
      this.itemId = null
    },
    //  新增/编辑
    handleEdit (row) {
      this.openModal = true
      this.$nextTick(() => {
        this.$refs.addModal.setData(row)
      })
    },
    addSuccess () {
      this.$refs.table.refresh()
    },
    //  资金明细
    handleDetail (row) {
      this.$router.push({ name: 'fundAccountDetail', params: { sn: row.settleAccountSn } })
    },
    //  审核
    handleAudit (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '审核通过后，账户信息不能再修改，确认审核通过吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          settleAccountAudit({
            settleAccountSn: row.settleAccountSn,
            auditState: 'PASS'
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
            _this.spinning = false
          })
        }
      })
    },
    // 启用，禁用
    handleEnable (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: row.state == 'ENABLE' ? '确认禁用该账户吗？' : '确认启用该账户吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          settleAccountEnable({
            settleAccountSn: row.settleAccountSn,
            enableFlag: row.state == 'ENABLE' ? 'DISABLE' : 'ENABLE'
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
            _this.spinning = false
          })
        }
      })
    },
    //  删除
    handleDel (row) {
      const _this = this
      _this.$confirm({
        title: '提示',
        content: '删除后原数据不可恢复，是否删除？',
        centered: true,
        onOk () {
          _this.spinning = true
          settleAccountDelete({ settleAccountSn: row.settleAccountSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
            _this.spinning = false
          })
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      // 结算账户状态查询
      if (this.$hasPermissions('M_fundAccountList') && this.$hasPermissions('B_fundAccountEnable')) {
      	this.GetSettleAccountState().then(res => {
      	  if (res.status == 200) {
      		this.enableFundAccount = res.data.functionEnableFlag
      	  }
      	})
      }
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 270
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .funAccountList-wrap{
    .funAccountList-page-search-wrapper{
      font-size: 16px;
      padding:10px 15px;
      margin-bottom: 8px;
      background-color: #fff;
      > div{
        padding: 5px 0;
        &:last-child{
          color: #999;
          font-size: 14px;
        }
      }
    }
    .sTable{
      margin-top: 10px;
    }
	.active{
		color: #ed1c24;
		cursor: pointer;
	}
	.common{
		color: rgba(0, 0, 0);
	}
  }
</style>
