<template>
  <div class="app-container">
    <!--列表页面-->
    <div v-show="viewState == 1">
      <button-bar view-title="逾期车辆列表" ref="btnbar" :btndisabled="btndisabled" @btnhandle="btnHandle"/>
      <!--Start查询列表部分-->
      <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="110px" class="tab-header">
              <el-form-item label="分公司">
                <el-input v-model="listQuery.params.useOrgName" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="销售部门">
                <el-input v-model="listQuery.params.dept" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="贷款合同编号">
                <el-input v-model="listQuery.params.loanContractNo" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="车架号">
                <el-input v-model="listQuery.params.vinNo" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="车牌号">
                <el-input v-model="listQuery.params.carNum" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="贷款人">
                <el-input v-model="listQuery.params.borrowerName" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="还款操作人">
                <el-input v-model="listQuery.params.repaymentName" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="逾期期数">
                <el-input v-model="listQuery.params.beOverduePeriod" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="逾期金额">
                <el-input v-model="listQuery.params.beOverdueMoney" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="转换期数">
                <el-input v-model="listQuery.params.beOverdueMoneyAndPeriod" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="最近催收日期">
                <el-date-picker v-model="listQuery.params.collectionStartDate" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
                <span style="padding: 0 8px">至</span>
                <el-date-picker v-model="listQuery.params.collectionEndDate" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
              </el-form-item>
              <el-form-item label="风控专员">
                <el-input v-model="listQuery.params.riskStaffName" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="远程控制状态">
                <el-select v-model="listQuery.params.lockCarState" clearable placeholder="请选择" filterable>
                  <el-option v-for="item in lockCarState_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictValue">
                  </el-option>
                </el-select>
              </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>
        <!--End查询列表部分-->
        <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>
        <!--Start 主页面主要部分 -->
        <div class="">
          <el-table :key="tableKey" v-loading="listLoading" :data="list" :border="true" style="width: 100%;" @selection-change="handleSelectionChange">
            <el-table-column type="selection" align="center" width="50"/>
            <el-table-column label="序号" type="index" width="80" :index="indexMethod" align="center"/>
            <el-table-column prop="useOrgName" label="分公司" align="center" width="130"/>
            <el-table-column prop="dept" label="销售部门" align="center" width="130"/>
            <el-table-column prop="loanContractNo" label="贷款合同编号" align="center" width="165"/>
            <el-table-column prop="vinNo" label="车架号" align="center" width="100"/>
            <el-table-column prop="carNum" label="车牌号" align="center" width="100"/>
            <el-table-column prop="borrowerName" label="贷款人" align="center" min-width="100"/>
            <el-table-column prop="repaymentName" label="还款操作人" align="center" width="150"/>
            <el-table-column prop="beOverduePeriod" label="逾期期数" align="center" width="100"/>
            <el-table-column prop="beOverdueMoney" label="逾期金额" align="center" width="100"/>
            <el-table-column prop="beOverdueMoneyAndPeriod" label="换算期数" align="center" width="100"/>
            <el-table-column label="最近催收日期" align="center" width="120">
              <template slot-scope="scope">
                <span class="bluezi" @click="lookCollection(scope.row)">{{ scope.row.createTime }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="bankCardRemark" label="处置记录" align="center" width="90">
              <template>
                <span class="bluezi">查看</span>
              </template>
            </el-table-column>
            <el-table-column prop="riskStaffUserName" label="风控专员" align="center" width="100"/>
            <el-table-column prop="lockCarState" label="远程控制状态" align="center" width="120"/>
          </el-table>
        </div>
        <!--End 主页面主要部分-->
        <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>
        <!--End查询列表部分-->
      </div>
    </div>
    <el-dialog title="分配风控专员" :visible.sync="dialogVisible" width="30%" :show-close="false">
      <el-form class="formadd" v-model="formobj">
        <el-row style="border-top: 1px solid #E0E3EB">
          <el-col :span="24">
            <el-select v-model="formobj.userSid" placeholder="请选择" clearable filterable>
              <el-option v-for="item in user_list" :key="item.sid" :label="item.name" :value="item.sid"></el-option>
            </el-select>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleConfirm()">确定</el-button>
        <el-button size="small" @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    <!-- 查看车辆历史催收记录 -->
    <vehiclecollectionrecords v-show="viewState == 2" ref="divRecords" @doback="resetState"/>
  </div>
</template>

<script>
import Pagination from '@/components/pagination'
import pageye from '@/components/pagination/pageye'
import ButtonBar from '@/components/ButtonBar'
import req from '@/api/overduevehicle/overduevehicle'
import { getButtonPermissions, selAllByOrgSidPath } from '@/api/Common/dictcommons'
import vehiclecollectionrecords from './vehiclecollectionrecords'

export default {
  name: 'OverdueVehicle',
  components: {
    Pagination,
    pageye,
    ButtonBar,
    vehiclecollectionrecords
  },
  data() {
    return {
      btndisabled: false,
      dialogVisible: false,
      btnList: [
        {
          type: 'primary',
          size: 'small',
          icon: '',
          btnKey: 'toUndistributed',
          btnLabel: '未分配'
        },
        {
          type: 'primary',
          size: 'small',
          icon: '',
          btnKey: 'toAllot',
          btnLabel: '分配风控人员'
        },
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      isSearchShow: false,
      searchxianshitit: '显示查询条件',
      viewState: 1, // 1、列表 2、新增 3、编辑 4、查看
      tableKey: 0,
      list: [],
      sids: [], // 用于导出的时候保存已选择的SIDs
      lockCarState_list: [
        {
          dictKey: '0',
          dictValue: '未控制'
        },
        {
          dictKey: '1',
          dictValue: '控制成功'
        },
        {
          dictKey: '2',
          dictValue: '控制失败'
        },
        {
          dictKey: '3',
          dictValue: '已解控'
        }
      ],
      user_list: [],
      FormLoading: false,
      listLoading: false,
      // 翻页
      listQuery: {
        current: 1,
        size: 10,
        total: 0,
        params: {
          repaymentName: '',
          beOverdueMoney: '',
          beOverdueMoneyAndPeriod: '',
          beOverduePeriod: '',
          borrowerName: '',
          carNum: '',
          dept: '',
          loanContractNo: '',
          menuSid: '',
          menuUrl: '',
          orgPath: '',
          riskStaffName: '',
          useOrgName: '',
          userSid: '',
          vinNo: '',
          collectionStartDate: '',
          collectionEndDate: '',
          isFP: false,
          lockCarState: ''
        }
      },
      // 分配风控专员参数
      formobj: {
        saleVehSids: [],
        userSid: ''
      }
    }
  },
  created() {
    // 初始化变量
    this.getList()
  },
  mounted() {
    getButtonPermissions({ userSid: window.sessionStorage.getItem('userSid'), url: this.$route.path, type: 0 }).then((res) => {
      if (res.success) {
        for (var i = 0; i < res.data.length; i++) {
          for (var k = 0; k < this.btnList.length; k++) {
            if (res.data[i].buttonId === this.btnList[k].btnKey) {
              this.btnList.splice(k, 1)
            }
          }
        }
        this.$refs['btnbar'].setButtonList(this.btnList)
      }
    })
  },
  methods: {
    // 搜索条件效果
    clicksearchShow() {
      this.isSearchShow = !this.isSearchShow
      if (this.isSearchShow) {
        this.searchxianshitit = '隐藏查询条件'
      } else {
        this.searchxianshitit = '显示查询条件'
      }
    },
    btnHandle(btnKey) {
      console.log('XXXXXXXXXXXXXXX    ' + btnKey)
      switch (btnKey) {
         case 'toUndistributed':
           this.toUndistributed()
           break
        case 'toAllot':
          this.toAllot()
          break
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    // 信息条数 获取点击时当前的sid
    handleSelectionChange(row) {
      const aa = []
      const bb = []
      row.forEach(element => {
        aa.push(element.sid)
        bb.push(element.saleVehSid)
      })
      this.sids = aa
      this.formobj.saleVehSids = bb
    },
    // 表中序号
    indexMethod(index) {
      var pagestart = (this.listQuery.current - 1) * this.listQuery.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    // 查询列表信息
    getList() {
      this.listLoading = true
      this.listQuery.params.userSid = window.sessionStorage.getItem('userSid')
      this.listQuery.params.orgPath = window.sessionStorage.getItem('defaultOrgPath')
      this.listQuery.params.menuUrl = this.$route.path
      req.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
        }
      })
    },
    // 查询按钮
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    // 点击重置
    handleReset() {
      this.listQuery = {
        current: 1,
        size: 10,
        total: 0,
        params: {
          repaymentName: '',
          beOverdueMoney: '',
          beOverdueMoneyAndPeriod: '',
          beOverduePeriod: '',
          borrowerName: '',
          carNum: '',
          dept: '',
          loanContractNo: '',
          menuSid: '',
          menuUrl: '',
          orgPath: '',
          riskStaffName: '',
          useOrgName: '',
          userSid: '',
          vinNo: '',
          collectionStartDate: '',
          collectionEndDate: '',
          isFP: false,
          lockCarState: ''
        }
      }
      this.getList()
    },
    // 查看车辆历史催收记录
    lookCollection(row) {
      this.viewState = 2
      this.$refs['divRecords'].showInfo(row)
    },
    // 查询所有未分配记录
    toUndistributed() {
      this.listQuery.params.isFP = true
      this.getList()
    },
    // 分配风控人员
    toAllot() {
      if (this.formobj.saleVehSids.length > 0) {
        this.dialogVisible = true
        selAllByOrgSidPath({ orgSidPath: window.sessionStorage.getItem('defaultOrgPath') }).then((resp) => {
          if (resp.success) {
            this.user_list = resp.data
          }
        })
      } else {
        this.$message({ showClose: true, type: 'error', message: '请至少选择一条记录进行操作' })
      }
    },
    // 确定分配风控人员
    handleConfirm() {
      if (this.formobj.userSid !== '') {
        req.save(this.formobj).then((res) => {
          if (res.success) {
            this.$message({ showClose: true, type: 'success', message: '分配成功' })
            this.formobj = {
              saleVehSids: [],
              userSid: ''
            }
            this.dialogVisible = false
            this.getList()
          }
        })
      } else {
        this.$message({ showClose: true, type: 'error', message: '请选择风控人员' })
      }
    },
    resetState() {
      this.viewState = 1
    },
    doClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>
</style>
