<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <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="客户名称" :label-col="{ span:7 }" :wrapper-col="{ span:17}">
                <a-input id="sendOut-customeName" v-model.trim="queryParam.customeName" allowClear placeholder="请输入客户名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="发货单号">
                <a-input id="sendOut-sendBillNo" v-model.trim="queryParam.sendBillNo" allowClear placeholder="请输入发货单号"/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="收货状态">
                  <v-select
                    v-model="queryParam.receiveFlag"
                    ref="receiveFlag"
                    id="sendOut-receiveFlag"
                    code="RECEIVE_FLAG"
                    placeholder="请选择收货状态"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="托运日期">
                  <rangeDate ref="sendDate" :value="sendtime" @change="sendDateChange" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="物流情况">
                  <v-select
                    v-model="queryParam.transportState"
                    ref="transportState"
                    id="sendOut-transportState"
                    code="TRANSPORT_STATE"
                    placeholder="请选择物流情况"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="关联业务单号">
                  <a-input id="sendOut-bizNos" v-model.trim="queryParam.bizNos" allowClear placeholder="请输入备货或调拨单号"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所在区域">
                  <subarea id="sendOut-subarea" ref="subarea" @change="subareaChange"></subarea>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="地区" >
                  <Area id="sendOut-provinceSn" v-model="queryParam.provinceSn" placeholder="请选择省"></Area>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="仓库">
                  <chooseWarehouse ref="warehouse" @load="warehouseLoad" v-model="queryParam.warehouseSn" :isPermission="true"></chooseWarehouse>
                </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>
            </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-button
                  style="margin-left: 10px"
                  type="primary"
                  class="button-warning"
                  @click="handleExport"
                  :disabled="disabled"
                  :loading="exportLoading"
                  v-if="$hasPermissions('B_sendOutExport')"
                  id="sendOut-export">导出</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>
    </a-card>
    <a-card size="small" :bordered="false" class="sendOut-wrap">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 操作按钮 -->
        <div v-if="isShowImport">
          <a-button type="primary" class="button-error" v-if="$hasPermissions('B_sendOutPlImport')" @click="openGuideModal=true">批量导入</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+70+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y:tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 发货单号 -->
          <template slot="sendBillNo" slot-scope="text, record">
            <span class="link-bule" v-if="$hasPermissions('B_SooDetail')" @click="handleDetail(record,1)">{{ record.sendBillNo }}</span>
            <span v-else>{{ record.sendBillNo }}</span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              class="button-warning"
              v-if="record.receiveFlag == 0&&$hasPermissions('B_SooEdit')"
              @click="handleEdit(record)"
            >编辑</a-button>
            <a-button
              size="small"
              type="link"
              class="button-warning"
              v-if="record.receiveFlag == 0&&$hasPermissions('B_WLGZ')"
              @click="handleDetail(record,0)"
            >物流跟踪</a-button>
            <span v-if="record.receiveFlag == 1">--</span>
          </template>
        </s-table>
      </a-spin>
      <!-- 发货单详情 -->
      <detailModal v-drag ref="detailModal" :openModal="showTipModal" @cancel="showTipModal=false" @ok="sendSuccess"></detailModal>
      <!-- 编辑发货单 -->
      <sendGoodModal
        v-drag
        ref="sendGoodModal"
        modalTit="编辑发货单"
        :openModal="showSendGoodModal"
        @ok="sendSuccess"
        @cancel="showSendGoodModal=false"></sendGoodModal>
      <!-- 导入发货单 -->
      <importGuideModal v-drag :openModal="openGuideModal" @close="openGuideModal=false" @ok="resetSearchForm" />
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import rangeDate from '@/views/common/rangeDate.vue'
import { hdExportExcel } from '@/libs/exportExcel'
import { STable, VSelect } from '@/components'
import detailModal from './detailModal.vue'
import sendGoodModal from '../outboundOrder/sendGoodModal.vue'
import Area from '@/views/common/area.js'
import subarea from '@/views/common/subarea.js'
import ImportGuideModal from './importGuideModal.vue'
import chooseWarehouse from '@/views/common/chooseWarehouse'
import customerService from '@/views/common/customerService'
import { sendBillQueryPageList, sendBillExcel } from '@/api/sendBill'
export default {
  name: 'SendOutOrderList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, detailModal, sendGoodModal, Area, subarea, ImportGuideModal, chooseWarehouse, customerService },
  data () {
    return {
      spinning: false,
      exportLoading: false,
      advanced: true, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      openGuideModal: false,
      tableHeight: 0,
      time: [],
      sendtime: [],
      // 查询参数
      queryParam: {
        beginDate: '',
        endDate: '',
        sendBeginDate: '',
        sendEndDate: '',
        customeName: undefined, //  客户名称
        sendBillNo: '', //  发货单号
        bizNos: '', //  业务单号
        transportState: undefined, //  物流状态
        receiveFlag: undefined, // 收货状态
        subareaArea: {
          subareaSn: undefined,
          subareaAreaSn: undefined
        },
        provinceSn: undefined,
        warehouseSn: undefined,
        bizUserSn: undefined
      },
      totalData: {
        totalAmount: 0,
        totalQty: 0,
        totalRecord: 0
      },
      showTipModal: false,
      showSendGoodModal: false,
      isShowImport: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return sendBillQueryPageList(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: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '发货单号', scopedSlots: { customRender: 'sendBillNo' }, width: '12%', align: 'center' },
        { title: '区域', dataIndex: 'subareaArea.subareaName', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'subareaArea.subareaAreaName', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '地区', dataIndex: 'provinceName', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'customeName', width: '12%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '仓库', dataIndex: 'warehouseName', width: '10%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '托运日期', dataIndex: 'sendDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收货人', dataIndex: 'customerCacateName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '物流电话', dataIndex: 'transportTele', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '物流单号', dataIndex: 'transportNo', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '总件数', dataIndex: 'totalQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '运费合计', dataIndex: 'totalSendAmount', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '收货状态', dataIndex: 'receiveFlagDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收款时间', dataIndex: 'settleTime', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收货时间', dataIndex: 'receiveDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '物流情况', dataIndex: 'transportStateDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'transportRemark', width: '8%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '8%', align: 'center' }
      ]
      if (this.isShowWarehouse) {
        arr.splice(7, 0, { title: '仓库', dataIndex: 'warehouseName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } })
      }
      return arr
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    sendDateChange (date) {
      this.queryParam.sendBeginDate = date[0]
      this.queryParam.sendEndDate = date[1]
    },
    warehouseLoad (def, data) {
      this.isShowImport = data.length == 1
    },
    // 详情
    handleDetail (row, type) {
      this.showTipModal = true
      this.$refs.detailModal.getDetail(row, type, type ? '发货单详情' : '物流跟踪')
    },
    // 编辑
    handleEdit (row) {
      this.showSendGoodModal = true
      this.$refs.sendGoodModal.getDetail(row)
    },
    sendSuccess () {
      this.showSendGoodModal = false
      this.showTipModal = false
      this.$refs.table.refresh()
    },
    //  导出
    handleExport () {
      const _this = this
      _this.exportLoading = true
      _this.spinning = true
      hdExportExcel(sendBillExcel, _this.queryParam, '发货单列表', function () {
        _this.exportLoading = false
        _this.spinning = false
      })
    },
    subareaChange (val) {
      this.queryParam.subareaArea.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaArea.subareaAreaSn = val[1] ? val[1] : undefined
    },
    // 重置
    resetSearchForm () {
      this.time = []
      this.$refs.rangeDate.resetDate(this.time)
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.sendtime = []
      this.$refs.sendDate.resetDate(this.sendtime)
      this.queryParam.sendBeginDate = ''
      this.queryParam.sendEndDate = ''
      this.queryParam.customeName = undefined
      this.queryParam.sendBillNo = ''
      this.queryParam.bizNos = ''
      this.queryParam.transportState = undefined
      this.queryParam.receiveFlag = undefined
      this.queryParam.subareaArea.subareaSn = undefined
      this.queryParam.subareaArea.subareaAreaSn = undefined
      this.queryParam.provinceSn = undefined
      this.queryParam.warehouseSn = undefined
      this.queryParam.bizUserSn = undefined
      if (this.advanced) {
        this.$refs.subarea.clearData()
      }
      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 - 210 - (this.isShowImport ? 30 : 0)
    }
  },
  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">
  .sendOut-wrap{
    .sTable{
      margin-top: 10px;
    }
  }
</style>
