<template>
  <div class="main-conetent">

    <el-tabs v-model="activeName" type="border-card" @tab-click="handleTabClick">
      <el-tab-pane label="企业" name="company"> </el-tab-pane>
      <el-tab-pane label="个人" name="person"></el-tab-pane>
      <div class="search">
        <div />
        <div class="right">
          <div>
            <el-input v-model="search.name" :placeholder="activeName==='person'? '请输入姓名/证件号码': '请输入公司名称/统一社会信用代码'" style="width: 300px;" clearable />
          </div>
          <div>
            <el-button type="primary" @click="fetchData()">查询</el-button>
          </div>
          <div>
            <el-button v-show="activeName === 'company'" v-power="'addEnterprise'" type="primary" icon="el-icon-plus" @click="modifyClick('create')">新增</el-button>
            <el-button v-show="activeName === 'person'" v-power="'addPerson'" type="primary" icon="el-icon-plus" @click="modifyClick('create')">新增</el-button>
          </div>
        </div>
      </div>
      <cy-comm-table
        ref="commTable"
        v-loading="loading"
        parent-component="CreditInfoManage"
        :table-options="tableOptions"
        :columns="activeName === 'company'?columns: columnsPerson"
        :data="tableData"
        :count="total"
      />
    </el-tabs>

    <cy-dialog ref="companyDialog" title="新增企业" :top="'15vh'">
      <company-add ref="company"></company-add>
      <div class="submit-btn">
        <el-button @click="$refs.companyDialog.show = false;">关闭</el-button>
        <el-button type="primary" @click="addSure('company')">确定</el-button>
      </div>
    </cy-dialog>

    <cy-dialog ref="personDialog" title="新增个人" :top="'10vh'">
      <person-add ref="person"></person-add>
      <div class="submit-btn">
        <el-button @click="$refs.personDialog.show = false;">关闭</el-button>
        <el-button type="primary" @click="addSure('person')">确定</el-button>
      </div>
    </cy-dialog>

    <cy-dialog ref="companyEditDialog" title="征信报告" :top="'5vh'">
      <company-edit ref="companyEdit" :detail-id="detailId"></company-edit>
      <div class="submit-btn">
        <el-button @click="$refs.companyEditDialog.show = false;">关闭</el-button>
        <el-button type="primary" @click="editSure('companyEdit')">确定</el-button>
      </div>
    </cy-dialog>

    <cy-dialog ref="companyViewDialog" title="征信报告" :top="'5vh'">
      <company-edit ref="companyView" :detail-id="detailId" :type="'view'"></company-edit>
      <div class="submit-btn">
        <el-button @click="$refs.companyViewDialog.show = false;">关闭</el-button>
      </div>
    </cy-dialog>

    <cy-dialog ref="personEditDialog" title="征信报告" :top="'5vh'">
      <person-edit ref="personEdit" :detail-id="detailId"></person-edit>
      <div class="submit-btn">
        <el-button @click="$refs.personEditDialog.show = false;">关闭</el-button>
        <el-button type="primary" @click="editSure('personEdit')">确定</el-button>
      </div>
    </cy-dialog>

    <cy-dialog ref="personViewDialog" title="征信报告" :top="'5vh'">
      <person-edit ref="personView" :detail-id="detailId" :type="'view'"></person-edit>
      <div class="submit-btn">
        <el-button @click="$refs.personViewDialog.show = false;">关闭</el-button>
      </div>
    </cy-dialog>
  </div>
</template>

