<template>
  <a-spin :spinning="spinning" tip="Loading...">
    <!-- 搜索条件 -->
    <div class="table-page-search-wrapper">
      <a-form-model
        id="storeTransOutReport-form"
        ref="ruleForm"
        class="form-model-con"
        layout="inline"
        :model="queryParam"
        :rules="rules"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        @keyup.enter.native="handleSearch" >
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-model-item label="审核时间" prop="time">
              <rangeDate id="storeTransOutReport-time" ref="rangeDate" @change="dateChange" />
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-model-item label="店内调出单号">
              <a-input id="storeTransOutReport-storeCallOutNo" v-model.trim="queryParam.storeCallOutNo" allowClear placeholder="请输入店内调出单号"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="调往对象">
              <custList id="storeTransOutReport-putPersonName" ref="custList" @change="custChange"></custList>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="调拨类型">
              <a-select id="sstoreTransOutReport-callOutType" v-model="queryParam.callOutType" placeholder="请选择调拨类型" allowClear >
                <a-select-option v-for="item in storeCallOutTypeList" :id="item.storeCallOutTypeSn" :key="item.storeCallOutTypeSn" :value="item.storeCallOutTypeSn">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="出库时间">
              <rangeDate id="storeTransOutReport-outWareTime" ref="outWareRangeDate" :value="outWareTime" @change="outWareDateChange" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
            <a-button type="primary" @click="handleSearch" :disabled="disabled" id="storeTransOutReport-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="storeTransOutReport-reset">重置</a-button>
            <a-button
              type="primary"
              style="margin-left: 5px"
              @click="handleExport"
              :disabled="disabled"
              :loading="exportLoading"
              v-if="$hasPermissions('M_storeTransferOutReportExport')"
              class="button-warning"
              id="storeTransOutReport-export-btn">导出</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <!-- 合计 -->
    <a-alert type="info" style="margin-bottom:10px">
      <div class="ftext" slot="message">
        总单数：<strong>{{ (totalData && (totalData.productTotalCategory || totalData.productTotalCategory==0)) ? totalData.productTotalCategory : '--' }}</strong>；
        产品总数量：<strong>{{ (totalData && (totalData.productTotalQty || totalData.productTotalQty==0)) ? totalData.productTotalQty : '--' }}</strong>；
        <div v-if="$hasPermissions('M_ShowAllCost')" style="display: inline-block;">
          调出总成本：<strong>{{ (totalData && (totalData.productTotalCost || totalData.productTotalCost==0)) ? toThousands(totalData.productTotalCost) : '--' }}</strong>；
        </div>
      </div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :defaultLoadData="false"
      bordered>
    </s-table>
  </a-spin>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { downloadExcel } from '@/libs/JGPrint.js'
import custList from '@/views/common/custList.vue'
import { storeCallOutTypeAllList } from '@/api/storeCallOutType'
import { reportStoreCallOutPageList, reportStoreCallOutListTotal, reportStoreCallOutExport } from '@/api/reportData'
export default {
  components: { STable, VSelect, rangeDate, custList },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      advanced: false, // 高级搜索 展开/关闭
      tableHeight: 0,
      outWareTime: [], // 出库时间
      queryParam: { //  查询条件
        time: [],
        beginDate: '', // 开始时间
        endDate: '', // 结束时间
        customerNameCurrent: undefined, //  调往对象
        customerSnCurrent: undefined, // 调往对象sn
        callOutType: undefined, // 调拨类型
        storeCallOutNo: ''// 调拨单号
      },
      rules: {
        'time': [{ required: true, message: '请选择审核时间', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        const params = Object.assign(parameter, this.queryParam)
        this.spinning = true
        delete params.time
        return reportStoreCallOutPageList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            // 总计
            this.getCount(params)
            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
        })
      },
      totalData: null, //  合计
      storeCallOutTypeList: [] //  调拨类型
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '店内调出单号', dataIndex: 'storeCallOutNo', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调往对象', width: '15%', align: 'center', customRender: function (text, record) { return record.putPersonName || '--' }, ellipsis: true },
        { title: '调拨类型', dataIndex: 'callOutTypeName', width: '7%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '款数', dataIndex: 'productTotalCategory', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '数量', dataIndex: 'productTotalQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '成本', dataIndex: 'productTotalCost', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '审核时间', dataIndex: 'auditTime', width: '13%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      if (this.$hasPermissions('M_ShowAllCost')) {
        arr.splice(6, 0, { title: '成本', dataIndex: 'productTotalCost', width: '7%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      return arr
    }
  },
  methods: {
    // 选择调往对象
    custChange (v, row) {
      if (row && row.customerSn) {
        this.queryParam.customerSnCurrent = row.customerSn
        this.queryParam.customerNameCurrent = undefined
      } else {
        this.queryParam.customerNameCurrent = v
        this.queryParam.customerSnCurrent = undefined
      }
    },
    // 合计
    getCount (params) {
      reportStoreCallOutListTotal(params).then(res => {
        if (res.status == 200) {
          this.totalData = res.data
        } else {
          this.totalData = null
        }
      })
    },
    //  创建时间  change
    dateChange (date) {
      this.queryParam.time = date
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    // 出库时间
    outWareDateChange (date) {
      this.queryParam.outWarehouseBeginDate = date[0]
      this.queryParam.outWarehouseEndDate = date[1]
    },
    //  查询
    handleSearch () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$refs.table.refresh(true)
        } else {
          return false
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.time = []
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.$refs.outWareRangeDate.resetDate('')
      this.outWareTime = []
      this.queryParam.outWarehouseBeginDate = ''
      this.queryParam.outWarehouseEndDate = ''

      this.$refs.custList.resetForm()
      this.queryParam.customerSnCurrent = undefined
      this.queryParam.customerNameCurrent = undefined
      this.queryParam.storeCallOutNo = ''
      this.queryParam.callOutType = undefined
      this.$refs.ruleForm.resetFields()
      this.totalData = null
      this.$refs.table.clearTable()
    },
    // 导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.queryParam
          params.showCost = this.$hasPermissions('M_ShowAllCost') ? '1' : '0'
          _this.exportLoading = true
          _this.spinning = true
          reportStoreCallOutExport(params).then(res => {
            downloadExcel(res, '店内调出报表')
            _this.exportLoading = false
            _this.spinning = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  调拨类型
    getStoreCallOutTypeAllList () {
      storeCallOutTypeAllList().then(res => {
        if (res.status == 200) {
          this.storeCallOutTypeList = res.data
        } else {
          this.storeCallOutTypeList = []
        }
      })
    },
    // 初始化
    pageInit () {
      this.getStoreCallOutTypeAllList()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
