<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form-model
          id="allocationList-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 id="allocationList-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="调往对象">
                <a-input
                  id="allocationList-targetName"
                  v-model.trim="queryParam.targetName"
                  allowClear
                  placeholder="请输入调往对象" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="调拨单号">
                <a-input
                  id="allocationList-allocateNo"
                  v-model.trim="queryParam.allocateNo"
                  allowClear
                  placeholder="请输入调拨单号" />
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="费用/调拨类型">
                  <AllocateType
                    id="allocationList-allocateTypeVal"
                    v-model="allocateTypeVal"
                    :changeOnSelect="true"
                    placeholder="请选择费用/调拨类型"
                    @change="changeAllocatype"></AllocateType>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="业务状态">
                  <v-select
                    v-model="queryParam.state"
                    ref="state"
                    id="allocationList-state"
                    code="ALLOCATE_STATUS2"
                    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.checkStatus"
                    ref="checkStatus"
                    id="allocationList-checkStatus"
                    code="CHECK_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.printState"
                    ref="printState"
                    id="allocationList-printState"
                    code="PRINT_STATUS"
                    placeholder="请选择打印状态"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="收货客户名称">
                  <dealerSearchList id="allocationList-receiverSn" ref="receiverSn" @change="custChange" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="调出仓库">
                  <warehouse v-model="queryParam.warehouseSn" id="allocationList-warehouseSn" placeholder="请选择调出仓库" />
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24">
              <a-button
                type="primary"
                @click="handleSearch"
                :disabled="disabled"
                id="allocationList-refresh">查询</a-button>
              <a-button
                style="margin-left: 5px"
                @click="resetSearchForm"
                :disabled="disabled"
                id="allocationList-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px" id="allocationList-advanced">
                {{ 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+70+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y:tableHeight, x:1890 }"
          :defaultLoadData="false"
          bordered>
          <!-- 起止时间 -->
          <template slot="promoDate" slot-scope="text, record">
            <span v-if="record.promoStartDate || record.promoEndDate">{{ record.promoStartDate }} ~
              {{ record.promoEndDate }}</span>
            <span v-else>--</span>
          </template>
          <!-- 调拨单号 -->
          <template slot="allocateNo" slot-scope="text, record">
            <span class="link-bule" :id="'allocationList-allocateNo-'+record.id" @click="handleDetail(record,2)">{{ record.allocateNo }}</span>
          </template>
          <!-- 发货单号  -->
          <template slot="sendBillNo" slot-scope="text, record">
            <span
              v-if="record.sendBill&&record.sendBill.sendBillNo"
              class="link-bule"
              :id="'allocationList-sendBillNo-'+record.id"
              @click="handleDetail(record,1)">{{ record.sendBill.sendBillNo }}</span>
            <span v-else>--</span>
          </template>
          <!-- 调拨类型 -->
          <template slot="allocateType" slot-scope="text, record">
            <div>
              <span v-if="record.allocateSortName">{{ record.allocateSortName }}</span>
              <span v-if="record.allocateSortName&&record.allocateTypeName">/</span>
              <span v-if="record.allocateTypeName">{{ record.allocateTypeName }}</span>
            </div>
          </template>
        </s-table>
        <!-- 查看详情 -->
        <commonModal
          modalTit="调拨单详情"
          bodyPadding="10px"
          width="70%"
          :showFooter="false"
          :openModal="showDetailModal"
          @cancel="showDetailModal = false">
          <allocationDetailModal v-if="showDetailModal" :outBizSn="bizSn" />
        </commonModal>
        <!-- 发货单详情 -->
        <detailModal v-drag ref="detailModal" :openModal="showTipModal" @cancel="showTipModal=false"></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 commonModal from '@/views/common/commonModal.vue'
import detailModal from '@/views/salesManagement/sendOutOrder/detailModal.vue'
import AllocateType from '@/views/common/allocateType.js'
import dealerSearchList from '@/views/common/dealerSearchList.vue'
import warehouse from '@/views/common/chooseWarehouse.js'
import allocationDetailModal from '@/views/allocationManagement/transferOut/detail.vue'
// 接口
import { queryOverviewPage } from '@/api/allocateBill'
export default {
  name: 'AllocationList',
  mixins: [commonMixin],
  components: { STable, VSelect, allocationDetailModal, AllocateType, warehouse, dealerSearchList, rangeDate, commonModal, detailModal },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      showDetailModal: false, // 调拨单详情弹窗
      showTipModal: false, // 发货单详情弹窗
      bizSn: null, // 调拨单sn
      tableHeight: 0, // 表格高度
      allocateTypeVal: [], // 调拨类型

      // 查询参数
      queryParam: {
        // 创建时间默认值
        time: [
          moment(getDate.getThreeMonthDays().starttime, 'YYYY-MM-DD'),
          moment(getDate.getCurrMonthDays().endtime, 'YYYY-MM-DD')
        ],
        beginDate: getDate.getThreeMonthDays().starttime, // 创建开始时间
        endDate: getDate.getCurrMonthDays().endtime, // 创建结束时间
        targetName: '', //  调往对象
        costTypeSn: undefined, // 费用类型
        allocateSortSn: undefined, //  调拨类型1
        allocateTypeSn: undefined, //  调拨类型2
        state: undefined, //  业务状态
        allocateNo: '', //  调拨单号
        receiverSn: undefined, // 收货客户sn
        checkStatus: undefined, // 对单状态
        printState: undefined, // 打印状态
        warehouseSn: undefined// 出库仓库sn
      },
      // 表单验证规则
      rules: {
        'time': [{ required: true, message: '请选择退货完成时间', trigger: 'change' }]
      },
      // 加载数据方法 必须为 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
        })
      }
    }
  },
  computed: {
    // 表头
    columns () {
      const _this = this
      const arr = [{
        title: '创建时间',
        dataIndex: 'createDate',
        width: '130px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        },
        fixed: 'left'
      },
      {
        title: '起止时间',
        scopedSlots: {
          customRender: 'promoDate'
        },
        align: 'center',
        width: '150px',
        fixed: 'left'
      },
      {
        title: '调往对象',
        dataIndex: 'targetName',
        width: '150px',
        align: 'left',
        customRender: function (text) {
          return text || '--'
        },
        ellipsis: true,
        fixed: 'left'
      },
      {
        title: '省份',
        dataIndex: 'provinceName',
        align: 'center',
        width: '80px',
        customRender: function (text) {
          return text || '--'
        },
        fixed: 'left'
      },
      {
        title: '调拨单号',
        scopedSlots: {
          customRender: 'allocateNo'
        },
        align: 'center',
        width: '120px',
        fixed: 'left'
      },
      {
        title: '发货单号',
        scopedSlots: {
          customRender: 'sendBillNo'
        },
        width: '120px',
        align: 'center'
      },
      {
        title: '客户类型',
        dataIndex: 'dealerLevelDictValue',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '收货客户名称',
        dataIndex: 'receiverName',
        width: '150px',
        align: 'left',
        customRender: function (text) {
          return text || '--'
        },
        ellipsis: true
      },
      {
        title: '所在区域',
        dataIndex: 'subareaArea.subareaName',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '所在分区',
        dataIndex: 'subareaArea.subareaAreaName',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '总成本',
        dataIndex: 'totalCost',
        width: '80px',
        align: 'right',
        customRender: text => ((text || text == 0) ? _this.toThousands(text) : '--')
      },
      {
        title: '总售价',
        dataIndex: 'totalPrice',
        width: '80px',
        align: 'right',
        customRender: text => ((text || text == 0) ? _this.toThousands(text) : '--')
      },
      {
        title: '总数量',
        dataIndex: 'totalQty',
        width: '80px',
        align: 'center',
        customRender: function (text) {
          return ((text || text == 0) ? text : '--')
        }
      },
      {
        title: '费用类型',
        dataIndex: 'costTypeName',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '调拨类型',
        scopedSlots: {
          customRender: 'allocateType'
        },
        width: '100px',
        align: 'center'
      },
      {
        title: '调出仓库',
        dataIndex: 'warehouseName',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '审核时间',
        dataIndex: 'auditTime',
        width: '130px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '业务状态',
        dataIndex: 'stateDictValue',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '发货创建时间',
        dataIndex: 'sendBill.createDate',
        width: '130px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },

      {
        title: '发货状态',
        dataIndex: 'sendBill.sendFlagDictValue',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '发货编号',
        dataIndex: 'sendNo',
        width: '80px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '托运日期',
        dataIndex: 'sendBill.sendDate',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '收货人',
        dataIndex: 'sendBill.customerCacateName',
        width: '80px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        },
        ellipsis: true
      },
      {
        title: '物流电话',
        dataIndex: 'sendBill.transportTele',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '物流单号',
        dataIndex: 'sendBill.transportNo',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },

      {
        title: '总件数',
        dataIndex: 'sendBill.totalQty',
        width: '80px',
        align: 'center',
        customRender: function (text) {
          return ((text || text == 0) ? text : '--')
        }
      },
      {
        title: '收货状态',
        dataIndex: 'sendBill.receiveFlagDictValue',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '收货时间',
        dataIndex: 'sendBill.receiveDate',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '物流情况',
        dataIndex: 'sendBill.transportStateDictValue',
        width: '100px',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '备注',
        dataIndex: 'sendBill.transportRemark',
        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
        }
      })
    },
    // 调拨详情
    handleDetail (row, typeNum) {
      if (typeNum == 2) {
        this.bizSn = row.allocateSn
        this.showDetailModal = true
      } else {
        this.showTipModal = true
        this.$refs.detailModal.getDetail(row.sendBill, typeNum, '发货单详情')
      }
    },
    //  时间  change
    dateChange (date) {
      if (date[0] && date[1]) {
        this.queryParam.time = date
      } else {
        this.queryParam.time = []
      }
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    // 调拨类型
    changeAllocatype (val, opts) {
      this.allocateTypeVal = val || []
      this.queryParam.costTypeSn = val && val[0] ? val[0] : ''
      this.queryParam.allocateSortSn = val && val[1] ? val[1] : ''
      this.queryParam.allocateTypeSn = val && val[2] ? val[2] : ''
    },
    // 收货客户名称
    custChange (val) {
      if (val.row) {
        this.queryParam.receiverSn = val.row.dealerSn
        this.queryParam.receiverName = undefined
      } else {
        this.queryParam.receiverSn = undefined
        this.queryParam.receiverName = val.key
      }
    },
    // 重置
    resetSearchForm () {
      this.queryParam.beginDate = getDate.getThreeMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam.time = [
        getDate.getThreeMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ]
      this.$refs.rangeDate.resetDate(this.queryParam.time)
      this.queryParam.targetName = ''
      this.queryParam.costTypeSn = undefined
      this.queryParam.allocateSortSn = undefined
      this.queryParam.allocateTypeSn = undefined
      this.queryParam.state = undefined
      this.queryParam.checkStatus = undefined
      this.queryParam.printState = undefined
      this.queryParam.allocateNo = ''
      this.queryParam.receiverName = ''
      this.queryParam.receiverSn = undefined
      this.queryParam.warehouseSn = undefined
      this.allocateTypeVal = []
      this.$refs.receiverSn.resetForm()
      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 - 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()
    }
  },
  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">
  .pushOrder-wrap {
    .sTable {
      margin-top: 10px;
    }
  }
</style>
