<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <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="创建时间">
                <rangeDate id="manualEntryForm-rangeDate" ref="rangeDate" :value="creatDate" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="调账单号">
                <a-input id="manualEntryForm-dealerName" v-model.trim="queryParam.changeNo" allowClear placeholder="请输入调账单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="客户名称">
                <dealerSubareaScopeList id="accountStatement-dealerName" ref="dealer" @change="custChange"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="借贷类型">
                <v-select
                  v-model="queryParam.changeType"
                  ref="changeType"
                  id="conFrontModal-changeType"
                  code="VERIFY_ACCOUNT_CHANGE_TYPE"
                  placeholder="请选择借贷类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="状态">
                <v-select
                  v-model="queryParam.status"
                  ref="status"
                  id="conFrontModal-status"
                  code="VERIFY_ACCOUNT_CHANGE_STATUS"
                  placeholder="请选择状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="manualEntryForm-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="manualEntryForm-reset">重置</a-button>
              <a-button
                style="margin-left: 10px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_promotionFeeExport')"
                id="manualEntryForm-export">导出</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px">
              </a></a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false" class="manualEntryForm-wrap">
      <!-- 操作按钮 -->
      <div class="table-operator">
        <div class="flex-center">
          <a-button type="primary" @click="handleAdd" v-if="$hasPermissions('B_vac_creat')">调账</a-button>
          <div class="tongji-bar"></div>
        </div>
        <div></div>
      </div>
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+80+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :pageSize="30"
          :defaultLoadData="false"
          bordered>
          <template slot="actions" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="record.status=='WAIT_SUBMIT'&&$hasPermissions('B_vac_edit')"
              class="button-info"
              @click="handleEdit(record)"
            >
              编辑
            </a-button>
            <a-button
              v-if="record.status=='WAIT_SUBMIT'&&$hasPermissions('B_vac_del')"
              size="small"
              type="link"
              class="button-error"
              @click="handleDel(record)"
            >
              删除
            </a-button>
            <a-button
              v-if="record.status!='WAIT_SUBMIT'&&$hasPermissions('B_vac_auditStatus')"
              size="small"
              type="link"
              @click="auditStatus(record)"
              class="button-info"
            >
              审核进度
            </a-button>
          </template>
        </s-table>
      </a-spin>
    </a-card>
    <!-- 审核进度 -->
    <verifyModal v-drag :openModal="openModal" :itemSn="itemSn" ref="verifyModal" @close="openModal=false"></verifyModal>
    <!-- 基础信息 -->
    <baseModal v-drag :show="baseModal" :itemSn="itemSn" @cancel="baseModal=false" @ok="hanldAudit"></baseModal>
    <!-- 提交  选择审批人员 -->
    <chooseDepartUserModal :openModal="openDepartUserModal" @close="openDepartUserModal=false" @submit="handleSave"></chooseDepartUserModal>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { hdExportExcel } from '@/libs/exportExcel'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import rangeDate from '@/views/common/rangeDate.vue'
import baseModal from './baseModal.vue'
import verifyModal from './verifyModal.vue'
import chooseDepartUserModal from './chooseDepartUserModal.vue'
import { verifyAccountChangeList, verifyAcountBillSubmit, verifyAccountChangeDel, verifyAccountChangeExport } from '@/api/verifyAccountChange.js'
export default {
  name: 'ManualEntryFormList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, baseModal, chooseDepartUserModal, dealerSubareaScopeList, verifyModal },
  data () {
    return {
      spinning: false,
      baseModal: false,
      openModal: false,
      exportLoading: false, // 导出按钮加载状态
      tableHeight: 0,
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        changeNo: '',
        dealerName: '',
        changeType: undefined,
        status: undefined
      },
      creatDate: [], //  创建时间
      disabled: false, //  查询、重置按钮是否可操作
      advanced: true,
      openDepartUserModal: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        return verifyAccountChangeList(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
              // 借方
              if (data.list[i].changeType == 'DEBIT') {
                data.list[i].borrowAmount = this.toThousands(data.list[i].changeAmount, 2)
              }
              // 贷方
              if (data.list[i].changeType == 'LENDER') {
                data.list[i].loanAmount = this.toThousands(data.list[i].changeAmount * -1, 2)
              }
            }
            this.total = data.count || 0
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      total: 0, // 合计
      itemSn: null
    }
  },
  // 根据权限显示列表字段
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调账单号', dataIndex: 'changeNo', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'dealerName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '借方', dataIndex: 'borrowAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '贷方', dataIndex: 'loanAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '备注', dataIndex: 'remarks', width: '30%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '审核时间', dataIndex: 'auditDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'statusDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'actions' }, width: '8%', align: 'center' }
      ]
      return arr
    }
  },
  methods: {
    //  创建时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0] ? date[0] : ''
      this.queryParam.endDate = date[1] ? date[1] : ''
    },
    // 导出
    handleExport () {
      const _this = this
      const params = _this.queryParam
      _this.exportLoading = true
      _this.spinning = true
      hdExportExcel(verifyAccountChangeExport, params, '人工录入单', function () {
        _this.exportLoading = false
        _this.spinning = false
      })
    },
    // 客户
    custChange (v) {
      if (v && v.key) {
        this.queryParam.dealerSn = v.key
        this.queryParam.dealerName = v.row ? v.row.dealerName : ''
      } else {
        this.queryParam.dealerSn = ''
        this.queryParam.dealerName = ''
      }
    },
    //  重置
    resetSearchForm () {
      this.creatDate = []
      this.$refs.rangeDate.resetDate(this.creatDate)
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.changeNo = ''
      this.queryParam.dealerName = ''
      this.queryParam.dealerSn = ''
      if (this.$refs.dealer) {
        this.$refs.dealer.resetForm()
      }
      this.queryParam.changeType = undefined
      this.queryParam.status = undefined
      this.$refs.table.refresh(true)
    },
    // 审核进度
    auditStatus (row) {
      this.openModal = true
      this.itemSn = row.changeSn
    },
    // 删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示（' + row.dealerName + '）',
        content: <div style="font-size:14px;"><div>调账单号：{row.changeNo}</div><div>确定删除吗？</div></div>,
        centered: true,
        closable: true,
        onOk () {
          _this.spinning = true
          verifyAccountChangeDel({ sn: row.changeSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
            _this.spinning = false
          })
        }
      })
    },
    // 调账
    handleAdd () {
      this.itemSn = null
      this.baseModal = true
    },
    // 编辑
    handleEdit (row) {
      this.itemSn = row.changeSn
      this.baseModal = true
    },
    // 审核提交弹框
    hanldAudit (data) {
      this.$refs.table.refresh()
      this.itemSn = data.changeSn
      this.openDepartUserModal = true
    },
    // 提交审核人员
    handleSave (data) {
      verifyAcountBillSubmit({ changeSn: this.itemSn, ...data }).then(res => {
        if (res.status == 200) {
          this.$message.success('提交成功')
          this.openDepartUserModal = false
          this.$refs.table.refresh()
        }
      })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 250
    }
  },
  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">
  .manualEntryForm-wrap{
    .active{
      color: #ed1c24;
      cursor: pointer;
    }
    .common{
      color: rgba(0, 0, 0);
    }
  }
</style>
