<template>
  <!-- 供应商产能报表 -->
  <div id="supplyCapacityTable">
    <!-- 查询区域 -->
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-item label="订单号">
                <a-input placeholder="请输入订单号" v-model="queryParam.csrccode"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="起止日期">
                <a-range-picker
                  style="width: 100%"
                  v-model="queryParam.timeRange"
                  format="YYYY-MM-DD"
                  :placeholder="['开始时间', '结束时间']"
                  @change="onDateChange"
                  @ok="onDateOk"
                />
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="订单类型">
                <a-input placeholder="请输入订单类型" v-model="queryParam.orderType"></a-input>
              </a-form-item>
            </a-col>

            <template v-if="toggleSearchStatus">
              <a-col :md="6" :sm="8">
                <a-form-item label="供应商">
                  <a-input placeholder="请输入供应商" v-model="queryParam.venName"></a-input>
                </a-form-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-item label="计划单号">
                  <a-input placeholder="请输入计划单号" v-model="queryParam.planLotNumber"></a-input>
                </a-form-item>
              </a-col>
            </template>

            <a-col :md="6" :sm="8">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                <a @click="handleToggleSearch" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>

    <!-- 操作按钮区域 -->
    <a-card :bordered="false" style="marginTop:10px;">
      <div class="table-operator">
        <a-button type="primary" @click="handleExportXls('供应链产能报表')" icon="download">合并导出</a-button>
        <a-button type="primary" @click="monthSupplyExport('月份供应商合计导出')" icon="download"
          >月份供应商合计导出</a-button
        >
        <a-button type="primary" @click="monthDepartExport('月份部门合计导出')" icon="download"
          >月份部门合计导出</a-button
        >
      </div>

      <!-- 合计 table  rowKey="id" :scroll="{ x: 1500 }" :pagination="ipagination  -->
      <a-table
        bordered
        :row-key="record => record.id"
        :columns="supplyCapacityColumns"
        :data-source="supplyCapacityData"
        :loading="loading"
        :pagination="pagination"
        @change="handleTableChange"
      >
      </a-table>
    </a-card>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import moment from 'moment'

import { supplyCapList } from '@api/reportForms/supply-capacity-table'
import { downFile } from '@/api/manage'
export default {
  name: 'SupplyCapacityTable', // 供应商产能报表
  mixins: [JeecgListMixin],
  components: { JEllipsis, moment },

  data() {
    // let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} /> // 省略
    return {
      description: '供应商产能报表',
      supplyCapacityColumns: [
        {
          title: '订单号',
          width: 140,
          align: 'left',
          dataIndex: 'csrccode',
          className: 'replacecolor'
        },

        {
          title: '计划单号',
          width: 90,
          dataIndex: 'cplanLotNumber',
          className: 'replacecolor'
        },
        { title: '部门', width: 90, dataIndex: 'cdepCode', className: 'replacecolor' },
        { title: '业务员', width: 90, dataIndex: 'cpersonCode', className: 'replacecolor' },
        { title: '供应商', width: 180, dataIndex: 'cvenCode', className: 'replacecolor' },

        { title: '供应商类型', width: 100, dataIndex: 'cvenCodeType', className: 'replacecolor' },
        { title: '订单类型', width: 90, dataIndex: 'iorderType', className: 'replacecolor' },
        {
          title: '数量',
          width: 100,
          dataIndex: 'iquantity',
          className: 'replacecolor'
        },
        { title: '累计入库数量', width: 120, dataIndex: 'ireceivedQTY', className: 'replacecolor' },
        { title: '单位', width: 90, dataIndex: 'unit', className: 'replacecolor' },
        {
          title: '计划到货月份',
          width: 120,
          dataIndex: 'arriveMonth',
          // customRender: text => {
          //   return moment(text).format('YYYY-MM')
          // },
          className: 'replacecolor'
        },
        {
          title: '计划到货日期',
          width: 120,
          dataIndex: 'darriveDate',
          customRender: text => {
            return moment(text).format('YYYY-MM-DD')
          },
          className: 'replacecolor'
        },
        { title: '系数', width: 60, dataIndex: 'coefficient', className: 'replacecolor' }
      ],
      supplyCapacityData: [],
      loading: false, // 表格加载
      // 查询条件
      queryParam: {
        csrccode: '',
        startDate: '',
        endDate: '',
        // timeRange = [], 月份范围
        orderType: '',
        venCode: '',
        planLotNumber: '',
        pageNo: '1',
        pageSize: '10'
      },
      // 分页
      pagination: {
        // total: '',
        // current: 0,
        // pageSize: 0
      },
      dateFormat: 'YYYY-MM-DD'
    }
  },
  created() {
    this.getSupplyCapList() // 渲染 供应链产能报表
  },

  methods: {
    // 供应商产能数据
    getSupplyCapList() {
      this.$nextTick(() => {
        supplyCapList(this.queryParam).then(res => {
          if (res.success) {
            this.supplyCapacityData = res.result.records
            console.log('【供应商产能报表数据】:', this.supplyCapacityData)
            this.pagination = {
              total: res.result.total,
              current: res.result.current,
              pageSize: res.result.size
            }
          }
        })
      })
    },

    //合并导出
    handleExportXls(fileName) {
      console.log('需导出的fileName:', fileName)
      const params = this.supplyCapacityData
      console.log('导出参数', params)
      downFile('/scas/supplierCapacity/excel1', params).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
      })
    },

    // 月份供应链合计导出
    monthSupplyExport(fileName) {
      console.log('需导出的fileName:', fileName)
      const params = this.supplyCapacityData
      console.log('导出参数', params)
      downFile('/scas/supplierCapacity/excel3', params).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
      })
    },
    // 月份部门合计导出
    monthDepartExport(fileName) {
      console.log('需导出的fileName:', fileName)
      const params = this.supplyCapacityData
      console.log('导出参数', params)
      downFile('/scas/supplierCapacity/excel2', params).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
      })
    },

    // 查询按钮
    searchQuery() {
      this.queryParam.pageNo = ''
      this.getSupplyCapList()
    },
    searchReset() {
      this.queryParam = {}
      this.getSupplyCapList()
    },
    // 查询条件 月范围份转换成字符串并赋值
    onDateChange(value, dateString) {
      this.queryParam.startDate = dateString[0]
      this.queryParam.endDate = dateString[1]
      console.log('起始日期', this.queryParam.startDate, this.queryParam.endDate)
    },
    onDateOk(value) {
      console.log('value', value)
    },
    // 分页变化时触发
    handleTableChange(pagination, filters, sorter) {
      console.log('分页器信息', pagination)
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = sorter.order == 'ascend' ? 'asc' : 'desc'
      }
      this.queryParam.pageNo = pagination.current
      this.getSupplyCapList()
    }
  }
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
@import '~@assets/less/overwriter.less';
/deep/ .ant-table-thead > tr > th {
  text-align: center;
  // font-weight: 700;
}

/deep/ .ant-table-tbody {
  text-align: center;
}

// /deep/ th.replacecolor {
//   background-color: #ccc;
// }

/deep/ .ant-table-footer .ant-table.body {
  // overflow: hidden !important;
}
/deep/ .ant-table.ant-table-bordered .ant-table-footer {
  border: none;
  padding: 0;
}
</style>
