<template>
  <div class="dealerAccountDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <div>
        <a-page-header :ghost="false" :backIcon="false" class="header-bar">
          <!-- 自定义的二级文字标题 -->
          <template slot="subTitle">
            <a id="dealerAccountDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
            <span v-if="totalData" class="subTitle-word text-overflow" style="margin:0 15px;vertical-align: middle;"><span>轮胎补贴金额（{{ totalData.dealerEntity?totalData.dealerEntity.dealerName?totalData.dealerEntity.dealerName:'--':'--' }}）：￥</span><span style="font-size:20px;" >{{ (totalData.subsidyTire||totalData.subsidyTire==0)?toThousands(totalData.subsidyTire):'--' }}</span></span>
            <span v-else>--</span>
            <a-button
              type="danger"
              v-if="$hasPermissions('B_checkAccount')"
              style="margin-right:15px;"
              ghost
              id="dealerAccountDetail-checkAccount"
              @click="openCheckAccountModal = true">调账</a-button>
            <a-button
              type="primary"
              v-if="$hasPermissions('B_checkRecord')"
              class="button-info"
              ghost
              id="dealerAccountDetail-checkRecord"
              @click="openCheckRecordModal=true">调账记录</a-button>
          </template>
        </a-page-header>
      </div>
      <a-card size="small" :bordered="false">
        <div ref="tableSearch" class="table-page-search-wrapper newTableSearchName" >
          <a-form-model
            layout="inline"
            :model="queryParam"
            id="dealerAccountDetail-form"
            @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="时间">
                  <rangeDate ref="rangeDate" id="dealerAccountDetail-time" :value="time" @change="dateChange" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="业务类型">
                  <v-select
                    v-model="queryParam.flowBizType"
                    ref="flowBizType"
                    id="dealerAccountDetail-flowBizType"
                    code="FLOW_BIZ_TYPE"
                    placeholder="请选择业务类型"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="入账类型">
                  <v-select
                    v-model="queryParam.amountType"
                    ref="amountType"
                    id="dealerAccountDetail-amountType"
                    code="DEALER_AMOUNT_TYPE"
                    placeholder="请选择入账类型"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="dealerAccountDetail-refresh">查询</a-button>
                <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="dealerAccountDetail-reset">重置</a-button>
                <a-button
                  style="margin-left: 10px"
                  type="primary"
                  class="button-warning"
                  @click="handleExport"
                  :disabled="disabled"
                  :loading="exportLoading"
                  v-if="$hasPermissions('B_dealerAccountDetail_export')"
                  id="dealerAccountDetail-export">导出</a-button>
              </a-col>
            </a-row>
          </a-form-model>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          :pageSize="30"
          :style="{ height: tableHeight+60+'px' }"
          :scroll="{ y: tableHeight-10 }"
          bordered>
        </s-table>
      </a-card>
    </a-spin>
    <!-- 调账 -->
    <check-account-modal
      v-drag
      ref="checkAccountModal"
      :dealerName="totalData&&totalData.dealerEntity&&totalData.dealerEntity.dealerName?totalData.dealerEntity.dealerName:''"
      :dealerNum="totalData&&totalData.subsidyTire?totalData.subsidyTire:''"
      :openModal="openCheckAccountModal"
      @ok="$refs.table.refresh(true)"
      @close="openCheckAccountModal = false" />
    <!-- 调账记录 -->
    <check-record-modal v-drag :openModal="openCheckRecordModal" @ok="$refs.table.refresh(true)" @close="openCheckRecordModal = false" />
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import checkAccountModal from './checkAccountModal.vue'
import checkRecordModal from './checkRecordModal.vue'
import { hdExportExcel } from '@/libs/exportExcel'
// 接口
import { dealerAccountFlowList, dealerAccountFlowExport, queryDealerAccount } from '@/api/dealerAccount'
export default {
  name: 'DealerAccountDetail',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, checkAccountModal, checkRecordModal },
  data () {
    const _this = this
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出时加载状态
      openCheckAccountModal: false, // 打开调账弹窗
      openCheckRecordModal: false, // 打开调账记录弹窗
      // 查询参数
      time: [],
      queryParam: {
        beginDate: '', // 开始时间
        endDate: '', // 结束时间
        flowBizType: undefined, // 业务类型
        amountType: undefined // 入账类型
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '记账日期', dataIndex: 'accountDate', width: '16%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务类型', dataIndex: 'flowBizTypeDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入账类型', dataIndex: 'amountTypeDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '金额（￥）', dataIndex: 'flowAmount', width: '16%', align: 'center', customRender: text => { return (text || text == 0) ? _this.toThousands(text) : '--' } },
        { title: '备注', dataIndex: 'remark', width: '16%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ],
      totalData: null,
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        this.queryParam.dealerSn = this.$route.params.sn
        const params = Object.assign(parameter, this.queryParam)
        // 获取列表数据  有分页
        return dealerAccountFlowList(params).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.getCount()
          }
          this.disabled = false
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      if (date[0] && date[1]) {
        this.time = date
      } else {
        this.time = []
      }
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    getCount () {
      queryDealerAccount({ accountSn: this.$route.params.aSn }).then(res => {
        if (res.status == 200 && res.data) {
          this.totalData = res.data
        } else {
          this.totalData = null
        }
      })
    },
    // 重置
    resetSearchForm () {
      this.queryParam = {
        beginDate: '', // 开始时间
        endDate: '', // 结束时间
        flowBizType: undefined, // 业务类型
        amountType: undefined // 入账类型
      }
      this.time = []
      this.$refs.rangeDate.resetDate()
      this.$refs.table.refresh(true)
    },
    // 导出
    handleExport () {
      const _this = this
      _this.exportLoading = true
      _this.spinning = true
      hdExportExcel(dealerAccountFlowExport, _this.queryParam, '轮胎补贴金额列表', function () {
        _this.exportLoading = false
        _this.spinning = false
      })
    },
    //  返回
    handleBack () {
      this.$router.push({ name: 'dealerAccountList' })
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      _this.$refs.table.refresh(true)
    },
    // 计算列表高度
    setTableH () {
      const searchHeight = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - searchHeight - 240
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
