<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="合作伙伴名称">
              <j-input placeholder="请输入合作伙伴名称" v-model="queryParam.partnersName"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="入驻开始时间">
              <a-date-picker placeholder="入驻开始时间" valueFormat="YYYY-MM-DD"
                v-model="queryParam.createTime_begin"></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="入驻结束时间">
              <a-date-picker placeholder="入驻结束时间" valueFormat="YYYY-MM-DD"
                v-model="queryParam.createTime_end"></a-date-picker>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="8" :sm="10">
            <a-form-item label="入驻时间">
              <a-range-picker v-model="queryParam.propertyTime" />
            </a-form-item>
          </a-col> -->
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="propertyDetails(1)" type="primary" icon="plus">新增</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('t_property_settled')">导出</a-button> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{
        selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" v-if="selectedRowKeys.length > 0" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns"
        :dataSource="dataSource" :pagination="pagination" :loading="loading" class="j-table-force-nowrap"
        @change="handleTableChange">
        <span slot="totalDivideProfits" slot-scope="text, record">
          <a v-if="record.totalDivideProfits > 0" @click="showBalance(record.platformCode)">{{record.totalDivideProfits}}
          </a>
          <template v-else>{{record.totalDivideProfits}}</template>
        </span>
        <span slot="empowerEndDate" slot-scope="text, record">
          <span v-if="record.empowerEndDate">{{record.empowerBeginDate}} 至 {{record.empowerEndDate}} </span>
        </span>
        <span slot="partnersStatus" slot-scope="text, record">
          <a-tag color="green" v-if="record.partnersStatus === 'normal'">正常</a-tag>
          <a-tag color="red" v-else>冻结</a-tag>
        </span>
        <span slot="expirationStatus" slot-scope="text, record">
          <a-tag>{{record.expirationStatus === 'notStarted' ? '未开始' :record.expirationStatus === 'normal' ? '正常' :record.expirationStatus === 'soonExpired' ? '即将到期' :record.expirationStatus === 'expire' ? '已过期' : ''}}</a-tag>
        </span>
        <span slot="action" slot-scope="text, record">

          <a href="javascript:;" @click="propertyDetails(4, record.id)">详情</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item v-if="record.auditStatus === 'auditPass'">
                <a @click="handlePerssion(record.roleId)">授权</a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;" @click="propertyDetails(2, record.id)"
                  v-if="record.auditStatus != 'waitAudit'">编辑</a>
              </a-menu-item>
              <a-menu-item v-if="record.auditStatus === 'waitAudit'">
                <a href="javascript:;" @click="propertyDetails(3, record.id)">审核</a>
              </a-menu-item>
              <a-menu-item v-if="record.auditStatus === 'auditPass'">
                <a href="javascript:;"
                  @click="onStatus(record)">{{record.partnersStatus === 'normal' ? '冻结' : '解冻'}}</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)" placement="topLeft">
                  <a style="color: #f00;">删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>

    </div>
    <!-- table区域-end -->
    <!-- 右侧的角色权限配置 -->
    <user-role-modal ref="modalUserRole"></user-role-modal>
    <Share-Balance-Modal ref="ShareBalanceModal"></Share-Balance-Modal>
  </a-card>
</template>

<script>
import { operatePartnersApi } from '@/api/api'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
import UserRoleModal from './modules/UserRoleModal'
import ShareBalanceModal from './modules/ShareBalanceModal'

const columns = [
  {
    title: '合作伙伴名称',
    dataIndex: 'partnersName',
    key: 'partnersName',
    align: 'center',
  },
  {
    title: '统一社会信用代码',
    dataIndex: 'creditCode',
    key: 'creditCode',
    align: 'center',
  },
  {
    title: '联系人',
    dataIndex: 'contactName',
    key: 'contactName',
    align: 'center',
  },
  {
    title: '电话',
    dataIndex: 'contactPhone',
    key: 'contactPhone',
    align: 'center',
  },
  {
    title: '授权时间',
    dataIndex: 'empowerEndDate',
    scopedSlots: { customRender: 'empowerEndDate' },
    align: 'center',
  },
  {
    title: '推广商铺数量',
    dataIndex: 'shopCount',
    key: 'shopCount',
    align: 'center',
  },
  {
    title: '推广费用合计',
    dataIndex: 'totalDivideProfits',
    scopedSlots: { customRender: 'totalDivideProfits' },
    key: 'totalDivideProfits',
    align: 'center',
  },
  {
    title: '审核状态',
    dataIndex: 'auditStatus',
    key: 'auditStatus',
    customRender: function (text) {
      return filterDictTextByCache('auditStatus', text)
    },
  },
  {
    title: '到期提示',
    dataIndex: 'expirationStatus',
    scopedSlots: { customRender: 'expirationStatus' },
    key: 'expirationStatus',
    align: 'center',
  },
  {
    title: '冻结状态',
    dataIndex: 'partnersStatus',
    scopedSlots: { customRender: 'partnersStatus' },
    key: 'partnersStatus',
    align: 'center',
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    fixed: 'right',
    width: 150,
  },
]

export default {
  name: 'PartnersList',
  mixins: [JeecgListMixin],
  components: { UserRoleModal, ShareBalanceModal },
  data() {
    return {
      columns: columns,
      loading: false,
      pagination: {},
      url: {
        list: '/property-central/partners/partnersInfo/list',
        delete: '/property-central/partners/partnersInfo/delete',
        deleteBatch: '/property-central/partners/partnersInfo/deleteBatch',
      },
    }
  },
  methods: {
    propertyDetails(type, id) {
      if (type == 1) {
        this.$router.push({
          path: '/settled/partners/PartnersDetails?type=add',
        })
      } else if (type == 2) {
        this.$router.push({
          path: '/settled/partners/PartnersDetails?type=edit&id=' + id,
        })
      } else if (type == 3) {
        this.$router.push({
          path: '/settled/partners/PartnersDetails?type=audit&id=' + id,
        })
      } else if (type == 4) {
        this.$router.push({
          path: '/settled/partners/PartnersDetails?type=detail&id=' + id,
        })
      }
    },
    // onLoadDetail(record, type) {
    //   record['registAdress'] = [record.provinceCode, record.cityCode, record.countyCode]
    //   if (type === 'edit') {
    //     this.handleEdit(record)
    //   } else {
    //     this.handleDetail(record)
    //   }
    // },
    // onChange(date, dateString) {
    //   if (dateString.length > 0) {
    //     this.queryParam.createTime_begin = dateString[0]
    //     this.queryParam.createTime_end = dateString[1]
    //   } else {
    //     this.queryParam.createTime_begin = ''
    //     this.queryParam.createTime_end = ''
    //   }
    // },
    onStatus(record) {
      let that = this
      this.$confirm({
        title: `确认${record.partnersStatus === 'normal' ? '冻结' : '解冻'}此合作伙伴?`,
        closable: true,
        okText: `${record.partnersStatus === 'normal' ? '冻结' : '解冻'}`,
        onOk() {
          return operatePartnersApi({
            id: record.id,
            status: `${record.partnersStatus === 'normal' ? 'freeze' : 'normal'}`,
          }).then((res) => {
            that.searchQuery()
          })
        },
        onCancel() {},
      })
    },
    handlePerssion(roleId) {
      this.$refs.modalUserRole.show(roleId)
    },
    showBalance(platformCode) {
      if (platformCode) {
        this.$refs.ShareBalanceModal.show(platformCode)
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>