<template>
  <div class="jg-page-wrap customerReportList-wrap">
    <a-card size="small" :bordered="false" class="table-page-search-wrapper">
      <!-- 搜索条件 -->
      <a-form-model
        id="salesOutofStockReport-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 ref="rangeDate" @change="dateChange" />
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-model-item label="客户名称">
              <custList id="salesOutofStockReport-buyerName" ref="custSatelliteList" @change="custSatelliteChange"></custList>
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
            <a-button type="primary" @click="handleSearch" :disabled="disabled" id="salesOutofStockReport-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="salesOutofStockReport-reset">重置</a-button>
            <a-button
              style="margin-left: 5px"
              type="primary"
              class="button-warning"
              @click="handleExport"
              :disabled="disabled"
              :loading="exportLoading"
              v-if="$hasPermissions('B_isSalesOosExport')"
              id="salesOutofStockReport-export">导出</a-button>
          </a-col>
        </a-row>
      </a-form-model>

      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :defaultLoadData="false"
        bordered>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            class="button-success"
            v-if="$hasPermissions('B_isSalesOosDetail')"
            @click="handleDetail(record)"
          >详情</a-button>
          <span v-else>--</span>
        </template>
      </s-table>
      </a-spin>
    </a-card>
    <!-- 详情 -->
    <detail-modal :openModal="openDetailModal" ref="detailModal" @close="openDetailModal = false" />
  </div>
</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 detailModal from './detailModal.vue'
import { salesOosQueryList, salesOosExport } from '@/api/reportData'
export default {
  components: { STable, VSelect, rangeDate, custList, detailModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      queryParam: { //  查询条件
        time: [],
        beginDate: '',
        endDate: '',
        targetName: undefined, // 客户名称
        targetSn: undefined // 客户sn
      },
      openDetailModal: false, // 详情弹窗
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        const params = Object.assign(parameter, this.queryParam)
        this.spinning = true
        delete params.time
        return salesOosQueryList(params).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
        })
      },
      rules: {
        'time': [{ required: true, message: '请选择审核时间', trigger: 'change' }]
      }
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售单号', dataIndex: 'salesBillNo', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'targetName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '缺货款数', dataIndex: 'totalCategory', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '缺货数量', dataIndex: 'totalQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '缺货金额', dataIndex: 'totalAmount', width: '7%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '8%', align: 'center' }
      ]
      return arr
    }
  },
  methods: {
    //  查询
    handleSearch () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$refs.table.refresh(true)
        } else {
          this.$message.info('请选择创建时间')
          return false
        }
      })
    },
    // 查看详情
    handleDetail (row) {
      this.openDetailModal = true
      const _this = this
      this.$nextTick(() => {
        _this.$refs.detailModal.getDetail(row)
      })
    },
    //  创建时间  change
    dateChange (date) {
      this.queryParam.time = date
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    custSatelliteChange (v, row) {
      if (row && row.customerSn) {
        this.queryParam.targetSn = row.dealerSn
        this.queryParam.targetName = row.customerName
      } else {
        this.queryParam.targetName = v
        this.queryParam.targetSn = undefined
      }
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.time = []
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.targetName = undefined
      this.queryParam.targetSn = undefined
      this.$refs.ruleForm.resetFields()
      this.$refs.custSatelliteList.resetForm()
      this.$refs.table.clearTable()
    },
    // 导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.queryParam
          _this.exportLoading = true
          _this.spinning = true
          salesOosExport(params).then(res => {
            downloadExcel(res, '销售缺货报表')
            _this.exportLoading = false
            _this.spinning = false
          })
        } else {
          this.$message.info('请选择创建时间')
          return false
        }
      })
    },
    beforeRouteEnter (to, from, next) {
      next(vm => {})
    }
  }
}
</script>
