<template>
  <div class="receiptPrint-wrap">
    <div style="background-color: #fff;">
      <a-tabs v-model="currentTab" @change="changeTab">
        <a-tab-pane :key="1" tab="全部">
        </a-tab-pane>
        <a-tab-pane :key="2" tab="待打印">
        </a-tab-pane>
      </a-tabs>
    </div>
    <a-card style="margin-top: 5px;" size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 搜索条件 -->
        <div ref="tableSearch" class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-item label="创建时间">
                  <rangeDate ref="rangeDate" :value="time" @change="dateChange" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="客户名称">
                  <dealerSubareaScopeList ref="settleClientName" id="salesCollectionList-settleClientName" @change="custChange" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="销售单号">
                  <a-input id="salesCollectionList-bizNo" v-model.trim="queryParam.bizNo" allowClear placeholder="请输入销售单号"/>
                </a-form-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="6" :sm="24">
                  <a-form-item label="备货单号">
                    <a-input id="salesCollectionList-dispatchBillNo" v-model.trim="queryParam.dispatchBillNo" allowClear placeholder="请输入备货单号"/>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="收货客户名称">
                    <dealerSubareaScopeList ref="receiverName" id="salesCollectionList-receiverName" @change="receiverChange" />
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="业务状态">
                    <v-select
                      v-model="queryParam.billStatus"
                      ref="billStatus"
                      id="salesCollectionList-billStatus"
                      code="DISPATCH_BILL_STATUS"
                      placeholder="请选择业务状态"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="单据状态">
                    <v-select
                      v-model="queryParam.voidFlag"
                      ref="voidFlag"
                      id="salesCollectionList-voidFlag"
                      code="VOID_FLAG"
                      placeholder="请选择单据状态"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24" v-if="currentTab == 1">
                  <a-form-item label="收款打印状态">
                    <v-select
                      v-model="queryParam.printState"
                      ref="printState"
                      id="salesCollectionList-printState"
                      code="RECEIPT_PRINT"
                      placeholder="请选择打印状态"
                      allowClear
                    ></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="所在区域">
                    <subarea id="salesCollectionList-subarea" v-model="queryParam.subareaSn"></subarea>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-row>
                    <a-form-item label="地区">
                      <a-col span="24">
                        <a-form-item prop="dealerProvinceSn" style="margin: 0;">
                          <Area id="salesCollectionList-dealerProvinceSn" v-model="queryParam.dealerProvinceSn" placeholder="请选择省"></Area>
                        </a-form-item>
                      </a-col>
                    </a-form-item>
                  </a-row>
                </a-col>
              </template>
              <a-col :md="6" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" :disabled="disabled" @click="$refs.table.refresh(true)">查询</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>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+60.5+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y:tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 单号 -->
          <template slot="bizNo" slot-scope="text, record">
            <span v-if="$hasPermissions('B_salesDetail')" class="link-bule" @click="handleDetail(record)">{{ record.bizNo }}</span>
            <span v-else>{{ record.bizNo }}</span>
          </template>
          <!-- 下推单号 -->
          <template slot="dispatchBillNo" slot-scope="text, record">
            <span v-if="$hasPermissions('B_dispatchDetail')" class="link-bule" @click="handleBhDetail(record)">{{ record.dispatchBillNo }}</span>
            <span v-else>{{ record.dispatchBillNo || '--' }}</span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="record.printState == 'NO_PRINT'&&$hasPermissions('B_SkPrint')"
              class="button-warning"
              @click="handlePrint(record)"
            >收款打印</a-button>
            <a-button
              size="small"
              type="link"
              class="button-warning"
              v-else-if="record.printState == 'PRINT'&&$hasPermissions('B_SKPrintRecord')"
              @click="viewPrint(record)"
            >打印记录</a-button>
            <span v-else>--</span>
          </template>
        </s-table>

        <!-- 收款打印 -->
        <commonModal
          modalTit="收款打印"
          bodyPadding="10px"
          width="1024px"
          :showFooter="false"
          :openModal="showTipModal"
          @cancel="showTipModal=false">
          <printModel ref="printModel" @cancel="showTipModal=false" @printOk="printOk"></printModel>
        </commonModal>

        <!-- 打印记录 -->
        <recordModal ref="recordModal" modalTit="打印记录" :openModal="showRecordModal" @cancel="showRecordModal=false"></recordModal>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
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 recordModal from './recordModal.vue'
import printModel from './printModel.vue'
import { settleReceiptList } from '@/api/settleReceipt'
export default {
  name: 'PushOrderManagementList',
  mixins: [commonMixin],
  components: { STable, VSelect, dealerSubareaScopeList, rangeDate, subarea, commonModal, recordModal, Area, printModel },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      time: [],
      currentTab: 2,
      // 查询参数
      queryParam: {
        beginDate: '',
        endDate: '',
        bizNo: '',
        dispatchBillNo: '',
        settleClientName: '',
        receiverName: '',
        subareaSn: undefined,
        billStatus: undefined,
        voidFlag: undefined,
        dealerProvinceSn: undefined,
        printState: 'NO_PRINT'
      },
      totalData: {
        totalAmount: 0,
        totalQty: 0,
        totalRecord: 0
      },
      showTipModal: false,
      showRecordModal: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return settleReceiptList(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 arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '9%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售单号', scopedSlots: { customRender: 'bizNo' }, width: '9%', align: 'center' },
        { title: '备货单号', scopedSlots: { customRender: 'dispatchBillNo' }, width: '9%', align: 'center' },
        { title: '发货编号', dataIndex: 'sendNo', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '客户名称', dataIndex: 'settleClientName', align: 'left', width: '10%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '收货客户名称', dataIndex: 'receiverName', align: 'left', width: '10%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品款数', dataIndex: 'totalCategory', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品数量', dataIndex: 'qty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '总售价', dataIndex: 'totalAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '业务状态', dataIndex: 'billStatusDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单据状态', dataIndex: 'voidFlagDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收款打印状态', dataIndex: 'printStateDictValue', width: '6%', align: 'center' },
        { title: '打印次数', dataIndex: 'printCount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '7%', align: 'center' }
      ]

      if (this.$hasPermissions('B_isShowPrice')) { //  售价权限
        arr.splice(9, 0, { title: '总售价', dataIndex: 'totalAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
      }
      return arr
    }
  },
  methods: {
    changeTab (v) {
      this.currentTab = v
      this.resetSearchForm()
    },
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    custChange (val) {
      this.queryParam.settleClientName = val.name
    },
    receiverChange (val) {
      this.queryParam.receiverName = val.name
    },
    // 销售单详情
    handleDetail (row) {
      this.$router.push({ name: 'salesDetail', params: { sn: row.bizSn, type: 'salesCollection' } })
    },
    // 备货单号
    handleBhDetail (row) {
      this.$router.push({ name: 'pushOrderDetail', params: { sn: row.dispatchBillSn, type: 'salesCollection' } })
    },
    // 收款打印
    handlePrint (row) {
      this.showTipModal = true
      this.$nextTick(() => {
        this.$refs.printModel.getData(row)
      })
    },
    printOk () {
      this.$refs.table.refresh()
    },
    // 打印记录
    viewPrint (row) {
      this.showRecordModal = true
      this.$refs.recordModal.setData(row, {
        billType: 'SETTLE_RECEIPT',
        billSn: row.accountReceiptSn
      })
    },
    // 重置
    resetSearchForm () {
      this.time = []
      if (this.advanced) {
        this.$refs.rangeDate.resetDate(this.time)
        this.$refs.receiverName.resetForm()
      }
      this.$refs.settleClientName.resetForm()
      this.queryParam = {
        beginDate: '',
        endDate: '',
        bizNo: '',
        dispatchBillNo: '',
        settleClientName: '',
        receiverName: '',
        subareaSn: undefined,
        billStatus: undefined,
        voidFlag: undefined,
        dealerProvinceSn: undefined,
        printState: this.currentTab == 2 ? 'NO_PRINT' : 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 - 228
    }
  },
  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">
  .receiptPrint-wrap{
    .sTable{
      margin-top: 10px;
    }
    .ant-tabs-bar{
      margin:0;
    }
  }
</style>
