<template>
  <div class="app-container">
    <button-bar view-title="待出库车辆列表" ref="btnbar" :btndisabled="btndisabled" @btnhandle="btnHandle"/>
    <div class="main-content">
      <div class="searchcon">
        <el-button size="small" class="searchbtn" @click="clicksearchShow">{{ searchxianshitit }}</el-button>
        <div v-show="isSearchShow" class="search">
          <el-form ref="listQueryform" :inline="true" :model="listQuery" label-width="100px" class="tab-header">
            <el-form-item label="合同编号">
              <el-input v-model="listQuery.params.contractNo" placeholder="" clearable class="filter-item"/>
            </el-form-item>
            <el-form-item label="客户名称">
              <el-input v-model="listQuery.params.name" placeholder="" clearable class="filter-item"/>
            </el-form-item>
            <el-form-item label="车架号">
              <el-input v-model="listQuery.params.vinNo" placeholder="" clearable class="filter-item"/>
            </el-form-item>
          </el-form>
          <div class="btn" style="text-align: center;">
            <el-button type="primary" icon="el-icon-search" size="small" @click="handleFilter">查询</el-button>
            <el-button type="primary" icon="el-icon-refresh" size="small" @click="handleReset">重置</el-button>
          </div>
        </div>
      </div>
      <div class="listtop">
        <div class="tit">待出库车辆列表</div>
        <pageye v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
      </div>
      <div class="">
        <el-table ref="multipleTable" :key="tableKey" v-loading="listLoading" :data="list" :border="true" style="width: 100%;" @selection-change="handleSelectionChange">
          <el-table-column fixed type="selection" align="center" width="50"/>
          <el-table-column fixed label="序号" type="index" width="60" :index="indexMethod" align="center"/>
          <el-table-column label="业务状态" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.subscription }}</span>
            </template>
          </el-table-column>
          <el-table-column label="销售类型" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.paymentMethod }}</span>
            </template>
          </el-table-column>
          <el-table-column label="合同编号" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.contractNo }}</span>
            </template>
          </el-table-column>
          <el-table-column label="贷款人" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.loanName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="客户名称" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="车架号" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.vinNo }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pages">
        <div class="tit"/>
        <!-- 翻页 -->
        <pagination v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
      </div>
    </div>
  </div>
</template>

<script>
import { listPage } from '@/api/chukuguanli/chukubanli'
import Pagination from '@/components/pagination'
import pageye from '@/components/pagination/pageye'
import ButtonBar from '@/components/ButtonBar'

export default {
  name: 'daichukucheliang',
  components: {
    Pagination,
    pageye,
    ButtonBar
  },
  data() {
    return {
      btndisabled: false,
      isSearchShow: false, // 主页面：隐藏查询条件按钮
      searchxianshitit: '显示查询条件',
      // 查询 -----------
      tableKey: 0,
      sids: [], // 用于导出的时候保存已选择的SIDs
      FormLoading: false,
      listLoading: false,
      typeList: [], // 选择器
      list: [],
      btnList: [
        {
          type: 'primary',
          size: 'small',
          icon: '',
          btnKey: 'doCreate',
          btnLabel: '确定'
        },
        {
          type: 'info',
          size: 'small',
          icon: '',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      // 翻页
      listQuery: {
        current: 1,
        size: 10,
        total: 0,
        params: {
          contractNo: '', // 合同编号
          loanSid: '',
          name: '', // 客户名称
          vinNo: '', // 车架号
          userSid: '',
          vinSid: []
        }
      },
      multipleSelection: []
    }
  },
  mounted() {
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  methods: {
    btnHandle(btnKey) {
      console.log('XXXXXXXXXXXXXXX    ' + btnKey)
      switch (btnKey) {
        case 'doCreate':
          this.doCreate()
          break
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    showData(value, contractNo, loanSid) {
      const aa = []
      if (value.length > 0) {
        for (var i = 0; i < value.length; i++) {
          aa.push(value[i].vinSid)
        }
        this.listQuery.params.contractNo = contractNo
        this.listQuery.params.loanSid = loanSid
        this.listQuery.params.vinSid = aa
      } else {
        this.listQuery.params.vinSid = []
      }
      this.getList()
    },
    // ========== 页面按钮功能 ==========
    // 搜索条件效果
    clicksearchShow() {
      this.isSearchShow = !this.isSearchShow
      if (this.isSearchShow) {
        this.searchxianshitit = '隐藏查询条件'
      } else {
        this.searchxianshitit = '显示查询条件'
      }
    },
    // 表中序号
    indexMethod(index) {
      var pagestart = (this.listQuery.current - 1) * this.listQuery.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    // 信息条数 获取点击时当前的sid
    handleSelectionChange(row) {
      console.log('待出库', row)
      this.multipleSelection = row
      const aa = []
      row.forEach(element => {
        aa.push({
          contractNo: element.contractNo,
          vinSid: element.vinSid
        })
      })
      this.sids = aa
    },
    // 查询按钮
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    // 点击重置
    handleReset() {
      this.listQuery.current = 1
      this.listQuery.size = 10
      this.listQuery.total = 0
      this.listQuery.params.name = ''
      this.listQuery.params.vinNo = ''
      this.listQuery.params.userSid = ''
      this.getList()
    },
    // 查询列表信息
    getList() {
      this.listLoading = true
      this.listQuery.params.userSid = window.sessionStorage.getItem('userSid')
      listPage(this.listQuery).then(response => { // 拦截器
        this.listLoading = false
        if (response.success) {
          this.list = response.data.records
          this.listQuery.total = response.data.total
        } else {
          this.list = []
          this.listQuery.total = 0
        }
      })
    },
    // 打开新增
    doCreate() {
      if (this.sids.length === 0) {
        this.$message({ showClose: true, type: 'error', message: '请选择一条记录进行出库申请' })
        return
      }
      if (this.sids.length > 0) {
        this.FormLoading = true
        console.log(this.multipleSelection)
        for (var i = 0; i < this.multipleSelection.length; i++) {
          if (this.multipleSelection[i].contractNo !== this.multipleSelection[this.multipleSelection.length - 1].contractNo || this.multipleSelection[i].loanSid !== this.multipleSelection[this.multipleSelection.length - 1].loanSid) {
            this.$message({
              showClose: true,
              message: '请选择同一合同编号且同一贷款人的车辆！',
              type: 'error'
            })
            return
          }
        }
        console.log(this.sids, 9999)
        this.$emit('backData', this.sids)
      }
    },
    doClose() {
      this.$emit('doback')
    }
  }
}
</script>
<style scoped>
</style>
