<template>
  <!-- 调账记录 -->
  <a-drawer
    :title="'调账记录（'+$route.params.name+'）'"
    placement="right"
    closable
    :visible="isShow"
    class="checkRecord-modal"
    @close="isShow=false"
    width="78%">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="checkRecordModal-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"
                  :value="time"
                  id="checkRecordModal-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.amountType"
                  ref="amountType"
                  id="checkRecordModal-amountType"
                  code="DEALER_AMOUNT_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.status"
                  id="checkRecordModal-status"
                  code="DEALER_ADJU_ADUIT"
                  placeholder="请选择审核状态"
                  allowClear></v-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="dealerAccountNewList-refresh">查询</a-button>
              <a-button style="margin:0 5px" @click="resetSearchForm" :disabled="disabled" id="dealerAccountNewList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height:770+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :pageSize="30"
        :scroll="{ y: 730 }"
        :defaultLoadData="false"
        bordered>
        <!-- 记账日期   审核通过时显示审核时间 -->
        <template slot="postingDate" slot-scope="text, record">
          {{ record.status==='AUDIT_PASS'?record.auditDate:'--' }}
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            v-if="$hasPermissions('B_dealerAccount_edit')"
            class="button-info"
            @click="handleCheck(record)"
            :id="'checkRecordModal-audit-btn'+record.id">审核进度</a-button>
        </template>
      </s-table>
    </a-spin>
    <!-- 审核进度 -->
    <verifyModal v-drag :openModal="openVerifyModal" :itemSn="itemSn" @close="openVerifyModal=false"></verifyModal>
  </a-drawer>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import verifyModal from './verifyModal.vue'
// 接口
import { dealerAccountRecordList } from '@/api/dealerAccount'

export default {
  name: 'CheckRecordModal',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, verifyModal },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    const _this = this
    return {
      spinning: false,
      disabled: false, // 查询、重置按钮是否可操作
      isShow: this.openModal, //  是否打开弹框
      time: [], // 申请时间
      queryParam: {
        beginDate: '', // 开始时间
        endDate: '', // 结束时间
        amountType: undefined, // 入账类型
        status: undefined// 审核状态
      },
      openVerifyModal: false, // 审核进度弹窗
      itemSn: null, // 当前行sn
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '申请时间', dataIndex: 'createDate', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核时间', dataIndex: 'auditDate', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '记账日期', width: '13%', align: 'center', scopedSlots: { customRender: 'postingDate' } },
        { title: '入账类型', dataIndex: 'amountTypeDictValue', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '金额（￥）', dataIndex: 'changeAmount', width: '13%', align: 'right', customRender: text => { return (text || text == 0) ? _this.toThousands(text) : '--' } },
        { title: '审核状态', dataIndex: 'statusDictValue', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '13%', align: 'center' },
        { title: '备注', dataIndex: 'remark', width: '15%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        this.queryParam.dealerSn = this.$route.params.sn
        return dealerAccountRecordList(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: {
    // 审核进度
    handleCheck (row) {
      this.itemSn = row.accountChangeSn
      this.openVerifyModal = true
    },
    //  日期选择  change
    dateChange (date) {
      if (date[0] && date[1]) {
        this.time = date
      } else {
        this.time = []
      }
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    // 重置
    resetSearchForm () {
      this.time = []
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.amountType = undefined
      this.queryParam.status = undefined
      this.$refs.rangeDate.resetDate()
      this.$refs.table.refresh(true)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetSearchForm()
      } else {
        this.$nextTick(() => {
          this.$refs.table.refresh(true)
        })
      }
    }
  }
}
</script>
