
<template>
  <base-list>
    <template slot="n_header">
      <el-form v-model="pagerDto" inline>
        <el-form-item label="客户名称">
          <el-select v-model="pagerDto._Eq_CustomerId" placeholder="客户名称" clearable filterable>
            <el-option
              v-for="item in customers"
              :key="item.Id"
              :label="item.CompanyName"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="pagerDto._Eq_CreateUserName" placeholder="申请人" clearable filterable>
            <el-option
              v-for="item in salesmanUser"
              :key="item.Id"
              :label="item.NickName"
              :value="item.NickName"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="pagerDto._Eq_ApplyStatus" placeholder="审核状态" clearable filterable>
            <el-option
              v-for="item in applyStatus"
              :key="item.Id"
              :label="item.Name"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="pagerSerach(1)">
            搜索
          </el-button>
        </el-form-item>
      </el-form>
    </template>
    <template slot="n_option">
      <el-button type="success" size="small" @click="ApplyPass()">
        通过
      </el-button>
      <!-- <el-button type="danger" size="small" @click="ApplyReject()">
          驳回
        </el-button> -->
    </template>
    <template>
      <el-table
        v-loading="dataLoading"
        :data="data"
        :highlight-current-row="true"
        :select-on-indeterminate="true"
        width="100%"
        border
        @selection-change="onSelection"
      >
        <el-table-column key="Id" prop="Id" type="selection" :selectable="rowCanSelect" />
        <el-table-column label="客户名" prop="CustomerAccountName" />
        <el-table-column label="申请人" prop="CreateUserName" />
        <el-table-column label="授信额度(CNY)" prop="ApplyValue" />
        <el-table-column label="生效时间" prop="StartTime" />
        <el-table-column label="结束时间" prop="EndTime" />
        <el-table-column label="当前状态">
          <template slot-scope="r">
            <span v-if="r.row.ApplyStatus==1">待审</span>
            <span v-if="r.row.ApplyStatus==2">通过</span>
            <span v-if="r.row.ApplyStatus==3">驳回</span>
          </template>
        </el-table-column>
        <el-table-column label="申请说明" prop="ApplyDec" />
        <el-table-column label="回复" prop="ReplyDec" />
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 40,100, 400]"
        :current-page="pagerDto.PageIndex"
        :page-size.sync="pagerDto.PageSize"
        background
        layout="prev, pager, next ,total ,jumper,sizes,slot "
        :total="pagerDto.total"
        @size-change="changePager"
        @current-change="pagerSerach"
      />
    </template>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="this.titleDia"
    >
      <el-input v-model="ReplyDec" type="textarea" :rows="2" placeholder="选填，备注信息" />
      <br>
      <el-button type="primary" size="small" @click="ApplySubmit()">
        继续
      </el-button>
    </el-dialog>
  </base-list>
</template>
<script>
import BaseList from '@/components/Xyy/baselist'
import { _getCustomers } from '@/api/baseInfo'
import { _getSalesmanUsers } from '@/api/customer'
import { Get } from '@/api/enum/enums'
import { GetAccountQuotaPager, ApplySubmit } from '@/api/customerAccount/api'
export default {
  name: 'QuotaApply',
  components: { 'base-list': BaseList },
  data() {
    return {
      customers: [],
      salesmanUser: [],
      applyStatus: [],
      selectIds: '',
      titleDia: '',
      ReplyDec: '',
      submitStatus: 0,
      dialogVisible: false,
      data: [], dataLoading: false,
      pagerDto: { PageIndex: 0, PageSize: 10, total: 0,
        QuerySort: [{ Field: 'CreateTime', IsDesc: false }],
        '_Eq_ApplyStatus': 1
      }
    }
  },
  mounted() {
    _getCustomers().then(response => {
      this.customers = response
    })
    _getSalesmanUsers().then(response => {
      this.salesmanUser = response
    })
    Get('ObjectApplyEnum').then(response => {
      this.applyStatus = response
    })
    this.pagerSerach(1)
  },
  methods: {
    ApplySubmit() {
      const Ids = this.selectIds
      const ReplyDec = this.ReplyDec
      const applyStatu = this.submitStatus
      const params = { Ids, applyStatu, ReplyDec }
      ApplySubmit(params).then(res => {
        if (res.success) {
          this.$message({ type: 'success', message: res.message })
          this.pagerSerach(1)
          this.dialogVisible = false
        } else {
          this.$message({ type: 'success', message: res.message })
        }
      })
    },
    ApplyPass() {
      if (this.selectIds == '') { this.$message({ type: 'warning', message: '请选择待审数据后提交！' }); return false }
      this.titleDia = '点击继续将通过当前勾选申请！'
      this.dialogVisible = true
      this.submitStatus = 2
    },
    ApplyReject() {
      if (this.selectIds == '') { this.$message({ type: 'warning', message: '请选择待审数据后提交！' }); return false }
      this.titleDia = '点击继续将驳回当前勾选申请！'
      this.dialogVisible = true
      this.submitStatus = 3
    },
    rowCanSelect(row) {
      return row.ApplyStatus == 1
    },
    onSelection(s) {
      var ids = []
      for (let i = 0; i < s.length; i++) {
        ids.push(s[i].Id)
      }
      this.selectIds = ids.join(',')
    },
    pagerSerach(index) {
      this.dataLoading = true
      if (index) {
        this.pagerDto.PageIndex = index
      }
      GetAccountQuotaPager(this.pagerDto).then(res => {
        this.data = res.Result
        this.pagerDto.total = res.TotalCount
      }).finally(res => { this.dataLoading = false })
    },
    changePager(v) {
      this.PageSize = v
      this.pagerSerach(1)
    }
  }
}
</script>
