<template>
  <div>
    <a-card size="small" :bordered="false" class="warehousingOrderDetailList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper" ref="tableSearch">
        <a-form-model
          id="warehousingOrderDetailList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :model="queryParam"
          :rules="rules"
          @keyup.enter.native="handleSearch">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="完成日期" prop="time">
                <rangeDate ref="rangeDate" id="warehousingOrderDetailList-time" :value="queryParam.time" @change="dateChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="产品编码">
                <a-input id="warehousingOrderDetailList-productCode" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="入库单号">
                <a-input id="warehousingOrderDetailList-stockPutNo" v-model.trim="queryParam.stockPutNo" allowClear placeholder="请输入入库单号"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="单位名称">
                <a-input id="warehousingOrderDetailList-supplierName" v-model.trim="queryParam.supplierName" allowClear placeholder="请输入单位名称"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="产品名称">
                  <a-input id="warehousingOrderDetailList-productName" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="入库类型">
                  <v-select
                    v-model="queryParam.putBizType"
                    ref="putBizType"
                    id="warehousingOrderDetailList-putBizType"
                    code="PUT_STOCK_TYPE_REPORT"
                    placeholder="请选择入库类型"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="散件入库子类型">
                  <v-select
                    v-model="queryParam.sparePartsType"
                    ref="sparePartsType"
                    id="warehousingOrderDetailList-sparePartsType"
                    code="SPARE_PARTS_TYPE"
                    placeholder="请选择散件入库子类型"
                    allowClear></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="入库仓库">
                  <chooseWarehouse ref="warehouse" id="warehousingOrderDetailList-warehouseSn" isPermission v-model="queryParam.warehouseSn"></chooseWarehouse>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="24" :sm="24" style="margin-bottom: 10px;text-align:center;">
              <a-button style="margin-left: 5px" type="primary" @click="handleSearch" :disabled="disabled" id="warehousingOrderDetailList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="warehousingOrderDetailList-reset">重置</a-button>
              <a-button
                style="margin-left: 5px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_warehousingOrderDetailExport')"
                id="warehousingOrderDetailList-export">导出</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <!-- 列表 -->
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <s-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          :rowKey="(record) => record.no"
          rowKeyName="no"
          :columns="columns"
          :data="loadData"
          :pageSize="30"
          :defaultLoadData="false"
          :style="{ height: tableHeight+70+'px' }"
          :scroll="{ x:1890,y: tableHeight-34}"
          bordered>
          <template slot="footer">
            <a-row :gutter="15">
              <a-col :md="4" :sm="24">
                总数量：{{ (totalData && (totalData.totalPutQty || totalData.totalPutQty==0)) ? totalData.totalPutQty : '--' }}
              </a-col>
              <a-col :md="4" :sm="24" v-if="$hasPermissions('M_warehousingOrderDetailList_costPrice')">
                总金额：{{ (totalData && (totalData.totalPutAmount || totalData.totalPutAmount==0)) ? totalData.totalPutAmount : '--' }}
              </a-col>
            </a-row>
          </template>
        </s-table>
      </a-spin>
      <!-- 导出提示框 -->
      <reportModal :visible="showExport" @close="showExport=false"></reportModal>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { hdExportExcel } from '@/libs/exportExcel'
// 组件
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import reportModal from '@/views/common/reportModal.vue'
import chooseWarehouse from '@/views/common/chooseWarehouse'
// 接口
import { reportStockPutDetailList, reportStockPutDetailCount, reportStockPutDetailExport } from '@/api/reportData'
export default {
  name: 'WarehousingOrderDetailList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, reportModal, chooseWarehouse },
  data () {
    return {
      spinning: false,
      advanced: false, // 高级搜索 展开/关闭
      tableHeight: 0, // 表格高度
      showExport: false, // 导出弹窗
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出按钮加载状态
      //  查询条件
      queryParam: {
        time: [], // 完成日期
        beginDate: '', // 开始日期
        endDate: '', // 结束日期
        stockPutNo: '', // 入库单号
        putBizType: undefined, // 入库类型
        supplierName: '', // 单位名称
        productName: '', // 产品名称
        productCode: '', // 产品编码
        warehouseSn: undefined// 入库仓库
      },
      rules: {
        'time': [{ required: true, message: '请选择完成日期', trigger: 'change' }]
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        delete params.time
        // 获取列表数据 有分页
        return reportStockPutDetailList(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
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '入库单号', dataIndex: 'stockPutNo', width: '9%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库仓库', dataIndex: 'warehouseName', width: '8%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '入库开单日期', dataIndex: 'openBillTime', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位名称', dataIndex: 'supplierName', width: '10%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品名称', dataIndex: 'productName', width: '10%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品编码', dataIndex: 'productCode', width: '8%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'productUnit', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库数量', dataIndex: 'putQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '进价', dataIndex: 'putCost', width: '5%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '入库金额', dataIndex: 'putAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库类型', dataIndex: 'putBizTypeDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '散件入库子类型', dataIndex: 'sparePartsTypeDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '完成日期', dataIndex: 'auditTime', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      if (this.$hasPermissions('M_warehousingOrderDetailList_costPrice')) { //  成本价权限
        arr.splice(8, 0, { title: '进价', dataIndex: 'putCost', width: '5%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.splice(9, 0, { title: '入库金额', dataIndex: 'putAmount', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      return arr
    }
  },
  watch: {
    // 展开关闭 重新计算表格高度
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  methods: {
    // 总计
    getCount (params) {
      reportStockPutDetailCount(params).then(res => {
        if (res.status == 200 && res.data) {
          this.totalData = res.data
        } else {
          this.totalData = null
        }
      })
    },
    // 查询
    handleSearch () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.$refs.table.refresh(true)
        } else {
          _this.$message.error('请选择完成日期')
          return false
        }
      })
    },
    //  完成日期  change
    dateChange (date) {
      if (date[0] && date[1]) {
        this.queryParam.time = date
      } else {
        this.queryParam.time = []
      }
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    //  重置
    resetSearchForm () {
      this.queryParam.time = []
      this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.stockPutNo = ''
      this.queryParam.putBizType = undefined
      this.queryParam.supplierName = ''
      this.queryParam.productName = ''
      this.queryParam.productCode = ''
      this.queryParam.warehouseSn = undefined
      this.totalData = null
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearTable()
    },
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = this.queryParam
          _this.$store.state.app.curActionPermission = 'B_warehousingOrderDetailExport'
          _this.showExport = true
          _this.exportLoading = true
          _this.spinning = true
          hdExportExcel(reportStockPutDetailExport, params, '入库单明细报表', function () {
            _this.exportLoading = false
            _this.spinning = false
            _this.$store.state.app.curActionPermission = ''
          })
        } else {
          return false
        }
      })
    },
    // 初始化
    pageInit () {
      this.resetSearchForm()
      this.setTableH()
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 200
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
