<template>
  <div class="stockPrint-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="5" :sm="24">
                <a-form-item label="客户名称" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                  <dealerSubareaScopeList ref="dealerSubareaScopeList" id="stockPrint-buyerName" @change="custChange" />
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="销售单号">
                  <a-input id="stockPrint-salesBillNo" v-model.trim="queryParam.salesBillNo" allowClear placeholder="请输入销售单号"/>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="备货单号">
                  <a-input id="stockPrint-dispatchBillNo" v-model.trim="queryParam.dispatchBillNo" allowClear placeholder="请输入备货单号"/>
                </a-form-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="5" :sm="24">
                  <a-form-item label="收货客户名称" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                    <dealerSubareaScopeList ref="shbuyerName" id="stockPrint-shbuyerName" @change="shcustChange" />
                  </a-form-item>
                </a-col>
                <a-col :md="3" :sm="24">
                  <a-form-item label="业务状态">
                    <v-select
                      v-model="queryParam.billStatus"
                      ref="billStatus"
                      id="stockPrint-billStatus"
                      code="DISPATCH_BILL_STATUS"
                      placeholder="请选择业务状态"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="3" :sm="24" v-if="currentTab == 1">
                  <a-form-item label="单据状态">
                    <v-select
                      v-model="queryParam.voidFlag"
                      ref="voidFlag"
                      id="pushOrder-voidFlag"
                      code="VOID_FLAG"
                      placeholder="请选择单据状态"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="4" :sm="24" v-if="currentTab == 1">
                  <a-form-item label="备货打印状态">
                    <v-select
                      v-model="queryParam.printStatus"
                      ref="printStatus"
                      id="pushOrder-printStatus"
                      code="PRINT_STATUS"
                      placeholder="请选择备货打印状态"
                      allowClear></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="3" :sm="24">
                  <a-form-model-item label="所在区域">
                    <subarea id="stockPrint-subarea" v-model="queryParam.subareaSn"></subarea>
                  </a-form-model-item>
                </a-col>
                <a-col :md="3" :sm="24">
                  <a-form-model-item label="地区">
                    <a-form-model-item prop="shippingAddrProvinceSn">
                      <Area id="stockPrint-shippingAddrProvinceSn" v-model="queryParam.shippingAddrProvinceSn" placeholder="请选择省"></Area>
                    </a-form-model-item>
                  </a-form-model-item>
                </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+84.5+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y:tableHeight, x: 1600 }"
          :defaultLoadData="false"
          bordered>
          <!-- 销售单号 -->
          <template slot="salesBillNo" slot-scope="text, record">
            <span class="link-bule" v-if="$hasPermissions('B_salesDetail')" @click="handleDetail(record,0)">{{ record.salesBillNo }}</span>
            <span v-else>{{ record.salesBillNo }}</span>
          </template>
          <!-- 备货单号 -->
          <template slot="dispatchBillNo" slot-scope="text, record">
            <span class="link-bule" v-if="$hasPermissions('B_dispatchDetail')" @click="handleDetail(record,1)">{{ record.dispatchBillNo }}</span>
            <span v-else>{{ record.dispatchBillNo }}</span>
          </template>
          <div slot="costTitle">
            <a-tooltip placement="top">
              <template slot="title">
                如果收货客户名称不为空，则按照收货客户的价格级别计算得到。
              </template>
              <span style="margin-right: 5px;">实际总售价</span> <a-icon type="question-circle" />
            </a-tooltip>
          </div>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="record.printStatus == 'NO_PRINT'&&record.voidFlag==0&&$hasPermissions('B_sendTypePrint')"
              class="button-warning"
              @click="handlePrint(record)"
            >发货分类打印</a-button>
            <a-button
              size="small"
              type="link"
              class="button-warning"
              v-else-if="record.printStatus == 'PRINT'&&$hasPermissions('B_stockPrintRecord')"
              @click="viewPrint(record)"
            >打印记录</a-button>
            <span v-else>--</span>
          </template>
        </s-table>
        <!-- 分类打印 -->
        <sendTypeModal ref="typePrint" :openModal="showTipModal" @ok="handlePrintOk" @close="showTipModal=false" />
        <!-- 打印记录 -->
        <recordModal ref="recordModal" modalTit="打印记录" :openModal="showRecordModal" @cancel="showRecordModal=false"></recordModal>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
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 sendTypeModal from '@/views/salesManagement/pushOrderManagement/sendTypeModal.vue'
import recordModal from './recordModal.vue'
import { dispatchlList, dispatchDetailPrint } from '@/api/dispatch'
import { printFun } from '@/libs/JGPrint.js'
export default {
  name: 'PushOrderManagementList',
  mixins: [commonMixin],
  components: { STable, VSelect, dealerSubareaScopeList, rangeDate, subarea, sendTypeModal, recordModal, Area },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      time: [],
      currentTab: 2,
      // 查询参数
      queryParam: {
        beginDate: '',
        endDate: '',
        buyerSn: undefined, //  客户名称
        receiverSn: undefined, // 收货客户名称
        salesBillNo: '', //  销售单号
        dispatchBillNo: '', //  备货单号
        billStatus: undefined, //  业务状态
        subareaSn: undefined,
        shippingAddrProvinceSn: undefined,
        voidFlag: undefined,
        printStatus: 'NO_PRINT'
      },
      showTipModal: false,
      showRecordModal: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return dispatchlList(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: '60px', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '120px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售单号', scopedSlots: { customRender: 'salesBillNo' }, width: '120px', align: 'center' },
        { title: '备货单号', scopedSlots: { customRender: 'dispatchBillNo' }, width: '120px', align: 'center' },
        { title: '发货编号', dataIndex: 'sendNo', width: '70px', align: 'center' },
        { title: '客户名称', dataIndex: 'buyerName', width: '150px', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '收货客户名称', dataIndex: 'receiverName', width: '150px', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品款数', dataIndex: 'totalCategory', width: '70px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '产品数量', dataIndex: 'totalQty', width: '70px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { slots: { title: 'costTitle' }, dataIndex: 'totalAmount', width: '80px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '易损件售价', dataIndex: 'receiveYsjTotalAmount', width: '70px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '电池售价', dataIndex: 'receiveDcTotalAmount', width: '70px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '机油售价', dataIndex: 'receiveJyTotalAmount', width: '70px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '业务状态', dataIndex: 'billStatusDictValue', width: '70px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单据状态', dataIndex: 'voidFlagDictValue', width: '70px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备货打印状态', dataIndex: 'printStatusDictValue', width: '100px', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '打印次数', dataIndex: 'stockUpPrintTimes', width: '70px', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '100px', align: 'center', fixed: 'right' }
      ]
      if (this.$hasPermissions('B_isShowPrice')) { //  售价权限
        arr.splice(9, 0, { slots: { title: 'costTitle' }, dataIndex: 'totalAmount', width: '80px', 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.buyerSn = val.key
    },
    shcustChange (val) {
      this.queryParam.receiverSn = val.key
    },
    // 详情
    handleDetail (row, type) {
      if (type == 0) {
        this.$router.push({ name: 'salesDetail', params: { sn: row.salesBillSn, type: 'pushOrder' } })
      } else {
        this.$router.push({ name: 'pushOrderDetail', params: { sn: row.dispatchBillSn, type: 'pushOrder' } })
      }
    },
    // 发货分类打印
    handlePrint (row) {
      this.$refs.typePrint.setData(row, row.dispatchBillSn)
      this.showTipModal = true
    },
    // 发货分类打印
    handlePrintOk (obj, taskName, type) {
      const _this = this
      const params = {
        type: 'DISPATCH_BILL_TYPE',
        dispatchBillSn: obj.dispatchBillSn
      }
      _this.spinning = true
      printFun(dispatchDetailPrint, Object.assign(params, obj || {}), type, taskName, () => { _this.spinning = false })
    },
    // 打印记录
    viewPrint (row) {
      this.showRecordModal = true
      this.$refs.recordModal.setData(row, {
        billType: 'SETTLE_RECEIPT',
        billSn: row.accountReceiptSn
      })
    },
    // 重置
    resetSearchForm () {
      this.time = []
      this.$refs.rangeDate.resetDate(this.time)
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.buyerSn = undefined
      this.queryParam.receiverSn = undefined
      this.$refs.dealerSubareaScopeList.resetForm()
      this.$refs.shbuyerName.resetForm()
      this.queryParam.salesBillNo = ''
      this.queryParam.dispatchBillNo = ''
      this.queryParam.billStatus = undefined
      this.queryParam.subareaSn = undefined
      this.queryParam.shippingAddrProvinceSn = undefined
      this.queryParam.voidFlag = undefined
      this.queryParam.printStatus = this.currentTab == 1 ? '' : 'NO_PRINT'
      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 - 250
    }
  },
  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">
  .stockPrint-wrap{
    .sTable{
      margin-top: 10px;
    }
    .ant-tabs-bar{
      margin:0;
    }
  }
</style>
