<template>
  <div class="funAccountList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" 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" 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="expensesType" slot-scope="text, record">
            {{ record.expensesTypeName1 }} <span v-if="record.expensesTypeName2">>{{ record.expensesTypeName2 }}</span> <span v-if="record.expensesTypeName3">>{{ record.expensesTypeName3 }}</span>
          </template>
          <!-- 审核 -->
          <template slot="audit" slot-scope="text, record">
            <stateIcon :title="record.stateDictValue" :state="record.state == 'AUDIT_PASS'?'1':'2'"></stateIcon>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="record.state == 'WAIT_AUDIT' && $hasPermissions('B_expenseAudit')"
              class="button-primary"
              @click="handleDetail(record)"
              id="foundAcount-detail-btn">资金明细</a-button>
            <a-button
              size="small"
              type="link"
              v-if="record.state == 'WAIT_AUDIT' && $hasPermissions('B_expenseAudit')"
              class="button-primary"
              @click="handleAudit(record)"
              id="foundAcount-audit-btn">审核</a-button>
            <a-button
              size="small"
              type="link"
              v-if="record.state == 'WAIT_AUDIT' && $hasPermissions('B_expenseEdit')"
              class="button-primary"
              @click="handleEdit(record)"
              id="foundAcount-edit-btn">编辑</a-button>
            <a-button
              size="small"
              type="link"
              v-if="record.state == 'WAIT_AUDIT' && $hasPermissions('B_expenseDel')"
              class="button-error"
              @click="handleDel(record)"
              id="foundAcount-del-btn">删除</a-button>
            <span v-if="record.state != 'WAIT_AUDIT'||( !$hasPermissions('B_expenseAudit')&& !$hasPermissions('B_expenseEdit') && !$hasPermissions('B_expenseDel'))">--</span>
          </template>
        </s-table>
      </a-card>

    </a-spin>
    <!-- 详情 -->
    <addModal :openModal="openModal" :itemId="itemId" @close="closeAddModal" />
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { settleExpenseQuery, delExpense, auditExpense } from '@/api/settleExpense'
import addModal from './addModal.vue'
import stateIcon from '@/views/common/stateIcon'
export default {
  name: 'ExpenseManagementList',
  components: { STable, VSelect, addModal, rangeDate, stateIcon },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      advanced: true, //  高级搜索 展开/关闭
      enableFundAccount: '1',
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '账户类型', dataIndex: 'accountExpensesNo', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '账户名称', dataIndex: 'createDate', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '账户号码', scopedSlots: { customRender: 'expensesType' }, width: '20%', align: 'center' },
        { title: '收入金额', dataIndex: 'settleAmount', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? ('￥' + text) : '--') } },
        { title: '支出金额', dataIndex: 'settleAmount1', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? ('￥' + text) : '--') } },
        { title: '启用', scopedSlots: { customRender: 'enable' }, width: '3%', align: 'center' },
        { title: '审核', scopedSlots: { customRender: 'audit' }, width: '3%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return settleExpenseQuery(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: {
    enableFundAccountChange (v) {

    },
    // 关闭弹框
    closeAddModal () {
      this.openModal = false
      this.itemId = null
    },
    //  新增/编辑
    handleEdit (row) {
      this.itemId = row ? row.id : null
      this.openModal = true
    },
    //  资金明细
    handleDetail (row) {
      this.$router.push({ name: 'fundAccountDetail', params: { id: row.id } })
    },
    //  审核
    handleAudit (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定要审核吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          auditExpense({
            id: row.id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  删除
    handleDel (row) {
      const _this = this
      _this.itemId = row.id
      _this.$confirm({
        title: '提示',
        content: '删除后原数据不可恢复，是否删除？',
        centered: true,
        onOk () {
          _this.spinning = true
          delExpense({ id: _this.itemId }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    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>
