<template>
  <div class="salesList-wrap">
    <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="submitDateChange" />
              </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="salesList-buyerName" @change="custChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="销售单号">
                <a-input id="salesList-salesBillNo" v-model.trim="queryParam.salesBillNo" allowClear placeholder="请输入销售单号"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="超时环节提醒">
                  <a-select v-model="queryParam.timeoutflag" allowClear placeholder="请选择超时环节提醒状态">
                    <a-select-option value="NOT_SENDGOODS">未发货</a-select-option>
                    <a-select-option value="NOT_STOCK">未备货</a-select-option>
                    <a-select-option value="NOT_COLLECTION">未收款</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="审核时间">
                  <rangeDate ref="processRangeDate" :value="examineTime" @change="processDateChange" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="业务状态">
                  <v-select
                    v-model="queryParam.billStatus"
                    ref="billStatus"
                    id="salesList-billStatus"
                    code="SALES_OVERVIEW_BILL_STATUS"
                    placeholder="请选择业务状态"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="发货状态">
                  <v-select
                    v-model="queryParam.sendFlag"
                    ref="sendFlag"
                    id="outboundOrderList-sendFlag"
                    code="SEND_FLAG"
                    placeholder="请选择发货状态"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所属区域/分区">
                  <subarea id="salesList-subarea" ref="subarea" @change="subareaChange"></subarea>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="收款时间">
                  <rangeDate ref="collectionRangeDate" :value="paymentTime" @change="collectionDateChange" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="财务状态">
                  <v-select
                    v-model="queryParam.financialStatus"
                    ref="financialStatus"
                    id="salesList-financialStatus"
                    code="FINANCIAL_RECEIVE_STATUS"
                    placeholder="请选择财务状态"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="是否促销单">
                  <v-select
                    v-model="queryParam.promoFlag"
                    ref="cxFlag"
                    code="FLAG"
                    placeholder="请选择是否促销单"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="省份" prop="provinceSn">
                  <Area id="salesList-provinceSn" v-model="queryParam.provinceSn" placeholder="请选择省"></Area>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="advanced ? 24 : 6" :sm="24" :style="{textAlign: advanced ? 'center' : 'left'}">
              <div class="table-page-search-submitButtons">
                <a-button type="primary" :disabled="disabled" @click="handleSearch">查询</a-button>
                <a-button style="margin-left: 8px" :disabled="disabled" @click="resetSearchForm()">重置</a-button>
                <a @click="advanced=!advanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </div>
            </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+87+'px' }"
          size="small"
          :rowKey="(record) => record.no"
          rowKeyName="no"
          :columns="columns"
          :pageSize="30"
          :data="loadData"
          :scroll="{ y:tableHeight, x:1890 }"
          :defaultLoadData="false"
          bordered>
          <!-- 超时环节提示 -->
          <template slot="tip" slot-scope="text, record">
            <a-tooltip placement="top" v-if="record.dispatchOverDay*1!=0||record.sendOverDay*1!=0||record.settleOverDay*1!=0">
              <template slot="title">
                <div v-if="record.dispatchOverDay*1>0">已超时{{ record.dispatchOverDay }}天(要求销售单下推后3天内备货)</div>
                <div v-if="record.settleOverDay*1>0">已超时{{ record.settleOverDay }}天(要求销售单下推后2天内收款)</div>
                <div v-if="record.sendOverDay*1>0">已超时{{ record.sendOverDay }}天(要求销售单允许打印后3天内发货)</div>
              </template>
              <div class="statusBox">
                <a-icon type="exclamation-circle" theme="filled" :style="{color:'#ed1c24',fontSize:'18px',marginRight:'6px',display:'inlineBlock'}"/>
                <span v-if="record.dispatchOverDay*1>0">未备货</span>
                <span v-if="record.settleOverDay*1>0">未收款</span>
                <span v-if="record.sendOverDay*1>0">未发货</span>
              </div>
            </a-tooltip>
            <span v-else>--</span>
          </template>
          <!-- 销售单号 -->
          <template slot="salesBillNo" slot-scope="text, record">
            <span class="link-bule" @click="handleDetail(record,0)">{{ record.salesBillNo||'--' }}</span>
            <a-badge count="促" v-if="record.promoFlag==1" :number-style="{ backgroundColor: '#52c41a', zoom:'80%' }"></a-badge>
          </template>
          <!-- 备货单号 -->
          <template slot="dispatchBillNo" slot-scope="text, record">
            <span class="link-bule" @click="handleDetail(record,1)">{{ record.dispatchBillNo||'--' }}</span>
          </template>
          <!-- 发货单号  -->
          <template slot="sendBillNo" slot-scope="text, record">
            <span class="link-bule" @click="handleDetail(record,2)">{{ record.sendBillNo||'--' }}</span>
          </template>
        </s-table>

        <!-- 查看销售单或备货单详情 -->
        <commonModal
          :modalTit="detailType?'备货单详情':'销售单详情'"
          bodyPadding="0"
          width="70%"
          :showFooter="false"
          :openModal="showDetailModal"
          @cancel="closeDetailModal">
          <salesDetail v-if="detailType==0" ref="salesDetail" :bizSn="bizSn" @close="closeDetailModal"></salesDetail>
          <dispatchDetail v-if="detailType==1" ref="dispatchDetail" :bizSn="bizSn" @close="closeDetailModal"></dispatchDetail>
        </commonModal>
        <!-- 发货单详情 -->
        <detailModal v-drag ref="detailModal" :openModal="showTipModal" @cancel="showTipModal=false" @ok="sendSuccess"></detailModal>
      </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 subarea from '@/views/common/subarea.js'
