<template>
  <div class="fundAcountDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="fundAcountDetail-back" v-if="pageType=='pages'">
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="fundAcountDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <span style="color: #333;margin-left: 30px;font-weight: bold;" v-if="detailData">{{ detailData.settleStyleName }}({{ detailData.accountName }} {{ detailData.accountNo }})</span>
        </template>
      </a-page-header>
      <a-card size="small" :bordered="false">
        <!-- 搜索条件 -->
        <div ref="tableSearch" class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-item label="单据类型">
                  <v-select
                    v-model="queryParam.bizType"
                    ref="bizType"
                    :isEnable="true"
                    id="fundAcountDetail-bizType"
                    code="SETTLE_FLOW_BIZ_TYPE"
                    placeholder="请选择单据类型"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="关联单据号">
                  <a-input id="fundAcountDetail-bizNo" v-model.trim="queryParam.bizNo" allowClear placeholder="请输入关联单据号"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="操作时间">
                  <rangeDate id="fundAcountDetail-settleTime" ref="rangeDate" @change="dateChange" />
                </a-form-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="6" :sm="24">
                  <a-form-item label="往来单位">
                    <a-input id="fundAcountDetail-settleClientName" v-model.trim="queryParam.settleClientName" allowClear placeholder="请输入往来单位名称"/>
                  </a-form-item>
                </a-col>
              </template>
              <a-col :md="6" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="fundAcountDetail-refresh">查询</a-button>
                  <a-button style="margin-left: 8px" @click="resetSearchForm()" :disabled="disabled" id="fundAcountDetail-reset">重置</a-button>
                  <a @click="advanced=!advanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'"/>
                  </a>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </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="settleAmount" slot-scope="text, record">
            <span :style="{color:text<0?'red':''}">{{ ((text || text == 0) ? (text) : '--') }}</span>
          </template>
        </s-table>
      </a-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { findSettleFlowPage, settleAccountDetail } from '@/api/settleAcount'
export default {
  name: 'FundAccountDetail',
  components: { STable, VSelect, rangeDate },
  mixins: [commonMixin],
  props: {
    pageType: { // 页面表现形式，pages 新页签打开的页面，modal 弹框里的页面
      type: String,
      default: 'pages'
    },
    paramsData: {
      type: Object,
      default: function () {
        return null
      }
    },
    saveing: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      advanced: true, //  高级搜索 展开/关闭
      detailData: null, // 详细基础信息
      // 查询参数
      queryParam: {
        beginDate: '',
        endDate: '',
        bizType: '',
        bizNo: '',
        settleClientName: ''
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '单据类型', dataIndex: 'bizTypeDictValue', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '关联单据号', dataIndex: 'bizNo', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作时间', dataIndex: 'settleTime', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作人', dataIndex: 'operateName', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '往来单位', dataIndex: 'settleClientName', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '结算金额', dataIndex: 'settleAmount', scopedSlots: { customRender: 'settleAmount' }, width: '10%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        this.queryParam.settleAccountSn = this.$route.params.sn
        return findSettleFlowPage(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
        })
      }
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      if (date.length) {
        this.queryParam.beginDate = date[0]
        this.queryParam.endDate = date[1]
      } else {
        this.queryParam.beginDate = ''
        this.queryParam.endDate = ''
      }
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam = {
        beginDate: '',
        endDate: '',
        bizType: '',
        bizNo: '',
        settleClientName: ''
      }
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.getDetail()
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 270
    },
    //  返回
    handleBack () {
      if (this.pageType == 'pages') {
        this.$router.push({ name: 'fundAccountList' })
      } else {
        this.$emit('cansel')
      }
    },
    getDetail () {
      settleAccountDetail({ settleAccountSn: this.$route.params.sn }).then(res => {
        if (res.status == '200') {
          this.detailData = res.data
        }
      })
    }
  },
  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">
  .fundAcountDetail-wrap{
    .fundAcountDetail-back{
      margin-bottom: 8px;
    }
    .sTable{
      margin-top: 10px;
    }
	.active{
		color: #ed1c24;
		cursor: pointer;
	}
	.common{
		color: rgba(0, 0, 0);
	}
  }
</style>
