<template>
  <div class="customer-wrap">
    <div>
      <el-radio-group class="header-radio" v-model="selectedTab" @input="handleChange">
        <el-radio-button label="2" :disabled="isPrivate" :class="{ 'btn-disabled': isPrivate }">企业</el-radio-button>
        <el-radio-button label="0" :disabled="isPublic" :class="{ 'btn-disabled': isPublic }">个人</el-radio-button>
        <el-radio-button label="1" :disabled="isPublic" :class="{ 'btn-disabled': isPublic }">商户</el-radio-button>
      </el-radio-group>
      <section class="search-box" v-if="showSearch">
        <section class="search-cnt">
          <el-input v-model="searchQuery" :placeholder="searchPlace" clearable @clear="handleSearch(searchQuery)"
            @keyup.enter.native="handleSearch(searchQuery)">
          </el-input>
          <section class="search-btn" @click="handleSearch(searchQuery)">
            <i class="el-icon-search"></i>
          </section>
        </section>
      </section>
      <section class="operate-cnt">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="handleRefersh" :columns="columns"
          style="margin-left: auto"></right-toolbar>
        <el-button type="primary" size="small" style="margin-left: 10px" @click="handleAdd">新客登记</el-button>
      </section>
      <div class="personage-box" v-if="isPersonage">
        <section class="table-wrap">
          <el-table :data="tableData" style="width: 100%" v-loading="perLoading" max-height="615">
            <el-table-column align="center" key="custName" prop="custName" width="400" label="客户姓名" show-overflow-tooltip
              v-if="columns[0].visible">
              <template slot-scope="scope">
                <el-button type="text" @click="handleCustDetail(scope.row.id)">{{ scope.row.custName }}</el-button>
              </template>
            </el-table-column>
            <el-table-column align="center" key="custIdc" prop="custIdc" label="证件号" v-if="columns[1].visible">
              <template slot-scope="scope">
                <el-button type="text" @click="handleCustDetail(scope.row.id)">{{ scope.row.custIdc }}</el-button>
              </template>
            </el-table-column>
            <el-table-column align="center" key="custPhone" prop="custPhone" label="手机号"
              v-if="columns[2].visible"></el-table-column>
            <el-table-column align="center" key="custId" prop="custId" label="客户号"
              v-if="columns[3].visible"></el-table-column>
          </el-table>
          <!-- 分页区域 -->
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="->,total,prev,pager,next,sizes" :total="total"
            :current-page="pageNum"></el-pagination>
        </section>
      </div>
      <div class="commercial-box" v-if="isCommercial">
        <el-table :data="commercialTableData" style="width: 100%" v-loading="comLoading" max-height="615">
          <el-table-column align="center" prop="custName" width="400" label="商户名称" show-overflow-tooltip
            v-if="columns[0].visible">
            <template slot-scope="scope">
              <el-button type="text" @click="handleCommercialDetail(scope.row.id)">{{ scope.row.custName }}</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="lpName" label="经营者" v-if="columns[1].visible"></el-table-column>
          <el-table-column align="center" prop="custIdc" label="证件号" v-if="columns[2].visible">
            <template slot-scope="scope">
              <el-button type="text" @click="handleCommercialDetail(scope.row.id)">{{ scope.row.custIdc }}</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="custPhone" label="手机号" v-if="columns[3].visible"></el-table-column>

        </el-table>
        <!-- 分页区域 -->
        <el-pagination @size-change="commercialSizeChange" @current-change="commercialCurrentChange"
          :page-sizes="[10, 20, 30, 40]" :page-size="commercialPageSize" layout="->,total,prev,pager,next,sizes"
          :total="commercialTotal" :current-page="commercialPageNum"></el-pagination>
      </div>
      <div class="firm-box" v-if="isFirm">
        <el-table :data="firmTableData" style="width: 100%" v-loading="firmLoading" max-height="615">
          <el-table-column align="center" prop="custName" width="400" label="企业名称" show-overflow-tooltip
            v-if="columns[0].visible">
            <template slot-scope="scope">
              <el-button type="text" @click="handleFirmDetail(scope.row.id)">{{ scope.row.custName }}</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="lpName" label="企业法人" v-if="columns[1].visible"></el-table-column>
          <el-table-column align="center" prop="custIdc" label="证件号" v-if="columns[2].visible">
            <template slot-scope="scope">
              <el-button type="text" @click="handleFirmDetail(scope.row.id)">{{ scope.row.custIdc }}</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="custPhone" label="手机号" v-if="columns[3].visible"></el-table-column>

        </el-table>
        <!-- 分页区域 -->
        <el-pagination @size-change="firmSizeChange" @current-change="firmCurrentChange" :page-sizes="[10, 20, 30, 40]"
          :page-size="firmPageSize" layout="->,total,prev,pager,next,sizes" :total="firmTotal"
          :current-page="firmPageNum"></el-pagination>
      </div>

    </div>
  </div>