import Area from '@/views/common/area.js'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import commonModal from '@/views/common/commonModal.vue'
import customerService from '@/views/common/customerService.vue'
import salesDetail from '@/views/salesManagement/salesQueryNew/detail.vue'
import dispatchDetail from '@/views/salesManagement/pushOrderManagement/detail.vue'
import detailModal from '@/views/salesManagement/sendOutOrder/detailModal.vue'
import { salesOverviewQueryPage } from '@/api/salesNew'
export default {
  name: 'SalesListManagementList',
  mixins: [commonMixin],
  components: { STable, VSelect, dealerSubareaScopeList, rangeDate, subarea, dispatchDetail, commonModal, Area, salesDetail, detailModal, customerService },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      showDetailModal: false,
      showTipModal: false,
      bizSn: null,
      tableHeight: 0,
      detailType: 0,
      examineTime: [],
      paymentTime: [],
      rules: {
        'time': [{ required: true, message: '请选择退货完成时间', trigger: 'change' }]
      },
      // 查询参数
      queryParam: {
        time: [
          moment(getDate.getCurrMonthDays().starttime, 'YYYY-MM-DD'),
          moment(getDate.getCurrMonthDays().endtime, 'YYYY-MM-DD')
        ],
        submitBeginDate: getDate.getCurrMonthDays().starttime,
        submitEndDate: getDate.getCurrMonthDays().endtime,
        salesBillNo: '', //  销售单号
        subareaSn: undefined,
        subareaAreaSn: undefined,
        buyerSn: undefined, //  收货客户
        buyerName: undefined,
        financialStatus: undefined, // 财务状态
        auditBeginDate: undefined, // 审核时间
        auditEndDate: undefined,
        beginDate: undefined, // 收款开始时间
        endDate: undefined,
        billStatus: undefined, //  业务状态
        sendFlag: '', // 发货状态
        promoFlag: undefined,
        provinceSn: undefined, // 省份
        timeoutflag: undefined // 超时环节提醒
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return salesOverviewQueryPage(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
        })
      }
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '超时环节提醒', scopedSlots: { customRender: 'tip' }, width: '160px', align: 'center', fixed: 'left' },
        { title: '提交时间', dataIndex: 'submitDate', width: '100px', align: 'center', customRender: function (text) { return text || '--' }, fixed: 'left' },
        { title: '省份', dataIndex: 'provinceName', align: 'center', width: '80px', customRender: function (text) { return text || '--' }, fixed: 'left' },
        { title: '客户名称', dataIndex: 'buyerName', width: '150px', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true, fixed: 'left' },
        { title: '销售单号', scopedSlots: { customRender: 'salesBillNo' }, width: '120px', align: 'center', fixed: 'left' },
        { title: '备货单号', scopedSlots: { customRender: 'dispatchBillNo' }, width: '120px', align: 'center' },
        { title: '发货单号', scopedSlots: { customRender: 'sendBillNo' }, width: '120px', align: 'center' },
        { title: '所在区域', dataIndex: 'subareaName', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所在分区', dataIndex: 'subareaAreaName', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: '130px', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '收货客户名称', dataIndex: 'receiverName', width: '150px', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '收款方式', dataIndex: 'settleStyleDictValue', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },

        { title: '审核时间', dataIndex: 'auditDate', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '下推数量', dataIndex: 'dispatchTotalQty', width: '80px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '下推金额', dataIndex: 'dispatchTotalAmount', width: '80px', align: 'right', customRender: text => ((text || text == 0) ? _this.toThousands(text) : '--') },
        { title: '收款金额', dataIndex: 'dispatchSettledAmount', width: '80px', align: 'right', customRender: text => ((text || text == 0) ? _this.toThousands(text) : '--') },
        { title: '收款时间', dataIndex: 'dispatchSettleTime', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },

        { title: '备货允许打印时间', dataIndex: 'dispatchAllowPrintTime', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备货时间', dataIndex: 'dispatchAuditDate', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '发货编号', dataIndex: 'dispatchSendNo', width: '80px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '发货创建时间', dataIndex: 'sendCreateDate', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务状态', dataIndex: 'billStatusDictValue', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '发货状态', dataIndex: 'sendFlagDictValue', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },

        { title: '托运日期', dataIndex: 'sendDate', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收货人', dataIndex: 'sendCustomerContactName', width: '80px', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '物流电话', dataIndex: 'sendTransportTele', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '物流单号', dataIndex: 'sendTransportNo', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '总件数', dataIndex: 'sendTotalQty', width: '80px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '运费合计', dataIndex: 'sendTotalAmount', width: '80px', align: 'right', customRender: text => ((text || text == 0) ? _this.toThousands(text) : '--') },
        { title: '收货状态', dataIndex: 'sendReceiveFlagDictValue', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收货时间', dataIndex: 'sendReceiveDate', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '物流情况', dataIndex: 'sendTransportStateDictValue', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'sendRemarks', width: '100px', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ]
      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
    submitDateChange (date) {
      if (date[0] && date[1]) {
        this.queryParam.time = date
      } else {
        this.queryParam.time = []
      }
      this.queryParam.submitBeginDate = date[0] || ''
      this.queryParam.submitEndDate = date[1] || ''
    },
    // 审核时间 change
    processDateChange (date) {
      this.queryParam.auditBeginDate = date[0]
      this.queryParam.auditEndDate = date[1]
    },
    // 收款开始时间  change
    collectionDateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = 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
      }
    },
    // 详情
    handleDetail (row, type) {
      this.detailType = type
      if (type != 2) {
        this.bizSn = type == 0 ? row.salesBillSn : row.dispatchBillSn
        if (this.bizSn) {
          this.showDetailModal = true
        }
      } else {
        const infoObj = {
          sendBillSn: row.sendBillSn,
          customeName: row.buyerName
        }
        if (row.sendBillSn) {
          this.showTipModal = true
          this.$refs.detailModal.getDetail(infoObj, 1, '发货单详情')
        }
      }
    },
    closeDetailModal () {
      this.detailType = 3
      this.showDetailModal = false
      this.bizSn = null
    },
    sendSuccess () {
      this.showTipModal = false
      this.$refs.table.refresh()
    },
    subareaChange (val) {
      this.queryParam.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.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.submitBeginDate = getDate.getCurrMonthDays().starttime
      this.queryParam.submitEndDate = getDate.getCurrMonthDays().endtime
      this.queryParam.salesBillNo = ''
      this.queryParam.subareaSn = undefined
      this.queryParam.subareaAreaSn = undefined
      this.queryParam.buyerName = ''
      this.queryParam.buyerSn = undefined
      this.queryParam.financialStatus = undefined
      this.queryParam.auditBeginDate = undefined
      this.queryParam.auditEndDate = undefined
      this.queryParam.settleBeginDate = undefined
      this.queryParam.settleEndDate = undefined
      this.queryParam.billStatus = undefined
      this.queryParam.sendFlag = ''
      this.queryParam.promoFlag = undefined
      this.queryParam.provinceSn = undefined
      this.queryParam.timeoutflag = undefined
      this.examineTime = []
      this.paymentTime = []
      this.$refs.subarea.clearData()
      if (this.advanced) {
        this.$refs.dealerSubareaScopeList.resetForm()
        this.$refs.processRangeDate.resetDate()
        this.$refs.collectionRangeDate.resetDate()
      }
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      _this.resetSearchForm()
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 220
    }
  },
  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()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .salesList-wrap{
    .statusBox{
      span{
        vertical-align:top;
           }
      span::after{
        content:'/';
      }
      span:last-child::after{
         content:'' !important;
      }
    }
  }
</style>
