<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="matchSendOutOrderList-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="创建时间">
                <rangeDate ref="rangeDate" id="matchSendOutOrderList-time" :value="time" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="调往对象">
                <a-input id="matchSendOutOrderList-targetName" v-model.trim="queryParam.targetName" allowClear placeholder="请输入调往对象"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="调拨单号">
                <a-input id="matchSendOutOrderList-allocateNo" v-model.trim="queryParam.allocateNo" allowClear placeholder="请输入调拨单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" v-show="isShowWarehouse">
              <a-form-item label="调出仓库">
                <warehouse
                  v-model="queryParam.warehouseSn"
                  isPermission
                  id="matchSendOutOrderList-warehouseSn"
                  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.state"
                    ref="state"
                    id="matchSendOutOrderList-state"
                    code="ALLOCATE_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.checkStatus"
                    ref="checkStatus"
                    id="matchSendOutOrderList-checkStatus"
                    code="CHECK_STATUS"
                    placeholder="请选择对单状态"
                    allowClear
                  ></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="收货客户名称">
                  <dealerSearchList id="matchSendOutOrderList-receiverSn" ref="receiverSn" @change="custChange" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24" v-show="isShowCustomerSearch">
                <a-form-item label="客服">
                  <customerService ref="customerName" id="matchSendOutOrderList-bizUserSn" v-model="queryParam.bizUserSn"></customerService>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="isShowCustomerSearch?24:6" :sm="24" :style="{textAlign: isShowCustomerSearch?'center':''}">
              <div class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="matchSendOutOrderList-refresh">查询</a-button>
                <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="matchSendOutOrderList-reset">重置</a-button>
                <a @click="advanced=!advanced" style="margin-left: 5px" id="matchSendOutOrderList-advanced">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </div>
            </a-col>
          </a-row>
        </a-form>
      </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"
          :pageSize="30"
          :defaultLoadData="false"
          :scroll="{ y: tableHeight }"
          bordered>
          <!-- 单号 -->
          <template slot="allocateNo" slot-scope="text, record">
            <span v-if="$hasPermissions('M_transferOut_detail')" :id="'matchSendOutOrderList-allocateNo'+record.id" class="link-bule" @click="handleDetail(record)">{{ record.allocateNo }}</span>
            <span v-else>{{ record.allocateNo }}</span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="record.checkStatus == 'WAIT'&&$hasPermissions('B_YxAudit')"
              class="button-warning"
              @click="handleYxAudit(record)"
              :id="'matchSendOutOrderList-checkStatus'+record.id">对单通过</a-button>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-spin>
      <!-- 查看详情 -->
      <commonModal
        modalTit="调拨单详情"
        bodyPadding="10px"
        width="70%"
        :showFooter="false"
        :openModal="showDetailModal"
        @cancel="cancelDetail">
        <allocationDetailModal v-if="showDetailModal" :outBizSn="bizSn" />
      </commonModal>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
import getDate from '@/libs/getDate.js'
// 组件
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import commonModal from '@/views/common/commonModal.vue'
import dealerSearchList from '@/views/common/dealerSearchList.vue'
import customerService from '@/views/common/customerService.vue'
import allocationDetailModal from '@/views/allocationManagement/transferOut/detail.vue'
import warehouse from '@/views/common/chooseWarehouse.js'
// 接口
import { allocateBillList, allocateBillCheck } from '@/api/allocateBill'
export default {
  name: 'MatchSendOutOrderAllocationList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, dealerSearchList, commonModal, allocationDetailModal, warehouse, customerService },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      tableHeight: 0, // 表格高度
      // 创建时间 默认值
      time: [
        moment(getDate.getThreeMonthDays().starttime, 'YYYY-MM-DD'),
        moment(getDate.getCurrMonthDays().endtime, 'YYYY-MM-DD')
      ],
      //  查询条件
      queryParam: {
        beginDate: getDate.getThreeMonthDays().starttime, // 开始时间
        endDate: getDate.getCurrMonthDays().endtime, // 结束时间
        targetName: '', //  调往对象
        state: undefined, //  业务状态
        allocateNo: '', //  调拨单号
        warehouseSn: undefined, // 出库仓库
        receiverSn: undefined, // 收货客户名称
        checkStatus: undefined, // 对单状态
        printState: undefined, // 打印状态
        bizUserSn: undefined// 客服
      },
      showDetailModal: false, // 详情弹窗
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出按钮加载状态
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        // 获取列表数据  有分页
        return allocateBillList(Object.assign(parameter, this.queryParam, { authWarehouseFlag: 1 })).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
        })
      },
      bizSn: '', // 调拨sn
      spinningAudit: false, // 审核按钮加载状态
      openModal: false //  新增编辑  弹框

    }
  },
  computed: {
    // 表头
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '7%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调拨单号', scopedSlots: { customRender: 'allocateNo' }, align: 'center', width: '9%' },
        { title: '调往对象', dataIndex: 'targetName', width: '9%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '发货编号', dataIndex: 'sendNo', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '发货说明', dataIndex: 'explainInfo', width: '9%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '收货客户名称', dataIndex: 'receiverName', width: '9%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '总数量', dataIndex: 'totalQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '调出仓库', dataIndex: 'warehouseName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务状态', dataIndex: 'stateDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '对单状态', dataIndex: 'checkStatusDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '对单人员', dataIndex: 'checkPersonName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '对单时间', dataIndex: 'checkDate', width: '7%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '7%', align: 'center' }
      ]
      if (this.isShowWarehouse) {
        arr.splice(4, 0, { title: '调出仓库', dataIndex: 'warehouseName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } })
      }
      return arr
    }
  },
  methods: {
    // 收货客户名称 change
    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
      }
    },
    //  创建时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate(this.time)
      this.queryParam.beginDate = getDate.getThreeMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam.targetName = ''
      this.queryParam.state = undefined
      this.queryParam.allocateNo = ''
      this.queryParam.receiverSn = ''
      this.queryParam.receiverName = ''
      this.queryParam.warehouseSn = undefined
      this.queryParam.checkStatus = undefined
      this.queryParam.bizUserSn = undefined
      this.$refs.table.refresh(true)
      this.$refs.receiverSn.resetForm()
    },
    // 允许打印
    handleYxAudit (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认对单通过吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          allocateBillCheck({ sn: row.allocateSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  详情
    handleDetail (row) {
      this.$store.state.app.curActionPermission = 'M_transferOut_detail'
      this.bizSn = row.allocateSn
      this.showDetailModal = true
    },
    // 关闭详情弹窗
    cancelDetail () {
      this.$store.state.app.curActionPermission = ''
      this.showDetailModal = false
      this.bizSn = null
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.openModal = false
    },
    // 表格高度
    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()
      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">
	.active{
		color: #ed1c24;
		cursor: pointer;
	}
	.common{
		color: rgba(0, 0, 0);
	}
</style>