</template>
<script>
import { listByCustType } from '@/api/system/listByRetailKeywords.js'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      perLoading: false,
      comLoading: false,
      firmLoading: false,
      isPerLoading: false,
      isCommerLoading: false,
      isFirmLoading: false,
      showSearch: true,
      selectedTab: '2',
      tableData: [

      ],
      total: 0,
      pageSize: 10,
      pageNum: 1,
      searchQuery: '',
      columns: [

      ],
      isPersonage: false,
      isCommercial: false,
      isFirm: true,
      searchPlace: '输入客户名/证件号/手机号',
      commercialTableData: [],
      commercialPageSize: 10,
      commercialPageNum: 1,
      commercialTotal: 0,
      firmTableData: [],
      firmTotal: 0,
      firmPageSize: 10,
      firmPageNum: 1,
      deptId: '',
      deptName: '',
      custType: '2'
    }
  },
  computed: {
    ...mapGetters(['roles']),
    //对公
    isPublic() {
      return this.roles.includes('headPublic')
    },
    //对私
    isPrivate() {
      return this.roles.includes('headPrivate')
    },

  },
  created() {

    console.log(this.$route.query, 'sssssssss')
    if (this.$route.query.selectType) {
      if (this.$route.query.selectType == '0') {
        this.isPersonage = true
        this.isCommercial = false
        this.isFirm = false
        this.selectedTab = '0'
        this.custType = '0'
        this.searchPlace = '输入客户名/证件号/手机号'
        this.columns.push(
          { key: 0, label: '客户姓名', visible: true },
          { key: 1, label: '证件号', visible: true },
          { key: 2, label: '手机号', visible: true },
          { key: 3, label: '客户号', visible: true }
        )
        this.initListData()

      } else if (this.$route.query.selectType == '1') {
        this.selectedTab = '1'
        this.isPersonage = false
        this.isCommercial = true
        this.isFirm = false
        this.custType = '1'
        this.searchPlace = '输入商户名/证件号/手机号'
        this.columns.push(
          { key: 4, label: '商户名称', visible: true },
          { key: 5, label: '企业法人', visible: true },
          { key: 6, label: '证件号', visible: true },
          { key: 7, label: '手机号', visible: true }
        )
        this.initListData()
      } else {
        this.selectedTab = '2'
        this.isPersonage = false
        this.isCommercial = false
        this.isFirm = true
        this.custType = '2'
        this.searchPlace = '输入企业名/证件号/手机号'
        this.columns.push(
          { key: 8, label: '企业名称', visible: true },
          { key: 9, label: '企业法人', visible: true },
          { key: 10, label: '证件号', visible: true },
          { key: 11, label: '手机号', visible: true }
        )
        this.initListData()
      }
      for (let key in this.$route.query) {
        delete this.$route.query[key];
      }
    } else {
      if (this.isPublic) {
        this.isPersonage = false
        this.isCommercial = false
        this.isFirm = true
        this.selectedTab = '2'
        this.custType = '2'
        this.searchPlace = '输入企业名/证件号/手机号'
        this.columns.push(
          { key: 8, label: '企业名称', visible: true },
          { key: 9, label: '企业法人', visible: true },
          { key: 10, label: '证件号', visible: true },
          { key: 11, label: '手机号', visible: true }
        )
        this.initListData()
      } else if (this.isPrivate) {
        this.isPersonage = true
        this.isCommercial = false
        this.isFirm = false
        this.selectedTab = '0'
        this.custType = '0'
        this.searchPlace = '输入客户名/证件号/手机号'
        this.columns.push(
          { key: 0, label: '客户姓名', visible: true },
          { key: 1, label: '证件号', visible: true },
          { key: 2, label: '手机号', visible: true },
          { key: 3, label: '客户号', visible: true }
        )
        this.initListData()
      } else {
        this.isPersonage = false
        this.isCommercial = false
        this.isFirm = true
        this.custType = '2'
        this.selectedTab = '2'
        this.searchPlace = '输入企业名/证件号/手机号'
        this.columns.push(
          { key: 8, label: '企业名称', visible: true },
          { key: 9, label: '企业法人', visible: true },
          { key: 10, label: '证件号', visible: true },
          { key: 11, label: '手机号', visible: true }
        )
        this.initListData()
      }
    }
  },
  methods: {
    handleChange(val) {
      this.columns = []
      this.searchQuery = ''
      if (this.selectedTab == '0') {
        this.isPersonage = true
        this.isCommercial = false
        this.isFirm = false
        this.custType = '0'
        this.searchPlace = '输入客户名/证件号/手机号'
        this.columns.push(
          { key: 0, label: '客户姓名', visible: true },
          { key: 1, label: '证件号', visible: true },
          { key: 2, label: '手机号', visible: true },
          { key: 3, label: '客户号', visible: true }
        )
        this.initListData()
      } else if (this.selectedTab == '1') {
        this.isPersonage = false
        this.isCommercial = true
        this.isFirm = false
        this.custType = '1'
        this.searchPlace = '输入商户名/证件号/手机号'
        this.columns.push(
          { key: 4, label: '商户名称', visible: true },
          { key: 5, label: '企业法人', visible: true },
          { key: 6, label: '证件号', visible: true },
          { key: 7, label: '手机号', visible: true }
        )
        this.initListData()
      } else if (this.selectedTab == '2') {
        this.isPersonage = false
        this.isCommercial = false
        this.isFirm = true
        this.custType = '2'
        this.searchPlace = '输入企业名/证件号/手机号'
        this.columns.push(
          { key: 8, label: '企业名称', visible: true },
          { key: 9, label: '企业法人', visible: true },
          { key: 10, label: '证件号', visible: true },
          { key: 11, label: '手机号', visible: true }
        )
        this.initListData()
      }
    },
    initListData() {

      let params = {}
      if (this.selectedTab == '2') {
        params = {
          keyword: this.searchQuery,
          pageSize: this.firmPageSize,
          pageNum: this.firmPageNum,
          custType: this.custType
        }
      } else if (this.selectedTab == '1') {
        params = {
          keyword: this.searchQuery,
          pageSize: this.commercialPageSize,
          pageNum: this.commercialPageNum,
          custType: this.custType
        }
      } else if (this.selectedTab == '0') {
        params = {
          keyword: this.searchQuery,
          pageSize: this.pageSize,
          pageNum: this.pageNum,
          custType: this.custType
        }
      }
      listByCustType(params).then(res => {

        if (res.code == 200) {
          if (this.selectedTab == '2') {
            this.firmTableData = res.rows
            this.firmTotal = res.total
            this.isPersonage = false
            this.isCommercial = false
            this.isFirm = true
          } else if (this.selectedTab == '1') {
            this.commercialTableData = res.rows
            this.commercialTotal = res.total
            this.isPersonage = false
            this.isCommercial = true
            this.isFirm = false
          } else if (this.selectedTab == '0') {
            this.tableData = res.rows
            this.total = res.total
            this.isPersonage = true
            this.isCommercial = false
            this.isFirm = false
          }
        }
      })


    },
    handleSizeChange(newSize) {
      this.pageSize = newSize
      this.initListData()
    },
    handleCurrentChange(newPage) {
      this.pageNum = newPage
      this.initListData()
    },

    commercialSizeChange(val) {
      this.commercialPageSize = val
      this.initListData()
    },
    commercialCurrentChange(val) {
      this.commercialPageNum = val
      this.initListData()
    },

    firmSizeChange(val) {
      this.firmPageSize = val
      this.initListData()
    },
    firmCurrentChange(val) {
      this.firmPageNum = val
      this.initListData()
    },
    handleSearch() {
      if (this.selectedTab == '0') {
        this.initListData()
      } else if (this.selectedTab == '1') {
        this.initListData()
      } else if (this.selectedTab == '2') {
        this.initListData()
      }
    },
    handleRefersh() {
      this.searchQuery = ''
      if (this.selectedTab == '0') {
        this.initListData()
      } else if (this.selectedTab == '1') {
        this.initListData()
      } else if (this.selectedTab == '2') {
        this.initListData()
      }
    },
    handleCustDetail(id) {
      this.$router.push({ path: '/filing/filingpersonDetail', query: { id: id, type: '0', selectType: '0' } })

    },
    handleCommercialDetail(id) {
      this.$router.push({ path: '/filing/filingpersonDetail', query: { id: id, type: '1', selectType: '1' } })
    },
    handleFirmDetail(id) {
      this.$router.push({ path: '/filing/filingpersonDetail', query: { id: id, type: '2', selectType: '2' } })
    },

    handleAdd() {
      if (this.selectedTab == '0') {
        this.$router.push({ path: '/customer/charts/filing/personFiling' })
      } else if (this.selectedTab == '1') {
        this.$router.push({ path: '/customer/charts/filing/commercialFiling' })
      } else if (this.selectedTab == '2') {
        this.$router.push({ path: '/customer/charts/filing/firmFiling' })
      }

    },
  },

}
</script>
<style lang="scss" scoped>
.customer-wrap {
  background-color: #ffffff;
  overflow: hidden;
  box-shadow: 0 3px 8px 0 #00000017;
  border-radius: 16px 16px 0 0;

  .header-radio {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #ebebeb;

    .btn-disabled {
      ::v-deep .el-radio-button__inner {
        background-color: #e7e7e7;
      }
    }

    .el-radio-button {
      flex: 1;

      ::v-deep .el-radio-button__inner {
        width: 100%;
        border: none;
        font-weight: 400;
        letter-spacing: 0.44px;
        line-height: 25px;
        font-size: 16px;
        color: #666666;
        padding: 11px 0 12px 0;
      }

      ::v-deep .el-radio-button__orig-radio:checked+.el-radio-button__inner {
        background-color: #4886f8;
        font-weight: 400;
        color: #ffffff;
        border-bottom-left-radius: 0;
        border-bottom-right-radius: 0;
      }

      &:nth-child(2) {

        &::before,
        &::after {
          content: '';
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          height: 21px;
          width: 1px;
          background: #ebebeb;
          z-index: 1;
        }

        &::after {
          right: 1px;
        }
      }

      &.is-active {

        &::before,
        &::after {
          content: none;
        }
      }
    }
  }

  .search-box {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    margin: 24px 0 0 0;

    .search-cnt {
      position: relative;
      width: 70%;
    }

    ::v-deep .el-input {
      width: 100%;
      position: relative;

      .el-input__inner {
        height: 44px;
        line-height: 44px;
        border-radius: 23px;
        border: 1px solid #ebebeb;
        padding-left: 30px;
      }

      .el-input__suffix {
        right: 85px;
      }
    }

    .search-btn {
      width: 80px;
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
      position: absolute;
      right: 0;
      top: 2px;
      border: 1px solid #4886f8;
      background-color: #4886f8;
      border-radius: 20px;
      cursor: pointer;
      z-index: 1;

      .el-icon-search {
        color: #ffffff;
        font-size: 14px;
      }
    }
  }
}

