<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="5" :sm="8" style="display: none;">
            <a-form-item label="">
              <j-input placeholder="" v-model="queryParam.platformType"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="8">
            <a-form-item label="订单编号">
              <j-input placeholder="请输入订单编号" v-model="queryParam.orderNum"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="8">
            <a-form-item label="状态">
              <a-select style="width: 100%" v-model="queryParam.reconciliationStatus" placeholder="请选择状态">
                <a-select-option v-for="item in chargeList" :key="item.type"
                  :value="item.type">{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-item label="订单时间">
              <a-range-picker @change="onChangeData" :value="createTimeValue" />
            </a-form-item>
          </a-col>
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="5" :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" v-if="days">
      数据列表（以下账单，展示{{days}}天前的账单金额）
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns"
        :dataSource="dataSource" :pagination="ipagination" :loading="loading" class="j-table-force-nowrap"
        @change="handleTableChange">
        <span slot="orderSource" slot-scope="text, record">
          <template v-if="record.orderSource === 'miniapp'">小程序</template>
        </span>
        <span slot="reconciliationStatus" slot-scope="text, record">
          <a-tag color="orange" v-if="record.reconciliationStatus === 1">未对账</a-tag>
          <a-tag color="blue" v-else-if="record.reconciliationStatus === 2">对账待审核</a-tag>
          <a-tag color="green" v-else-if="record.reconciliationStatus === 3">已对账</a-tag>
          <a-tag color="red" v-else-if="record.reconciliationStatus === 4">对账驳回</a-tag>
        </span>
        <span slot="action" slot-scope="text, record">
          <a type="primary" @click="toDetails(record.id)">详情</a>
          <a type="primary" @click="toReconciliation(record)"
            v-if="(record.reconciliationStatus === 1 || record.reconciliationStatus === 4) && record.isShow === '1' " style="margin-left: 15px;">申请对账</a>
        </span>
      </a-table>

    </div>
    <!-- table区域-end -->
    <a-modal centered title="申请对账" :visible="visibleReconciliation" :confirm-loading="confirmLoading" @ok="handleOk"
      @cancel="handleCancel" width="60%">
      <a-form>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="对账总金额" :labelCol="labelCol" :wrapperCol="wrapperCol" style="width: 100%;">
              <a-input v-model="info.amount" placeholder="对账总金额" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="对账日期" :labelCol="labelCol" :wrapperCol="wrapperCol" style="width: 100%;">
              <a-input v-model="info.data" placeholder="对账日期" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" style="width: 100%;">
              <a-textarea v-model="model.remarks" :auto-size="{ minRows: 4, maxRows: 6 }" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>

  </a-card>
</template>

<script>
import { getShareruleApi } from '@/api/api'
import { httpAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

const columns = [
  {
    title: '订单编号',
    dataIndex: 'orderNum',
    key: 'orderNum',
    align: 'center',
  },
  {
    title: '提交时间',
    dataIndex: 'createTime',
    key: 'createTime',
    align: 'center',
  },
  {
    title: '买家名称',
    dataIndex: 'buyerName',
    key: 'buyerName',
    align: 'center',
  },
  {
    title: '分利金额',
    dataIndex: 'shareMoney',
    key: 'shareMoney',
    align: 'center',
  },
  {
    title: '订单金额',
    dataIndex: 'totalMoney',
    key: 'totalMoney',
    align: 'center',
  },
  {
    title: '支付方式',
    dataIndex: 'payWay',
    key: 'payWay',
    align: 'center',
  },
  {
    title: '订单来源',
    dataIndex: 'orderSource',
    scopedSlots: { customRender: 'orderSource' },
    key: 'orderSource',
    align: 'center',
  },
  {
    title: '订单状态',
    dataIndex: 'reconciliationStatus',
    scopedSlots: { customRender: 'reconciliationStatus' },
    key: 'reconciliationStatus',
    align: 'center',
  },
  {
    title: '所属平台',
    dataIndex: 'propertyName',
    key: 'propertyName',
    align: 'center',
  },
  {
    title: '所属小区',
    dataIndex: 'communityName',
    key: 'communityName',
    align: 'center',
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    fixed: 'right',
    width: 150,
  },
]

export default {
  components: {},
  name: 'ReconciliationList',
  mixins: [JeecgListMixin],
  data() {
    return {
      queryParam: {
        platformType: 'partner',
      },
      info: {},
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      chargeList: [
        { type: 1, name: '未对账' },
        { type: 2, name: '对账待审核' },
        { type: 3, name: '已对账' },
        { type: 4, name: '对账驳回' },
      ],
      columns: columns,
      loading: false,
      url: {
        list: '/property-central/partners/shareBenefit/list',
        reconciliation: '/property-central/partners/shareBenefit/reconciliation',
        getSharerule: '/property-central/sharerule/settlementSet/queryById',
      },
      dataSource: [],
      visibleReconciliation: false,
      confirmLoading: false,
      days: '',
    }
  },
  created() {
    //let data = new Date().toLocaleString()
    this.getSharerule()
  },
  methods: {
    async getSharerule() {
      let { result } = await getShareruleApi({ id: 1 })
      if (result.days) {
        this.days = result.days
      }
    },
    onChangeData(date, dateString) {
      if (dateString.length > 0) {
        this.createTimeValue = date
        this.queryParam.createTime_begin = dateString[0]
        this.queryParam.createTime_end = dateString[1]
      }
    },
    initPageParams(params) {
      params['platformType'] = 'partner'
    },
    toDetails(id) {
      this.$router.push({
        path: '/benefiting/ReconciliationDetails?type=detail&id=' + id,
      })
    },
    //申请对账
    toReconciliation(record) {
      this.visibleReconciliation = true
      this.info.amount = record.totalMoney
      this.info.data = record.createTime
      this.model.id = record.id
    },
    handleOk(e) {
      if (!this.model.remarks) {
        this.$message.warning('请填写备注')
        return false
      }
      const that = this
      this.confirmLoading = true
      let method = 'post'
      httpAction(this.url.reconciliation, this.model, method)
        .then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.$emit('ok')
          } else {
            that.$message.warning(res.message)
          }
          that.searchQuery()
          this.visibleReconciliation = false
          this.confirmLoading = false
          this.model = {}
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
    handleCancel(e) {
      this.visibleReconciliation = false
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>