<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form-model
          id="allocateBillList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :rules="rules"
          :model="queryParam"
          @keyup.enter.native="handleSearch">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="提交时间" prop="time">
                <rangeDate ref="rangeDate" :value="queryParam.time" @change="dateChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户名称" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                <dealerSubareaScopeList ref="dealerSubareaScopeList" id="returnSchedule-buyerSn" @change="custChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="总部销退单号">
                <a-input id="returnSchedule-salesReturnBillNo" v-model.trim="queryParam.salesReturnBillNo" allowClear placeholder="请输入总部销退单号"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="业务状态">
                  <v-select
                    v-model="queryParam.billStatus"
                    ref="billStatus"
                    id="returnSchedule-billStatus"
                    code="SALES_RETURN_BILL_STATUS2"
                    placeholder="请选择业务状态"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="退货申请审核时间">
                  <rangeDate ref="rangeExamineDate" @change="dateExamineChange" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="超时环节提醒">
                  <a-select v-model="queryParam.checkflag" allowClear placeholder="请选择超时环节提醒状态">
                    <a-select-option value="unReturnLevel1">!未退货</a-select-option>
                    <a-select-option value="unReturnLevel2">!!未退货</a-select-option>
                    <a-select-option value="oneouttime">!未清点</a-select-option>
                    <a-select-option value="twoouttime">!!未清点</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="退货类别" prop="goodFlag">
                  <v-select
                    v-model="queryParam.goodFlag"
                    ref=" goodFlag"
                    id="returnSchedule-goodFlag"
                    code="GOOD_FLAG"
                    placeholder="请选择退货类别"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24" v-show="isShowCustomerSearch">
                <a-form-item label="客服">
                  <customerService ref="customerName" v-model="queryParam.bizUserSn"></customerService>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所属区域/分区">
                  <subarea id="returnSchedule-subareaSn" ref="subarea" @change="subareaChange"></subarea>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="省份" prop="provinceSn">
                  <Area id="salesReturn-provinceSn" ref="provinceBox" v-model="queryParam.provinceSn" placeholder="请选择省" @change="chooseArea"></Area>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="handleSearch" :disabled="disabled" id="returnSchedule-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="returnSchedule-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+68+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :pageSize="30"
          :data="loadData"
          :scroll="{ x:1300,y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 超时环节提示 -->
          <template slot="tip" slot-scope="text, record">
            <div v-if="record.pickUpFirstAuditDateOverDay">
              <a-tooltip placement="top" v-if="record.pickUpFirstAuditDateOverDay*1>3">
                <template slot="title">
                  <span>已超时{{ record.pickUpFirstAuditDateOverDay }}天(要求提货后3-5天内完成清点)</span>
                </template>
                <div>
                  <a-icon type="exclamation-circle" theme="filled" :style="{color:'#ed1c24',fontSize:'18px',marginRight:'6px',display:'inlineBlock'}"/>
                  <a-icon v-if="record.pickUpFirstAuditDateOverDay*1>5" theme="filled" type="exclamation-circle" :style="{color:'#ed1c24',fontSize:'18px',marginRight:'6px',display:'inlineBlock'}"/>
                  <span style="vertical-align: top;">未清点</span>
                </div>
              </a-tooltip>
              <span v-else>--</span>
            </div>
            <div v-if="!record.pickUpFirstAuditDateOverDay&&record.auditDateOverDay">
              <a-tooltip placement="top" v-if="record.auditDateOverDay*1>=5">
                <template slot="title">
                  <span>已超时{{ record.auditDateOverDay }}天(要求销售退货单审核通过后5天内完成提货)</span>
                </template>
                <div>
                  <a-icon type="exclamation-circle" theme="filled" :style="{color:'#ed1c24',fontSize:'18px',marginRight:'6px',display:'inlineBlock'}"/>
                  <a-icon v-if="record.auditDateOverDay*1>10" theme="filled" type="exclamation-circle" :style="{color:'#ed1c24',fontSize:'18px',marginRight:'6px',display:'inlineBlock'}"/>
                  <span style="vertical-align: top;">未退货</span>
                </div>
              </a-tooltip>
              <span v-else>--</span>
            </div>
            <div v-if="!record.auditDateOverDay&&!record.pickUpFirstAuditDateOverDay">--</div>
          </template>
          <!-- 总部销退单号 -->
          <template slot="salesReturnBillNo" slot-scope="text, record">
            <span class="link-bule" @click="handleDetail(record)">{{ record.salesReturnBillNo }}</span>
          </template>
          <!-- 提货审核时间 -->
          <template slot="pickUpAuditDate" slot-scope="text, record">
            <span v-if="record.pickUpFlag==1">{{ record.pickUpAuditDate }}</span>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
