<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="审核时间">
                <rangeDate ref="rangeExamineDate" @change="dateExamineChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="总部销退单号">
                <a-input id="receiveCheck-salesReturnBillNo" v-model.trim="queryParam.salesReturnBillNo" allowClear placeholder="请输入总部销退单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="客户名称">
                <dealerSubareaScopeList ref="dealerSubareaScopeList" id="receiveCheck-buyerSn" @change="custChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="退货类别">
                <v-select
                  v-model="queryParam.goodFlag"
                  ref="goodFlag"
                  id="receiveCheck-goodFlag"
                  code="GOOD_FLAG"
                  placeholder="请选择退货类别"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="业务状态">
                <v-select
                  v-model="queryParam.billStatus"
                  ref="billStatus"
                  id="receiveCheck-billStatus"
                  code="SALES_RETURN_BILL_STATUS"
                  :notIn="['WAIT_SUBMIT', 'WAIT_AUDIT', 'AUDIT_REJECT']"
                  placeholder="请选择业务状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" v-show="isShowWarehouse">
              <a-form-model-item label="退货仓库">
                <chooseWarehouse ref="warehouse" 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>
            <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>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false" class="receiveCheck-wrap">
      <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"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 单号 -->
          <template slot="salesReturnBillNo" slot-scope="text, record">
            <div v-if="$hasPermissions('B_salesReturnDetail')">
              <span class="link-bule" @click="handleDetail(record)">{{ record.salesReturnBillNo }}</span>
            </div>
            <div v-else>{{ record.salesReturnBillNo }}</div>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <div>
              <a-button
                size="small"
                type="link"
                class="button-info"
                v-if="record.billStatus == 'WAIT_RECEIVE'&&$hasPermissions('B_salesReturnReceiving')"
                @click="handleReceiving(record)"
                id="receiveCheck-detail-btn">收货</a-button>
              <a-button
                size="small"
                type="link"
                class="button-warning"
                v-if="record.billStatus == 'WAIT_CHECK'&&$hasPermissions('B_salesReturnCheck')"
                @click="handleCheck(record)"
                id="salesReturn-eexamine-btn">品检</a-button>
              <a-button
                size="small"
                type="link"
                class="button-warning"
                @click="handleOpenPrintModal(record)"
                id="salesReturn-print-btn">打印</a-button>
            </div>
          </template>
        </s-table>
      </a-spin>
      <commonModal
        modalTit="品检打印预览"
        bodyPadding="10px"
        width="700pt"
        :showFooter="false"
        :openModal="showTipModal"
        @cancel="canselPrintView">
        <printModal ref="printModel" @cancel="showTipModal=false"></printModal>
      </commonModal>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import employee from '../../expenseManagement/expenseReimbursement/employee.js'
import rangeDate from '@/views/common/rangeDate.vue'
import { takeQueryPage } from '@/api/salesReturn'
import printModal from './printModal.vue'
import chooseWarehouse from '@/views/common/chooseWarehouse'
import customerService from '@/views/common/customerService'
import commonModal from '@/views/common/commonModal.vue'
export default {
  name: 'ReceiveCheckList',
  mixins: [commonMixin],
  components: {
    STable,
    VSelect,
    dealerSubareaScopeList,
    employee,
    rangeDate,
    printModal,
    commonModal,
    chooseWarehouse,
    customerService
  },
  data () {
    return {
      spinning: false,
      // 高级搜索 展开/关闭
      advanced: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      createDate: [], //  创建时间
      // 查询参数
      queryParam: {
        beginAuditDate: '',
        endAuditDate: '',
        buyerSn: undefined, //  客户名称
        salesReturnBillNo: undefined, //  总部销退单号
        billStatus: undefined, // 业务状态
        warehouseSn: undefined,
        goodFlag: undefined,
        bizUserSn: undefined
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return takeQueryPage(Object.assign(parameter, this.queryParam, {
          'excludeBillStatusList': ['WAIT_SUBMIT', 'WAIT_AUDIT', 'AUDIT_REJECT']
        })).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
        })
      },
      visibleAudit: false,
      auditInfo: null,
      spinningAudit: false,
      showTipModal: false
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '编号', dataIndex: 'no', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核时间', dataIndex: 'auditTime', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '总部销退单号', scopedSlots: { customRender: 'salesReturnBillNo' }, width: '8%', align: 'center' },
        { title: '客户名称', dataIndex: 'buyerName', width: '12%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '退货类别', dataIndex: 'goodFlagDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '申请退货数量', dataIndex: 'totalInitialQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '仓库实收数量', dataIndex: 'totalReceiveQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '坏件数量', dataIndex: 'totalBadQty', width: '5%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '良品数量', dataIndex: 'totalGoodQty', width: '5%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '返库数量', dataIndex: 'totalBackStockQty', width: '5%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '业务状态', dataIndex: 'billStatusDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '6%', align: 'center' }
      ]
      if (this.isShowWarehouse) {
        arr.splice(5, 0, { title: '退货仓库', dataIndex: 'warehouseName', width: '6%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true })
      }
      return arr
    }
  },
  methods: {
    //  审核时间  change
    dateExamineChange (date) {
      this.queryParam.beginAuditDate = date[0]
      this.queryParam.endAuditDate = date[1]
    },
    custChange (val) {
      this.queryParam.buyerSn = val.key
    },
    // 详情
    handleDetail (row) {
      this.$router.push({ name: 'salesReturnDetail', params: { sn: row.salesReturnBillSn }, query: { path: 'receiveCheck' } })
    },
    // 收货
    handleReceiving (row) {
      this.$router.push({ name: 'salesReturnReceiving', params: { sn: row.salesReturnBillSn, buyerSn: row.buyerSn } })
    },
    // 品检
    handleCheck (row) {
      this.$router.push({ name: 'salesReturnCheck', params: { sn: row.salesReturnBillSn, buyerSn: row.buyerSn } })
    },
    resetSearchForm () {
      this.$refs.rangeExamineDate.resetDate()
      this.$refs.dealerSubareaScopeList.resetForm()
      this.queryParam.beginAuditDate = ''
      this.queryParam.endAuditDate = ''
      this.queryParam.buyerSn = undefined
      this.queryParam.salesReturnBillNo = ''
      this.queryParam.billStatus = undefined
      this.queryParam.warehouseSn = undefined
      this.queryParam.goodFlag = undefined
      this.queryParam.bizUserSn = 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 - 200
    },
    canselPrintView () {
      this.showTipModal = false
      this.$refs.printModel.handleCommonCancel()
    },
    handleOpenPrintModal (row) {
      this.showTipModal = true
      this.$nextTick(() => {
        this.$refs.printModel.getData(row)
      })
    }
  },
  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>
