<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="业务时间">
                <rangeDate ref="rangeCreateDate" id="salesReturnRemove-createDate" :value="createDate" @change="dateCreateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="客户名称" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                <dealerSubareaScopeList ref="dealerSubareaScopeList" id="salesReturnRemove-dealerSn" @change="custChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="业务单号">
                <a-input id="salesReturnRemove-bizNo" v-model.trim="queryParam.bizNo" allowClear placeholder="请输入业务单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="业务类型">
                <v-select
                  v-model="queryParam.bizType"
                  ref="bizType"
                  id="salesReturnRemove-bizType"
                  code="REMOVE_BIZ_TYPE"
                  placeholder="请选择业务类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="24" :sm="24" style="text-align: center;margin-bottom: 10px;">
              <span class="table-page-search-submitButtons">
                <a-button id="salesReturnRemove-search" type="primary" :disabled="disabled" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button id="salesReturnRemove-reset" style="margin-left: 8px" :disabled="disabled" @click="resetSearchForm()">重置</a-button>
                <a-button
                  style="margin-left: 10px"
                  type="primary"
                  class="button-warning"
                  v-if="$hasPermissions('B_srr_export')"
                  @click="handleExport"
                  :disabled="disabled"
                  :loading="exportLoading"
                  id="salesReturnRemove-export">导出</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-spin :spinning="spinning" tip="Loading...">
        <div class="table-operator" style="flex-grow: 1;display: flex;justify-content: space-between;align-items: center;">
          <div style="flex-grow: 1;display: flex;align-items: center;">
            <!-- 操作按钮 -->
            <div>
              <a-button type="primary" v-if="$hasPermissions('B_srr_add')" @click="handleEdit">新增</a-button>
            </div>
          </div>
          <div></div>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+75+'px' }"
          size="small"
          :rowKey="(record) => record.no"
          rowKeyName="no"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record, index">
            <div>
              <a-button
                size="small"
                type="link"
                class="button-info"
                v-if="record.show&&$hasPermissions('B_srr_edit')"
                @click="handleEdit(record)"
                :id="'salesReturnRemove-edit-btn'+index">编辑</a-button>
              <a-button
                size="small"
                type="link"
                class="button-error"
                v-if="record.show&&$hasPermissions('B_srr_del')"
                @click="handleDel(record)"
                :id="'salesReturnRemove-del-btn'+index">删除</a-button>
            </div>
          </template>
        </s-table>
      </a-spin>
      <!-- 选择客户弹框 -->
      <baseModal :show="openModal" ref="openModal" @ok="handleOk" @cancel="openModal=false"></baseModal>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import baseModal from './baseModal.vue'
import { hdExportExcel } from '@/libs/exportExcel'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import rangeDate from '@/views/common/rangeDate.vue'
import { bizRemoveConfigPageList, bizRemoveConfigDelete, bizRemoveConfigReportExport } from '@/api/bizRemove'
import moment from 'moment'
import getDate from '@/libs/getDate.js'
export default {
  name: 'SalesReturnRemove',
  mixins: [commonMixin],
  components: {
    STable,
    VSelect,
    baseModal,
    dealerSubareaScopeList,
    rangeDate
  },
  data () {
    return {
      spinning: false,
      // 高级搜索 展开/关闭
      advanced: true,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      createDate: [
        moment(getDate.getThreeMonthDays().starttime, 'YYYY-MM-DD'),
        moment(getDate.getCurrMonthDays().endtime, 'YYYY-MM-DD')
      ], //  创建时间
      openModal: false, // 选择客户弹框是否显示
      openDepartUserModal: false,
      // 查询参数
      queryParam: {
        configType: 'BILLING_REPORT',
        beginDate: getDate.getThreeMonthDays().starttime,
        endDate: getDate.getCurrMonthDays().endtime,
        dealerSn: undefined, //  客户名称
        bizNo: undefined, //  业务单号
        bizType: undefined
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        return bizRemoveConfigPageList(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].show = new Date(data.list[i].bizDate).getMonth() == new Date().getMonth()
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      visibleAudit: false
    }
  },
  computed: {
    columns () {
      const a = ['', '剔除']
      const arr = [
        { title: '编号', dataIndex: 'no', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务类型', dataIndex: 'bizTypeDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务单号', dataIndex: 'bizNo', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务状态', dataIndex: 'billStatusName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务时间', dataIndex: 'bizDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'dealerName', width: '16%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '开单统计（客户）', dataIndex: 'salesStatsDealer', width: '10%', align: 'center', customRender: function (text) { return a[text] } },
        { title: '开单统计（省份）', dataIndex: 'salesStatsProvince', width: '10%', align: 'center', customRender: function (text) { return a[text] } },
        { title: '创建时间', dataIndex: 'createDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ]
      return arr
    }
  },
  methods: {
    //  创建时间  change
    dateCreateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 选择客户
    custChange (val) {
      this.queryParam.dealerSn = val.key
    },
    // 导出
    handleExport () {
      const _this = this
      const params = JSON.parse(JSON.stringify(_this.queryParam))
      _this.exportLoading = true
      _this.spinning = true
      hdExportExcel(bizRemoveConfigReportExport, params, '开单剔除配置-', function () {
        _this.exportLoading = false
        _this.spinning = false
      })
    },
    // 编辑
    handleEdit (row) {
      this.openModal = true
      this.$refs.openModal.edit(row)
    },
    handleOk (res) {
      this.$refs.table.refresh()
    },
    // 删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: <div>业务单号：{row.bizNo}<div>确认要删除吗？</div></div>,
        centered: true,
        onOk () {
          _this.spinning = true
          bizRemoveConfigDelete({ bizSn: row.bizSn, configType: 'BILLING_REPORT' }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
            _this.spinning = false
          })
        }
      })
    },
    // 重置查询
    resetSearchForm () {
      this.$refs.rangeCreateDate.resetDate(this.createDate)
      this.queryParam.beginDate = getDate.getThreeMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam.dealerSn = undefined
      this.$refs.dealerSubareaScopeList.resetForm()
      this.queryParam.bizNo = ''
      this.queryParam.bizType = undefined

      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 295
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
