<template>
  <a-card size="small" :bordered="false" class="allocationOrderTotalList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form-model
          id="allocationOrderTotalList-form"
          ref="ruleForm"
          :model="queryParam"
          :rules="rules"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          @keyup.enter.native="handleSearch" >
          <a-row :gutter="15">
            <a-col :md="7" :sm="24">
              <a-form-model-item label="调拨开单日期" prop="time">
                <rangeDate ref="rangeDate" :value="queryParam.time" @change="dateChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-model-item label="调拨单号">
                <a-input id="allocationOrderTotalList-allocateNo" v-model.trim="queryParam.allocateNo" allowClear placeholder="请输入调拨单号"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-model-item label="操作员">
                <a-select id="allocationOrderTotalList-creatorId" v-model="queryParam.creatorId" placeholder="请选择操作员" allowClear >
                  <a-select-option v-for="item in operatorList" :key="item.sn" :value="item.sn">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="调拨类别">
                  <v-select code="ALLOCATE_CATEGORY" id="allocationOrderTotalList-allocateCategory" v-model="queryParam.allocateCategory" allowClear placeholder="请选择调拨类别"></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="调拨类型名称">
                  <a-select id="allocationOrderTotalList-allocateTypeSn" v-model="queryParam.allocateTypeSn" placeholder="请选择调拨类型名称" allowClear >
                    <a-select-option v-for="item in allocateTypeList" :key="item.allocateTypeSn" :value="item.allocateTypeSn">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="7" :sm="24" style="margin-top: 3px;">
              <a-button
                style="margin-bottom: 18px;"
                type="primary"
                class="button-warning"
                size="small"
                @click="handleStock"
                id="allocationOrderTotalList-stockDate">盘点区间日期</a-button>
              <a-button style="margin: 0 0 18px 8px" type="primary" @click="handleSearch" :disabled="disabled" id="allocationOrderTotalList-refresh">查询</a-button>
              <a-button style="margin: 0 0 18px 8px" @click="resetSearchForm" :disabled="disabled" id="allocationOrderTotalList-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :defaultLoadData="false"
        :scroll="{ x: 1140 }"
        bordered>
        <template slot="footer" slot-scope="currentPageData">
          <a-row :gutter="15">
            <a-col :md="4" :sm="24">
              总数量：{{ (totalData && (totalData.totalQty || totalData.totalQty==0)) ? totalData.totalQty : '--' }}
            </a-col>
            <a-col :md="4" :sm="24">
              总成本价：{{ (totalData && (totalData.totalCost || totalData.totalCost==0)) ? totalData.totalCost : '--' }}
            </a-col>
            <a-col :md="4" :sm="24">
              总毛利：{{ (totalData && (totalData.totalGrossProfit || totalData.totalGrossProfit==0)) ? totalData.totalGrossProfit : '--' }}
            </a-col>
          </a-row>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { allocateTypeAllList } from '@/api/allocateType'
import { userQueryList } from '@/api/power-user'
import { allocateReportList, allocateReportCount } from '@/api/allocateReport'
export default {
  components: { STable, VSelect, rangeDate },
  data () {
    return {
      spinning: false,
      advanced: false, // 高级搜索 展开/关闭
      queryParam: { //  查询条件
        time: [],
        beginDate: '',
        endDate: '',
        allocateNo: '',
        creatorId: undefined,
        allocateCategory: undefined,
        allocateTypeSn: undefined
      },
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      rules: {
        'time': [{ required: true, message: '请选择调拨开单日期', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '调拨单号', dataIndex: 'allocateNo', width: 200, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调拨开单日期', dataIndex: 'allocateDate', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'targetName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '调拨数量', dataIndex: 'totalQty', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '成本价', dataIndex: 'totalCost', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '毛利', dataIndex: 'grossProfit', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '操作员', dataIndex: 'creatorName', width: 120, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        const params = Object.assign(parameter, this.queryParam)
        delete params.time
        this.spinning = true
        return allocateReportList(params).then(res => {
          // 总计
          this.getCount(params)
          const 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
        })
      },
      allocateTypeList: [], //  调拨类型
      operatorList: [], //  操作员下拉数据
      totalData: null //  合计
    }
  },
  methods: {
    // 盘点库存日期
    handleStock () {
      this.$message.info('无盘点区间的起始/终止时间，请自行选择日期区间查询！')
    },
    //  创建时间  change
    dateChange (date) {
      if (date.length) {
        if (date[0] == '' && date[1] == '') {
          this.resetSearchForm()
        } else {
          this.queryParam.time = date
        }
      }
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 合计
    getCount (params) {
      allocateReportCount(params).then(res => {
        if (res.status == 200) {
          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 {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.time = []
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.allocateNo = ''
      this.queryParam.creatorId = undefined
      this.queryParam.allocateCategory = undefined
      this.queryParam.allocateTypeSn = undefined
      this.$refs.ruleForm.resetFields()
      this.totalData = null
      this.$refs.table.clearTable()
    },
    //  调拨类型
    getAllocateTypeAllList () {
      allocateTypeAllList().then(res => {
        if (res.status == 200) {
          this.allocateTypeList = res.data
        } else {
          this.allocateTypeList = []
        }
      })
    },
    // 操作员
    getUserList () {
      userQueryList({}).then(res => {
        if (res.status == 200) {
          this.operatorList = res.data
        } else {
          this.operatorList = []
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getAllocateTypeAllList()
      vm.getUserList()
    })
  }
}
</script>