.title {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.checkBox {
  display: flex;

  .el-checkbox-group {
    display: flex;
    align-items: center;

    ::v-deep .el-checkbox__label {
      color: #3d3d3d;
      font-size: 12px;
      font-weight: 400;
      padding-left: 6px;
    }
  }
}

.radio-labels {
  height: 32px;
  line-height: 32px;
  color: #3d3d3d;
  font-size: 14px;
  font-weight: 400;
  margin-right: 30px;

  &::after {
    content: '*';
    left: 5px;
    color: #ff4d42;
    position: relative;
    top: 2px;
  }
}

.search-box {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  margin: 24px 0;

  .search-cnt {
    position: relative;
    width: 70%;
  }

  ::v-deep .el-input {
    width: 100%;
    position: relative;

    .el-input__inner {
      height: 44px;
      line-height: 44px;
      border-radius: 23px;
      border: 1px solid #ebebeb;
    }

    .el-input__suffix {
      right: 85px;
    }
  }

  .search-btn {
    width: 80px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    right: 0;
    top: 2px;
    border: 1px solid #4886f8;
    background-color: #4886f8;
    border-radius: 20px;
    cursor: pointer;
    z-index: 1;

    .el-icon-search {
      color: #ffffff;
      font-size: 14px;
    }
  }
}

a {
  color: rgb(7, 58, 177);
  text-decoration: none;
}

.main-wrap {
  margin: 0 30px;

  .checkBox {
    margin-bottom: 12px;

    &:last-child {
      margin-bottom: 0;
    }
  }
}

.operate-cnt {
  margin: 24px 0 16px 0;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;

  .top-right-btn {
    margin-right: 24px;
    padding-bottom: 6px;
  }

  .plain-raido-btn {
    ::v-deep .el-radio-button__inner {
      color: #666666;
      font-size: 14px;
      font-weight: 400;
      border: 1px solid #ebebeb;
      border-radius: 0;
    }

    ::v-deep .el-radio-button__orig-radio:checked+.el-radio-button__inner {
      border: 1px solid #4886f8;
      color: #4886f8;
      background-color: #ffffff;
      box-shadow: none;
    }
  }
}

.addDialog {
  ::v-deep .el-input {
    width: 200px;
  }
}

::v-deep .el-dialog__footer {
  text-align: center;
}

.commit_btn {
  margin-left: 50px;
}
</style>