import getDate from '@/libs/getDate.js'
import rangeDate from '@/views/common/rangeDate.vue'
import { STable, VSelect } from '@/components'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import chooseWarehouse from '@/views/common/chooseWarehouse'
import customerService from '@/views/common/customerService'
import subarea from '@/views/common/subarea.js'
import Area from '@/views/common/area.js'
import { queryOverviewPage } from '@/api/salesReturn'
export default {
  name: 'ReturnSchedule',
  mixins: [commonMixin],
  components: { STable, VSelect, subarea, dealerSubareaScopeList, rangeDate, chooseWarehouse, Area, customerService },
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      queryParam: { //  查询条件
        time: [
          moment(getDate.getCurrMonthDays().starttime, 'YYYY-MM-DD'),
          moment(getDate.getCurrMonthDays().endtime, 'YYYY-MM-DD')
        ],
        beginSubmitDate: getDate.getCurrMonthDays().starttime,
        endSubmitDate: getDate.getCurrMonthDays().endtime,
        beginAuditDate: undefined,
        endAuditDate: undefined,
        buyerSn: undefined,
        buyerName: undefined,
        subareaArea: {
          subareaSn: undefined,
          subareaAreaSn: undefined
        },
        goodFlag: undefined,
        salesReturnBillNo: '', //  单号
        billStatus: undefined, //  业务状态
        provinceName: undefined,
        provinceSn: undefined,
        salesReturnType: undefined, // 退货类别
        checkflag: undefined, // 超时环节提醒
        bizUserSn: undefined
      },
      rules: {
        'time': [{ required: true, message: '请选择退货完成时间', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      advanced: true,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return queryOverviewPage(Object.assign(parameter, this.queryParam)).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
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      openModal: false,
      itemSn: ''
    }
  },
  // 根据权限显示列表字段
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '超时环节提醒', scopedSlots: { customRender: 'tip' }, width: '130px', align: 'center', fixed: 'left' },
        { title: '提交时间', dataIndex: 'submitTime', width: '150px', align: 'center', customRender: function (text) { return text || '--' }, fixed: 'left' },
        { title: '单据来源', dataIndex: 'salesReturnBillSourceDictValue', width: '100px', align: 'center', customRender: function (text) { return text || '--' }, fixed: 'left' },
        { title: '省份', dataIndex: 'provinceName', align: 'center', width: '100px', customRender: function (text) { return text || '--' }, fixed: 'left' },
        { title: '客户名称', dataIndex: 'buyerName', align: 'left', width: '200px', customRender: function (text) { return text || '--' }, ellipsis: true, fixed: 'left' },
        { title: '总部销退单号', scopedSlots: { customRender: 'salesReturnBillNo' }, width: '130px', align: 'center', fixed: 'left' },
        { title: '退货类别', dataIndex: 'goodFlagDictValue', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所在区域', dataIndex: 'subareaArea.subareaName', align: 'center', width: '130px', customRender: function (text) { return text || '--' } },
        { title: '所在分区', dataIndex: 'subareaArea.subareaAreaName', align: 'center', width: '130px', customRender: function (text) { return text || '--' } },
        { title: '退货类型', dataIndex: 'salesReturnTypeDictValue', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },

        { title: '申请退货数量', dataIndex: 'totalInitialQty', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '申请退货金额', dataIndex: 'initialAmount', width: '100px', align: 'right', customRender: text => ((text || text == 0) ? _this.toThousands(text) : '--') },
        { title: '退货申请审核时间', dataIndex: 'auditTime', width: '150px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '提货审核时间', dataIndex: 'pickUpAuditDate', width: '150px', align: 'center', scopedSlots: { customRender: 'pickUpAuditDate' } },
        { title: '收货时间', dataIndex: 'receiveTime', width: '150px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '品检时间', dataIndex: 'checkTime', width: '150px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客服确认时间', dataIndex: 'customerServiceConfirmTime', width: '150px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库实收数量', dataIndex: 'totalReceiveQty', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '坏件数量', dataIndex: 'totalBadQty', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '良品数量', dataIndex: 'totalGoodQty', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '返库数量', dataIndex: 'totalBackStockQty', width: '100px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '实际退货金额', dataIndex: 'totalAmount', width: '100px', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '业务状态', dataIndex: 'billStatusDictValue', width: '100px', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      return arr
    }
  },
  methods: {
    // 查询
    handleSearch () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.$refs.table.refresh(true)
        } else {
          _this.$message.error('请选择提交时间')
          return false
        }
      })
    },
    //  提交时间  change
    dateChange (date) {
      if (date[0] && date[1]) {
        this.queryParam.time = date
      } else {
        this.queryParam.time = []
      }
      this.queryParam.beginSubmitDate = date[0] || ''
      this.queryParam.endSubmitDate = date[1] || ''
    },
    // 审核时间
    dateExamineChange (date) {
      this.queryParam.beginAuditDate = date[0]
      this.queryParam.endAuditDate = date[1]
    },
    // 客户名称
    custChange (val) {
      if (val.row) {
        this.queryParam.buyerSn = val.row.dealerSn
        this.queryParam.buyerName = undefined
      } else {
        this.queryParam.buyerSn = undefined
        this.queryParam.buyerName = val.key
      }
    },
    // 区域分区
    subareaChange (val) {
      this.queryParam.subareaArea.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaArea.subareaAreaSn = val[1] ? val[1] : undefined
    },
    //  重置
    resetSearchForm () {
      this.queryParam.time = [
        getDate.getCurrMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ]
      this.$refs.rangeDate.resetDate(this.queryParam.time)
      this.queryParam.beginSubmitDate = getDate.getCurrMonthDays().starttime
      this.queryParam.endSubmitDate = getDate.getCurrMonthDays().endtime

      this.queryParam.beginAuditDate = undefined
      this.queryParam.endAuditDate = undefined
      this.$refs.rangeExamineDate.resetDate()

      this.queryParam.subareaArea.subareaSn = undefined
      this.queryParam.subareaArea.subareaAreaSn = undefined
      this.$refs.subarea.clearData()
      this.queryParam.buyerSn = undefined
      this.queryParam.buyerName = undefined
      this.queryParam.salesReturnBillNo = ''
      this.queryParam.billStatus = undefined
      this.queryParam.provinceSn = undefined
      this.queryParam.provinceName = undefined
      this.queryParam.salesReturnType = undefined
      this.queryParam.goodFlag = undefined
      this.queryParam.checkflag = undefined
      this.queryParam.bizUserSn = undefined
      if (this.advanced) {
        this.$refs.dealerSubareaScopeList.resetForm()
      }
      this.$refs.table.refresh(true)
    },
    // 详情
    handleDetail (row) {
      this.$router.push({ name: 'salesReturnDetail', params: { sn: row.salesReturnBillSn }, query: { path: 'returnSchedule' } })
    },
    // 选择省份
    chooseArea (con, item) {
      this.queryParam.provinceSn = con
      this.queryParam.provinceName = item.name
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 200
    }
  },
  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>

<style lang="less">
  .returnSchedule-wrap{
    .ant-tooltip{
      max-width:300px !important;
    }
  }
</style>
