<template>
  <div>
    <a-card size="small" :bordered="false" class="rebateAmountList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper" ref="tableSearch">
        <a-form-model
          id="rebateAmountList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :model="queryParam">
          <a-row :gutter="15">
            <a-col :md="5" :sm="24">
              <a-form-model-item label="状态">
                <v-select
                  v-model="queryParam.subsidyQueryStatus"
                  id="rebateAmountList-subsidyQueryStatus"
                  code="INCREMENT_SUBSIDY_QUERY_STATUS"
                  placeholder="请选择状态"
                  allowClear></v-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button
                type="primary"
                @click="$refs.table.refresh(true)"
                :disabled="disabled"
                id="rebateAmountList-refresh">查询</a-button>
              <a-button
                style="margin-left: 8px"
                @click="resetSearchForm"
                :disabled="disabled"
                id="rebateAmountList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 操作按钮 -->
        <div class="table-operator">
          <a-button id="rebateAmountList-add-btn" type="primary" @click="openRebateAddModal = true">新增</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :style="{ height: tableHeight+70+'px' }"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight}"
          :defaultLoadData="false"
          bordered>
          <!-- $hasPermissions('B_tireSubsidySetting_edit')&& -->
          <!-- 结束时间 -->
          <template slot="endDate" slot-scope="text, record">
            {{ record.status==='END'? record.ruleEndDate:'--' }}
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              class="button-info"
              @click="handleEdit(record)"
              v-if="record.status==='IN_USE'&&record.isSomeTimeFlag"
              :id="'rebateAmountList-edit-btn'+record.id"
            >
              编辑
            </a-button>
            <span v-if="record.status==='FINISH'&&!record.isSomeTimeFlag">--</span>
          </template>
        </s-table>
      </a-spin>
    </a-card>
    <!-- 新增 -->
    <rebate-add-modal
      ref="rebateAdd"
      v-drag
      :openModal="openRebateAddModal"
      :pageType="pageType"
      @ok="$refs.table.refresh(true)"
      @close="openRebateAddModal = false" />
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import rebateAddModal from './rebateAddModal.vue'
// 接口
import { tireSubsidyRuleList } from '@/api/subsidyRule'
export default {
  name: 'RebateAmountList',
  mixins: [commonMixin],
  components: { STable, VSelect, rebateAddModal },
  props: {
    pageType: { //  弹框显示状态
      type: String,
      default: ''
    }
  },
  data () {
    const _this = this
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0, // 表格高度
      openRebateAddModal: false, // 新增弹窗
      //  查询条件
      queryParam: {
        ruleType: undefined, // 页面类型
        subsidyQueryStatus: 'NOT_FINISH'// 状态默认值
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '开始时间', dataIndex: 'newRuleStartDate', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '结束时间', dataIndex: 'ruleEndDate', width: '12%', align: 'center', scopedSlots: { customRender: 'endDate' } },
        { title: '返利基数', dataIndex: 'ruleValue1', width: '12%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '状态', dataIndex: 'statusDictValue', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        // 获取列表数据  有分页
        this.queryParam.ruleType = this.pageType
        const params = Object.assign(parameter, this.queryParam)
        return tireSubsidyRuleList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            // 计算表格序号
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              data.list[i].isSomeTimeFlag = data.list[i].ruleStartDate ? this.isSameMonth(data.list[i].ruleStartDate) : false
              data.list[i].newRuleStartDate = data.list[i].ruleStartDate ? this.formatDate(data.list[i].ruleStartDate) : '--'
              data.list[i].ruleEndDate = data.list[i].ruleEndDate ? this.formatDate(data.list[i].ruleEndDate) : '--'
            }
          }
          this.disabled = false
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 置换时间
    formatDate (input) {
      // 分割日期部分，提取年和月
      const [datePart] = input.split(' ')
      const [year, month] = datePart.split('-')
      // 确保月份为两位数并拼接结果
      return `${year}年${month.padStart(2, '0')}月`
    },
    // 判断当前时间是否和开始时间一致
    isSameMonth (inputDateStr) {
      // 提取输入日期的年、月
      const [inputYear, inputMonth] = inputDateStr.split('-').map(s => parseInt(s))
      // 获取当前年、月（月份需+1，因Date对象月份从0开始）
      const currentDate = new Date()
      const currentYear = currentDate.getFullYear()
      const currentMonth = currentDate.getMonth() + 1
      // 对比结果
      return inputYear === currentYear && inputMonth === currentMonth
    },
    // 编辑
    handleEdit (row) {
      this.$refs.rebateAdd.pageInit(row)
      this.$nextTick(() => {
        this.openRebateAddModal = true
      })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.ruleType = undefined
      this.queryParam.subsidyQueryStatus = 'NOT_FINISH'
      this.$refs.table.refresh(true)
    },
    // 初始化
    pageInit () {
      this.$nextTick(() => { // 页面渲染完成后的回调
        this.setTableH()
      })
      this.resetSearchForm()
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 300
    }
  },
  mounted () {
    this.pageInit()
  }
}
</script>
