<template>
  <div class="customer">
    <template v-if="!detailVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <el-form-item label="名称">
          <el-input v-model="dataForm.customerName" placeholder="客户名称" clearable/>
        </el-form-item>
        <el-form-item label="客户级别">
          <el-select
            v-model="dataForm.level"
            remote
            placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="search()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center">
        </el-table-column>
        <el-table-column
          prop="customerName"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="客户名称">
        </el-table-column>
        <el-table-column
          prop="level"
          :formatter="levelFormat"
          header-align="center"
          align="center"
          label="级别">
        </el-table-column>
        <el-table-column
          prop="bizManagerName"
          header-align="center"
          align="center"
          min-width="120"
          label="对接业务员">
        </el-table-column>
        <el-table-column
          prop="receivedAmount"
          header-align="center"
          align="center"
          min-width="120"
          label="已收金额">
        </el-table-column>
        <el-table-column
          prop="notReceivedAmount"
          header-align="center"
          align="center"
          width="200"
          :show-tooltip-when-overflow="true"
          label="待收金额">
        </el-table-column>
        <el-table-column
          prop="invoicedAmount"
          header-align="center"
          align="center"
          label="已开票金额">
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          min-width="180"
          :show-tooltip-when-overflow="true"
          label="备注">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="true" type="text" size="small" @click="detailHandle(scope.row.customerId)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </template>
    <!-- 弹窗, 新增 / 修改 -->
    <customer-detail v-if="detailVisible" ref="detail" @onChose="onChose"/>
  </div>
</template>

<script>
  import customerDetail from './customer-detail'
  import { getLevel } from '@/api/cus'
  import { getCustomerList } from '@/api/finance'
  export default {
    name: 'customer',
    components: {
      customerDetail
    },
    data () {
      return {
        detailVisible: false,
        dataForm: {
          customerName: '',
          level: ''
        },
        options: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: []
      }
    },
    created () {
      this.getLevel()
      this.getDataList()
    },
    methods: {
      onChose () {
        this.addOrUpdateVisible = false
        this.detailVisible = false
      },
      // 查询
      search () {
        this.pageIndex = 1
        this.getDataList()
      },
      getLevel () {
        getLevel().then(({data}) => {
          if (data && data.code === '200') {
            this.options = data.data
          }
        })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'customerName': this.dataForm.customerName,
          'level': this.dataForm.level
        }
        getCustomerList(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 转换属性“级别”
      levelFormat (row) {
        for (let i = 0; i < this.options.length; i++) {
          if (this.options[i].code === row.level) {
            return this.options[i].value
          }
        }
      },
      detailHandle (id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detail.init(id)
        })
      }
    }
  }
</script>

<style scoped>

</style>
