<template>
  <!-- 调账记录 -->
  <a-drawer
    :title="'调账记录'+titVal"
    placement="right"
    closable
    :visible="isShow"
    class="lookUpCustomers-modal"
    @close="isShow=false"
    width="75%">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="dealerAccountNewList-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="申请日期" prop="time">
                <rangeDate
                  ref="rangeDate"
                  :showTime="false"
                  :today="false"
                  :value="queryParam.time"
                  id="tireSalesDealerList-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.dealerLevel"
                  ref="dealerLevel"
                  id="tireSalesDealerList-dealerLevel"
                  code="DEALER_LEVEL"
                  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.dealerLevel"
                  ref="dealerLevel"
                  id="tireSalesDealerList-dealerLevel"
                  code="DEALER_LEVEL"
                  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: tableHeight+70+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :pageSize="30"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 轮胎补贴金额 -->
        <template slot="moneyNum" slot-scope="text, record">
          <span v-if="$hasPermissions('B_salesDetail')" class="link-bule" @click="handleDetail(record)">111111111111</span>
          <span v-else>111111111111</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 审核进度 -->
    <verifyModal v-drag :openModal="openVerifyModal" :itemId="itemId" :itemSn="itemSn" @close="openVerifyModal=false"></verifyModal>
  </a-drawer>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import chooseDealer from '@/views/dealerManagement/provincialWarehouseManagement/chooseDealer.vue'
import { saveBatchDealerList } from '@/api/provinceServiceCenter'
import rangeDate from '@/views/common/rangeDate.vue'
import verifyModal from './verifyModal.vue'
// 接口
import { dealerUserList } from '@/api/dealer'

export default {
  name: 'AddDealerModal',
  mixins: [commonMixin],
  components: { STable, VSelect, chooseDealer, rangeDate, verifyModal },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      titVal: '（经销商名称）',
      queryParam: { time: [], dealerLevel: '' },
      openVerifyModal: true, // 审核进度弹窗
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '申请时间', dataIndex: 'dealerTypeName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '审核时间', dataIndex: 'dealerTypeName1', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '记账日期', dataIndex: 'dealerTypeName2', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入账类型', dataIndex: 'dealerTypeName3', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '金额（￥）', dataIndex: 'dealerLevelDictValue', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核状态', dataIndex: 'dealerTypeName4', width: '16%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'moneyNum' }, width: '10%', align: 'center' },
        { title: '备注', dataIndex: 'createDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return dealerUserList(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: {
    // 审核进度
    handleDetail () {
      this.openVerifyModal = true
    },
    //  日期选择  change
    dateChange (date) {
      if (date[0] && date[1]) {
        this.queryParam.time = date
      } else {
        this.queryParam.time = []
      }
      this.queryParam.bizBeginDate = date[0] ? date[0].replace(/-/g, '') : ''
      this.queryParam.bizEndDate = date[1] ? date[1].replace(/-/g, '') : ''
    },
    handleAddDealer (list) {
      const newList = list.map(item => {
        return {
          dealerSn: item.dealerSn,
          dealerName: item.dealerName,
          dealerLevel: item.dealerLevel
        }
      })
      this.spinning = true
      saveBatchDealerList(newList).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
        }
        this.spinning = false
        this.$emit('ok')
        this.isShow = false
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>