<script>
import { creditInfoList, addCreditInfo, updateCreditInfo } from '@/api/creditInfoManage/index'
import CompanyAdd from './components/CompanyAdd.vue'
import PersonAdd from './components/PersonAdd.vue'
import CompanyEdit from './components/CompanyEdit.vue'
import PersonEdit from './components/PersonEdit.vue'
export default {
  name: 'CreditInfoManage',
  components: {
    CompanyAdd,
    PersonAdd,
    CompanyEdit,
    PersonEdit
  },
  data() {
    return {
      total: 0,
      page: 1,
      size: 10,
      loading: false,
      activeName: 'company',
      search: {
        name: ''
      },
      tableData: [

      ],
      tableOptions: {
        sortTable: true,
        maxHeight: 'calc(100vh - 420px)',
        height: 'calc(100vh - 350px)'
      },
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '公司名称',
          prop: 'name',
          showTooltip: true
        },
        {
          label: '统一社会信用代码',
          showTooltip: true,
          prop: 'cardNo'
        },
        {
          label: '最近征信查询日期',
          prop: 'searchDate'
        },
        {
          label: '最近更新时间',
          prop: 'gmtModified'
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                msg: '编辑',
                icon: 'iconfont icon-xiugai',
                power: 'CreditUpdateEnterprise',
                category: 'edit'
              },
              {
                msg: '查看',
                icon: 'iconfont icon-Magnifier',
                power: 'CreditViewEnterprise',
                category: 'view'
              }
            ]
            const options = btnList.map(item => {
              return (
                this.tablePower({ item, row }) &&
                <el-tooltip class='item' effect='dark' content={item.msg} placement='top'>
                  <span class='table-icon-box'>
                    <i class={item.icon} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 100
        }
      ],
      columnsPerson: [
        {
          label: '序号',
          index: true
        },
        {
          label: '姓名',
          prop: 'name',
          showTooltip: true
        },
        {
          label: '证件类型',
          prop: 'documentType',
          showTooltip: true,
          render: (h, row) => {
            return <span>{ row.documentType === 1 ? '身份证' : '' }</span>
          }
        },
        {
          label: '证件号码',
          showTooltip: true,
          prop: 'cardNo'
        },
        {
          label: '手机号码',
          prop: 'phone'
        },
        {
          label: '最近征信查询日期',
          prop: 'searchDate'
        },
        {
          label: '最近更新时间',
          prop: 'gmtModified'
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                msg: '编辑',
                icon: 'iconfont icon-xiugai',
                power: 'CreditUpdatePersonal',
                category: 'edit'
              },
              {
                msg: '查看',
                icon: 'iconfont icon-Magnifier',
                power: 'viewCreditViewPersonal',
                category: 'view'
              }
            ]
            const options = btnList.map(item => {
              return (
                this.tablePower({ item, row }) &&
                <el-tooltip class='item' effect='dark' content={item.msg} placement='top'>
                  <span class='table-icon-box'>
                    <i class={item.icon} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 100
        }
      ],
      detailId: null,
      permission: JSON.parse(sessionStorage.getItem('buttons')) || []

    }
  },
  computed: {
    tablePower() {
      return ({ item, row }) => {
        const { power } = item
        if (power && !this.permission.includes(power)) {
          return false
        }
        return true
      }
    }
  },
  created() {
    // 判断是否有企业tab权限
    if (JSON.parse(sessionStorage.getItem('buttons')).includes('enterprise')) {
      this.activeName = 'company'
    } else {
      this.activeName = 'person'
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.loading = true
      const params = {
        page: this.page,
        rows: this.size,
        type: this.activeName === 'company' ? 1 : 0,
        ...this.search
      }
      creditInfoList(params).then(({ rows, records }) => {
        this.loading = false
        this.tableData = rows
        this.total = records
      })
    },
    handlerOperate(type, row) {
      const { id } = row
      this.detailId = Number(id)
      if (this.activeName === 'company') {
        if (type === 'edit') {
          this.$refs.companyEditDialog.show = true
        } else if (type === 'view') {
          this.$refs.companyViewDialog.show = true
        }
      } else if (this.activeName === 'person') {
        if (type === 'edit') {
          this.$refs.personEditDialog.show = true
        } else if (type === 'view') {
          this.$refs.personViewDialog.show = true
        }
      }
    },
    handleTabClick(tab) {
      this.activeName = tab.name
      this.page = 1
      this.size = 10
      this.search.name = ''
      this.fetchData()
    },
    modifyClick(type) {
      if (type === 'create') {
        if (this.activeName === 'company') {
          this.$refs.companyDialog.show = true
        } else if (this.activeName === 'person') {
          this.$refs.personDialog.show = true
        }
      }
    },

    // 弹框开始
    // add
    async addSure(name) {
      const postData = await this.$refs[name].validate()
      addCreditInfo(postData).then((res) => {
        if (res.success) {
          this.$message.success('新增成功')
          this.$refs.companyDialog.show = false
          this.$refs.personDialog.show = false
          this.page = 1
          this.fetchData()
        } else {
          this.$message.warning(res.msg)
        }
      })
    },

    // update
    async editSure(name) {
      if (name === 'companyEdit') {
        if (this.$refs[name].operationShow) {
          const postData = await this.$refs[name].validate()
          updateCreditInfo(postData).then((res) => {
            if (res.success) {
              this.$message.success('编辑成功')
              this.$refs.companyEditDialog.show = false
              // this.page = 1
              this.fetchData()
            } else {
              this.$message.warning(res.msg)
            }
          })
        } else {
          this.$refs.companyEditDialog.show = false
        }
      } else {
        const postData = await this.$refs[name].validate()
        updateCreditInfo(postData).then((res) => {
          if (res.success) {
            this.$message.success('编辑成功')
            this.$refs.personEditDialog.show = false
            // this.page = 1
            this.fetchData()
          } else {
            this.$message.warning(res.msg)
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.main-conetent {

  .submit-btn {
    padding-top: 40px;
    box-shadow: 0 -5px 5px -7px rgba(0, 0, 0, 0.5);
    margin: 0 -20px;
  }

  ::v-deep .el-button {
    margin-left: 0;
  }
}
</style>
