<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.propertyName"></j-input>
            </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-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="onBatchVerify"><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="dataSourceMap"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      >
        <span slot="reconciliationAmount" slot-scope="text, record">
          <span v-if="record.rangeType === 'all'">
            <span v-if="record.shopNum">{{`总平台 ${record.saasBenefits}% 合作伙伴 ${record.partnerBenefits}% 小区 ${record.communityBenefits}%`}}</span>
            <span v-else>{{`总平台 ${record.saasBenefits}% 小区 ${record.communityBenefits}%`}}</span>
          </span>
          <span v-else>
            <p v-for="item in record.shareRuleRefList" :key="item.id">
              总平台 {{item.saasBenefits}}% 合作伙伴 {{item.partnerBenefits}}% 小区 {{item.communityBenefits}}%
            </p>
          </span>
        </span>
        <span slot="rangeType" slot-scope="text, record">
          <span v-if="record.rangeType === 'all'">所有小区</span>
          <span v-else>{{record.shareRuleRefList.map(item=>item.communityName).join(',')}}</span>
        </span>
        <span slot="status" slot-scope="text, record">
          <a-switch :disabled="!record.shopNum" checked-children="启用" un-checked-children="冻结" v-model="record.statusBoolean" @change="flag=>onStatusChange(flag, record)" />
        </span>
        <span slot="action" slot-scope="text, record">
          <a href="javascript:;" @click="propertyDetails(2, record)">编辑</a>
          <a-divider type="vertical" />
          <a href="javascript:;" @click="propertyDetails(3, record)">查看</a>
          <template v-if="!!record.shopNum">
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
              <a>删除</a>
            </a-popconfirm>
          </template>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->
    <default-rule-modal ref="modalForm" @ok="searchQuery"></default-rule-modal>
  </a-card>
</template>

<script>
import { updateRuleStatusApi } from '@/api/api'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import defaultRuleModal from './modules/defaultRuleModal'

const columns = [
  {
    title: '序号',
    dataIndex: 'id',
    key: 'id',
    align: 'center',
  },
  {
    title: '规则名称',
    dataIndex: 'ruleName',
    key: 'ruleName',
    align: 'center',
  },
  {
    title: '所属合作伙伴',
    dataIndex: 'partnerName',
    key: 'partnerName',
    align: 'center',
  },
  {
    title: '限制内容',
    dataIndex: 'reconciliationAmount',
    key: 'reconciliationAmount',
    align: 'center',
    scopedSlots: { customRender: 'reconciliationAmount' }
  },
  {
    title: '限制小区',
    dataIndex: 'rangeType',
    key: 'rangeType',
    align: 'center',
    scopedSlots: { customRender: 'rangeType' }
  },
  {
    title: '已添加数量',
    dataIndex: 'propertyName',
    key: 'propertyName',
    align: 'center',
  },
  {
    title: '添加时间',
    dataIndex: 'createTime',
    key: 'createTime',
    align: 'center',
  },
  {
    title: '是否启用',
    dataIndex: 'status',
    key: 'status',
    align: 'center',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    fixed: 'right',
    width: 150,
  }
]

export default {
  name: 'PermissionListAsync',
  components: {
    defaultRuleModal,
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      // 表头
      columns: columns,
      url: {
        list: '/property-central/sharerule/shareRule/list',
        delete: '/property-central/sharerule/shareRule/delete',
        deleteBatch: '/property-central/sharerule/shareRule/deleteBatch'
      }
    }
  },
  computed: {
    dataSourceMap() {
      return this.dataSource.map(item=> {
        return {
          ...item,
          statusBoolean: item.status === 'normal'   // normal：正常，freeze：冻结
        }
      })
    }
  },
  methods: {
    propertyDetails(type, row) {
      if(row && !row.shopNum) {
        if(type === 2) {
          this.handleEdit(row)
        } else {
          this.handleDetail(row)
        }
      } else {
        let params = row ? `id=${row.id}&type=${type}` : `type=${type}`
        this.$router.push({
          path: '/rule/ruleDetail?' + params
        })
      }
    },
    async onStatusChange(flag, row) {
      console.log(row)
      // value.switchLoading = true
      let status = flag ? 'normal' : 'freeze'
      await updateRuleStatusApi(row.id, status)
      this.$message.success(`${flag ? '启用' : '冻结'}成功！`);
      this.searchQuery()
    },
    onBatchVerify(record) {
      let that = this
      this.$confirm({
        title: '确认批量审核此?',
        closable: true,
        okText: '审核通过',
        cancelText: '审核驳回',
        onOk() {
          // return auditPropertyApi({
          //   id: record.id,
          //   auditStatus: 'auditPass',
          // }).then((res) => {
          //   that.searchQuery()
          // })
        },
        onCancel() {
          // return auditPropertyApi({
          //   id: record.id,
          //   auditStatus: 'refuse',
          // }).then((res) => {
          //   that.searchQuery()
          // })
        },
      })
    },
    handlePerssion(roleId) {
      this.$refs.modalUserRole.show(roleId)
    }
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>