<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="pickUp-pickUpNo" v-model.trim="queryParam.pickUpNo" allowClear placeholder="请输入提货单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="关联销退单号">
                <a-input id="pickUp-salesReturnBillNo" v-model.trim="queryParam.salesReturnBillNo" 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.state"
                    ref="state"
                    id="pickUp-state"
                    code="PICK_UP_STATE"
                    :notIn="['WAIT_SUBMIT','WAIT_AUDIT','AUDIT_REJECT']"
                    placeholder="请选择状态"
                  ></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="申请人">
                  <employee style="width: 100%;" id="pickUp-Employee" v-model="queryParam.applyPersonSn"></employee>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="物流点">
                  <logisticsPoint style="width: 100%;" id="pickUp-logisticsPoint" v-model="queryParam.logisticsPoint"></logisticsPoint>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="发货方">
                  <custList placeholder="请输入客户名称搜索" ref="dealerSubareaScopeList" id="pickUp-senderSn" @change="custChange" />
                </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>
            </template>
            <a-col :md="advanced ? (isShowWarehouse ? 24 : 6) : 6" :sm="24" :style="{textAlign:advanced ? (isShowWarehouse?'center':'left') : 'left'}">
              <div 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>
              </div>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false" class="pickUp-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"
          :pageSize="30"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 单号 -->
          <template slot="pickUpNo" slot-scope="text, record">
            <div v-if="$hasPermissions('M_detailPickUp')">
              <span class="link-bule" @click="handleDetail(record)">{{ record.pickUpNo }}</span>
            </div>
            <div v-else>{{ record.pickUpNo }}</div>
          </template>
          <!-- 关联单数 -->
          <template slot="salesReturnNum" slot-scope="text, record">
            <div v-if="record.salesReturnNum">{{ record.salesReturnNum }} 单</div>
            <div v-else></div>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <div>
              <!-- <a-button
             size="small"
             type="link"
             class="button-info"
             v-if="(record.state == 'AUDIT_PASS'||record.state == 'FINISH')&&$hasPermissions('M_detailPickUp')"
             @click="handleDetail(record)"
             id="pickUp-detail-btn">详情</a-button> -->
              <a-button
                size="small"
                type="link"
                class="button-info"
                v-if="record.state == 'AUDIT_PASS'&&$hasPermissions('B_pickUp')"
                @click="handlePickUp(record)"
                id="pickUp-detail-btn">提货</a-button>
            </div>
          </template>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import custList from '@/views/common/custList.vue'
import employee from '../../expenseManagement/expenseReimbursement/employee.js'
import logisticsPoint from '../billOfLading/logisticsPoint'
import rangeDate from '@/views/common/rangeDate.vue'
import chooseWarehouse from '@/views/common/chooseWarehouse'
import { warehouseQueryPage, pickUpFinish } from '@/api/pickUp'
export default {
  name: 'PickUpList',
  mixins: [commonMixin],
  components: {
    STable,
    VSelect,
    custList,
    employee,
    rangeDate,
    logisticsPoint,
    chooseWarehouse
  },
  data () {
    return {
      spinning: false,
      // 高级搜索 展开/关闭
      advanced: true,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      // 查询参数
      queryParam: {
        auditBeginDate: '',
        auditEndDate: '',
        pickUpNo: '', // 提货单号
        applyPersonSn: undefined, // 申请人
        salesReturnBillNo: undefined, //  总部销退单号
        logisticsPoint: undefined, // 物流点
        senderSn: undefined, //  发货人
        state: 'AUDIT_PASS', // 业务状态
        stateList: ['FINISH', 'AUDIT_PASS'],
        warehouseSn: undefined

      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return warehouseQueryPage(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
        })
      },
      visibleAudit: false,
      auditInfo: null,
      spinningAudit: false
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '编号', dataIndex: 'no', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核时间', dataIndex: 'auditDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '提货单号', scopedSlots: { customRender: 'pickUpNo' }, width: '10%', align: 'center' },
        { title: '关联销退单', dataIndex: 'salesReturnNum', scopedSlots: { customRender: 'salesReturnNum' }, width: '6%', align: 'center' },
        { title: '申请人', dataIndex: 'applyPersonName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '主题', dataIndex: 'theme', width: '10%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '物流公司', dataIndex: 'logisticsCompany', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '物流点', dataIndex: 'logisticsPoint', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '发货方', dataIndex: 'dealerEntity.dealerName', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收货人', dataIndex: 'receiverName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'stateDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        // { title: '退货仓库', dataIndex: 'warehouseName', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '6%', align: 'center' }
      ]
      if (this.isShowWarehouse) {
        arr.splice(11, 0, { title: '退货仓库', dataIndex: 'warehouseName', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true })
      }
      return arr
    }
  },
  methods: {
    //  审核时间  change
    dateExamineChange (date) {
      this.queryParam.auditBeginDate = date[0] ? date[0] : ''
      this.queryParam.auditEndDate = date[1] ? date[1] : ''
    },
    custChange (val) {
      this.queryParam.senderSn = val.key
    },
    // 详情
    handleDetail (row) {
      this.$router.push({ name: 'billOfLadingDetail', params: { sn: row.pickUpSn } })
    },
    // 提货
    handlePickUp (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认已经提货吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          pickUpFinish({ pickUpSn: row.pickUpSn }).then(res => {
            _this.spinning = false
            if (res.status == 200) {
              _this.$message.info(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    resetSearchForm () {
      if (this.advanced) {
        this.$refs.rangeExamineDate.resetDate()
        this.$refs.dealerSubareaScopeList.resetForm()
      }
      this.queryParam.auditBeginDate = ''
      this.queryParam.auditEndDate = ''
      this.queryParam.pickUpNo = ''
      this.queryParam.senderSn = undefined
      this.queryParam.applyPersonSn = undefined
      this.queryParam.logisticsPoint = undefined
      this.queryParam.salesReturnBillNo = ''
      this.queryParam.state = undefined
      this.queryParam.warehouseSn = undefined
      this.queryParam.stateList = ['FINISH', 'AUDIT_PASS']
      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
    }
  },
  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>
