<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper" >
        <a-form layout="inline" id="transferReturnConfirmation-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="创建时间">
                <rangeDate id="transferReturnConfirmation-rangeDate" ref="rangeDate" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="调拨退货单号">
                <a-input
                  id="transferReturnConfirmation-allocateReturnNo"
                  v-model.trim="queryParam.allocateReturnNo"
                  allowClear
                  placeholder="请输入调拨退货单号" />
              </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="transferReturnConfirmation-targetName"
                  v-model.trim="queryParam.targetName"
                  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.grabFlag"
                    ref="grabFlag"
                    id="transferReturnConfirmation-grabFlag"
                    code="FLAG"
                    placeholder="请选择是否抓单"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="仓库">
                  <chooseWarehouse ref="warehouse" id="transferReturnConfirmation-warehouse" v-model="queryParam.warehouseSn"></chooseWarehouse>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="状态">
                  <v-select
                    v-model="queryParam.state"
                    ref="state"
                    id="transferReturnConfirmation-state"
                    code="ALLOCATE_RETURN_STATE_CONFIRM"
                    placeholder="请选择状态"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button
                type="primary"
                @click="$refs.table.refresh(true)"
                :disabled="disabled"
                id="transferReturnConfirmation-refresh">查询</a-button>
              <a-button
                style="margin-left: 5px"
                @click="resetSearchForm"
                :disabled="disabled"
                id="transferReturnConfirmation-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px" id="transferReturnConfirmation-advanced">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'" />
              </a>
            </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"
          :pageSize="30"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 销退单号 -->
          <template slot="allocateReturnNo" slot-scope="text, record">
            <span
              v-if="$hasPermissions('M_allocateReturnConfirmationDetail')"
              class="link-bule"
              :id="'transferReturnConfirmation-allocateReturnNo-'+record.id"
              @click="handleDetail(record)">{{ record.allocateReturnNo }}</span>
            <span v-else>{{ record.allocateReturnNo }}</span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="record.state=='WAIT_FINANCIAL_AUDIT' && $hasPermissions('B_allocateReturnConfirmationAudit')"
              class="button-warning"
              @click="handleAudit(record)"
              :id="'transferReturnConfirmation-submit-'+record.id">退货确认</a-button>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-spin>
      <!-- 查看 -->
      <AllocateReturnDetailModal v-drag :openModal="openModal" :itemSn="itemSn" @close="closeModal" />
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import rangeDate from '@/views/common/rangeDate.vue'
import { STable, VSelect } from '@/components'
import AllocateReturnDetailModal from './detailModal.vue'
import chooseWarehouse from '@/views/common/chooseWarehouse'
// 接口
import { allocateReturnQueryConfirmPage, allocateReturnFinanceAudit } from '@/api/allocateReturn'
export default {
  name: 'AllocateReturnConfirmationList',
  mixins: [commonMixin],
  components: { STable, VSelect, AllocateReturnDetailModal, rangeDate, chooseWarehouse },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      //  查询条件
      queryParam: {
        beginDate: '', // 创建开始时间
        endDate: '', // 创建结束时间
        allocateReturnNo: '', //  单号
        targetName: '', // 调拨退货客户
        grabFlag: undefined, // 是否抓单
        state: undefined, //  状态
        warehouseSn: undefined// 出库仓库
      },
      disabled: false, //  查询、重置按钮是否可操作
      advanced: true, // 展开关闭
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        // 获取列表数据  有分页
        return allocateReturnQueryConfirmPage(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
        })
      },
      openModal: false, // 打开详情弹窗
      itemSn: ''// 当前行sn
    }
  },
  // 根据权限显示列表字段
  computed: {
    // 表头
    columns () {
      const arr = [{
        title: '序号',
        dataIndex: 'no',
        width: '4%',
        align: 'center'
      },
      {
        title: '创建时间',
        dataIndex: 'createDate',
        width: '11%',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '退货单号',
        scopedSlots: {
          customRender: 'allocateReturnNo'
        },
        width: '16%',
        align: 'center'
      },
      {
        title: '调拨退货客户',
        dataIndex: 'targetName',
        align: 'center',
        width: '15%',
        customRender: function (text) {
          return text || '--'
        },
        ellipsis: true
      },
      {
        title: '仓库',
        dataIndex: 'warehouseName',
        align: 'center',
        width: '15%',
        customRender: function (text) {
          return text || '--'
        },
        ellipsis: true
      },
      {
        title: '是否抓单',
        dataIndex: 'grabFlagDictValue',
        width: '7%',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '退货总数量',
        dataIndex: 'totalQty',
        width: '7%',
        align: 'center',
        customRender: function (text) {
          return ((text || text == 0) ? text : '--')
        }
      },
      {
        title: '坏件数量',
        dataIndex: 'totalBadQty',
        width: '7%',
        align: 'center',
        customRender: function (text) {
          return ((text || text == 0) ? text : '--')
        }
      },
      {
        title: '返库数量',
        dataIndex: 'totalBackStockQty',
        width: '7%',
        align: 'center',
        customRender: function (text) {
          return ((text || text == 0) ? text : '--')
        }
      },
      // { title: '退货总金额', dataIndex: 'totalPrice', width: '7%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
      {
        title: '确认时间',
        dataIndex: 'financeAuditTime',
        width: '11%',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '状态',
        dataIndex: 'stateDictValue',
        width: '6%',
        align: 'center',
        customRender: function (text) {
          return text || '--'
        }
      },
      {
        title: '操作',
        scopedSlots: {
          customRender: 'action'
        },
        width: '8%',
        align: 'center'
      }
      ]
      if (this.$hasPermissions('M_allocateReturnConfirmationList_salesPrice')) { //  售价权限
        arr.splice(9, 0, {
          title: '退货总金额',
          dataIndex: 'totalPrice',
          width: '7%',
          align: 'right',
          customRender: text => ((text || text == 0) ? this.toThousands(text) : '--')
        })
      }
      return arr
    }
  },
  methods: {
    //  创建时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 退货客户
    custChange (val) {
      this.queryParam.buyerSn = val.key
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.allocateReturnNo = ''
      this.queryParam.targetName = ''
      this.queryParam.grabFlag = undefined
      this.queryParam.state = undefined
      this.queryParam.warehouseSn = undefined
      this.$refs.table.refresh(true)
    },
    // 详情
    handleDetail (row) {
      this.itemSn = row.allocateReturnSn
      this.openModal = true
    },
    // 关闭详情弹窗
    closeModal () {
      this.itemSn = ''
      this.openModal = false
    },
    // 退货确认
    handleAudit (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '请点击下方按钮确认操作',
        centered: true,
        closable: true,
        okText: '通过',
        cancelText: '不通过',
        onOk () {
          _this.auditOrder(row.allocateReturnSn, 'FINISH')
        },
        onCancel (e) {
          if (!e.triggerCancel) {
            _this.auditOrder(row.allocateReturnSn, 'FINANCIAL_REJECT')
          }
          _this.$destroyAll()
        }
      })
    },
    // 退货确认成功
    auditOrder (sn, flag) {
      const _this = this
      _this.spinning = true
      allocateReturnFinanceAudit({
        allocateReturnSn: sn,
        state: flag
      }).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$refs.table.refresh()
          _this.spinning = false
        } else {
          _this.spinning = false
        }
      })
    },
    // 初始化
    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>

<style lang="less">
  .transferReturnConfirmation-wrap {

  }
</style>
