<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      @submit.native.prevent="handleQuery"
    >
      <el-form-item label="客户信息" prop="custIsn">
        <el-input
          v-model="queryParams.custIsn"
          placeholder="请输入客户内码/证件号/客户名"
          clearable
          @keydown.enter.native.prevent="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型" prop="custType">
        <el-select v-model="queryParams.custType" placeholder="请选择客户类型" clearable style="width: 160px">
          <el-option label="个人" value="个人" />
          <el-option label="企业" value="企业" />
        </el-select>
      </el-form-item>
      <el-form-item label="担保方式" prop="guarType">
        <el-select v-model="queryParams.guarType" placeholder="请选择担保方式" clearable style="width: 160px">
          <el-option label="信用" value="信用" />
          <el-option label="保证" value="保证" />
          <el-option label="抵押" value="抵押" />
          <el-option label="质押" value="质押" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keydown.enter.native.prevent="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构号" prop="deptId">
        <treeselect
          v-model="queryParams.deptId"
          :options="deptOptions"
          :show-count="true"
          placeholder="请选择机构号"
          clearable
          style="width: 220px"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" native-type="submit">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workflowList" class="workflow-table" style="width: 100%">
      <el-table-column label="业务方流水号" align="center" prop="serialNum" min-width="320" class-name="workflow-important-column" />
      <el-table-column label="客户内码" align="center" prop="custIsn" min-width="320" class-name="workflow-important-column" />
      <el-table-column label="客户名称" align="center" prop="custName" min-width="240" class-name="workflow-important-column" />
      <el-table-column label="客户类型" align="center" prop="custType" min-width="110" />
      <el-table-column label="担保方式" align="center" prop="guarType" min-width="120" />
      <el-table-column label="申请金额(元)" align="center" prop="applyAmt" min-width="140" />
      <el-table-column label="最终测算利率(%)" align="center" prop="calculateRate" min-width="150">
        <template slot-scope="scope">
          <span>{{ formatRate(scope.row.calculateRate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行利率(%)" align="center" prop="executeRate" min-width="120">
        <template slot-scope="scope">
          <span>{{ formatRate(scope.row.executeRate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="170">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" min-width="220" class-name="workflow-important-column" />
      <el-table-column label="机构号" align="center" prop="deptId" min-width="110" />
      <el-table-column label="操作" align="center" fixed="right" min-width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            v-if="canEdit(scope.row)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 客户类型选择对话框 -->
    <customer-type-selector :visible.sync="showTypeSelector" @select="handleSelectType"/>

    <!-- 客户号查询选择对话框 -->
    <customer-map-selector
      :visible.sync="showCustomerMapSelector"
      :customer-type="selectedCustomerType"
      @select="handleCustomerMapSelect"
    />

    <!-- 个人客户创建对话框 -->
    <personal-create-dialog
      :visible.sync="showPersonalDialog"
      :customer-map="selectedCustomerMap"
      :edit-data="editWorkflow"
      @success="handleCreateSuccess"
    />

    <!-- 企业客户创建对话框 -->
    <corporate-create-dialog
      :visible.sync="showCorporateDialog"
      :customer-map="selectedCustomerMap"
      :edit-data="editWorkflow"
      @success="handleCreateSuccess"
    />
  </div>
</template>

<script>
import {getWorkflowEdit, listWorkflow} from "@/api/loanPricing/workflow"
import {deptTreeSelect} from "@/api/system/user"
import {mapGetters} from "vuex"
import CustomerTypeSelector from "./components/CustomerTypeSelector"
import CustomerMapSelector from "./components/CustomerMapSelector"
import PersonalCreateDialog from "./components/PersonalCreateDialog"
import CorporateCreateDialog from "./components/CorporateCreateDialog"
import {formatRate} from "@/utils/rate"
import Treeselect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"

export default {
  name: "LoanPricingWorkflow",
  components: {
    Treeselect,
    CustomerTypeSelector,
    CustomerMapSelector,
    PersonalCreateDialog,
    CorporateCreateDialog
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 利率定价流程表格数据
      workflowList: [],
      // 机构树选项
      deptOptions: [],
      // 创建时间范围
      dateRange: [],
      // 是否显示客户类型选择弹出层
      showTypeSelector: false,
      // 是否显示客户号查询选择弹出层
      showCustomerMapSelector: false,
      // 当前选择的客户类型
      selectedCustomerType: undefined,
      // 当前选择的客户号映射记录
      selectedCustomerMap: null,
      // 当前编辑的流程记录
      editWorkflow: null,
      // 是否显示个人客户创建弹出层
      showPersonalDialog: false,
      // 是否显示企业客户创建弹出层
      showCorporateDialog: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custIsn: undefined,
        custType: undefined,
        guarType: undefined,
        createBy: undefined,
        deptId: undefined
      }
    }
  },
  created() {
    this.getList()
    this.getDeptTree()
  },
  computed: {
    ...mapGetters([
      'name',
      'nickName'
    ]),
    currentCreateBy() {
      return `${this.nickName}-${this.name}`
    }
  },
  activated() {
    this.getList()
  },
  watch: {
    showPersonalDialog(val) {
      if (!val && !this.showCorporateDialog) {
        this.clearSelectedCustomer()
      }
    },
    showCorporateDialog(val) {
      if (!val && !this.showPersonalDialog) {
        this.clearSelectedCustomer()
      }
    }
  },
  methods: {
    formatRate,
    /** 查询机构下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
      })
    },
    /** 查询利率定价流程列表 */
    getList() {
      this.loading = true
      const hasDateRange = Array.isArray(this.dateRange) && this.dateRange.length === 2
      const query = {
        ...this.queryParams,
        beginCreateTime: hasDateRange ? `${this.dateRange[0]} 00:00:00` : undefined,
        endCreateTime: hasDateRange ? `${this.dateRange[1]} 23:59:59` : undefined
      }
      listWorkflow(query).then(response => {
        this.workflowList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 查看详情操作 */
    handleView(row) {
      this.$router.push({
        name: 'LoanPricingWorkflowDetail',
        params: { serialNum: row.serialNum }
      })
    },
    /** 是否允许编辑 */
    canEdit(row) {
      return row && row.createBy === this.currentCreateBy
    },
    /** 编辑操作 */
    handleEdit(row) {
      getWorkflowEdit(row.serialNum).then(response => {
        this.editWorkflow = response.data
        this.selectedCustomerMap = null
        this.selectedCustomerType = undefined
        if (this.editWorkflow.custType === '个人') {
          this.showPersonalDialog = true
        } else if (this.editWorkflow.custType === '企业') {
          this.showCorporateDialog = true
        }
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editWorkflow = null
      this.showTypeSelector = true
    },
    /** 选择客户类型回调 */
    handleSelectType(type) {
      this.selectedCustomerType = type
      this.selectedCustomerMap = null
      this.showCustomerMapSelector = true
    },
    /** 选择客户号映射记录回调 */
    handleCustomerMapSelect(row) {
      this.editWorkflow = null
      this.selectedCustomerMap = row
      if (this.selectedCustomerType === 'personal') {
        this.showPersonalDialog = true
      } else if (this.selectedCustomerType === 'corporate') {
        this.showCorporateDialog = true
      }
    },
    /** 创建成功回调 */
    handleCreateSuccess() {
      this.clearSelectedCustomer()
      this.getList()
    },
    /** 清空已选客户信息 */
    clearSelectedCustomer() {
      this.selectedCustomerMap = null
      this.selectedCustomerType = undefined
      this.editWorkflow = null
    }
  }
}
</script>

<style scoped>
.workflow-table ::v-deep .workflow-important-column .cell {
  overflow: hidden;
  text-overflow: clip;
  white-space: nowrap;
  word-break: normal;
}
</style>
