<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-date-picker
                  placeholder="请选择开始月份"
                  style="width: 100%"
                  format="YYYY-MM"
                  v-model="queryParam.startDate"
                ></a-date-picker>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="结束月份">
                <a-date-picker
                  placeholder="请选择结束月份"
                  style="width: 100%"
                  format="YYYY-MM"
                  v-model="queryParam.endDate"
                ></a-date-picker>
              </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.orderType"></a-input>
                </a-form-item>
              </a-col>

              <a-col :md="6" :sm="8">
                <a-form-item label="供应商">
                  <a-input placeholder="请输入供应商" v-model="queryParam.venCode"></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: 120,
          dataIndex: 'csrccode',
          className: 'replacecolor'
        },

        {
          title: '计划单号',
          width: 120,
          dataIndex: 'planLotNumber',
          className: 'replacecolor'
        },
        { title: '部门', width: 120, dataIndex: 'cDepCode', className: 'replacecolor' },
        { title: '业务员', width: 120, dataIndex: 'cPersonCode', className: 'replacecolor' },
        { title: '供应商', width: 120, dataIndex: 'venCode', className: 'replacecolor' },
        // customRender: function(text) {
        //   if (text == '1') {
        //     return '销售类型1'
        //   }
        //   if (text == '2') {
        //     return '销售类型2'
        //   }
        //   if (text == '3') {
        //     return '销售类型3'
        //   }
        //   if (text == '4') {
        //     return '销售类型4'
        //   }
        // }
        { title: '供应商类型', width: 120, dataIndex: 'venCodeType', className: 'replacecolor' },
        { title: '订单类型', width: 120, dataIndex: 'orderType', className: 'replacecolor' },
        {
          title: '数量',
          width: 120,
          dataIndex: 'iQuantity',
          className: 'replacecolor'
        },
        { title: '累计入库数量', width: 120, dataIndex: 'iReceivedQTY', className: 'replacecolor' },
        { title: '单位', width: 100, dataIndex: 'unit', className: 'replacecolor' },
        {
          title: '计划到货月份',
          width: 120,
          dataIndex: 'arriveMonth',
          // customRender: text => {
          //   return moment(text).format('YYYY-MM')
          // },
          className: 'replacecolor'
        },
        {
          title: '计划到货日期',
          width: 120,
          dataIndex: 'planDeliveryData',
          // customRender: text => {
          //   return moment(text).format('YYYY-MM-DD')
          // },
          className: 'replacecolor'
        },
        { title: '系数', width: 100, dataIndex: 'coefficient', className: 'replacecolor' }
      ],
      supplyCapacityData: [],
      loading: false, // 表格加载
      // 查询条件
      queryParam: {
        csrccode: '',
        startDate: '',
        endDate: '',
        orderType: '',
        venCode: '',
        planLotNumber: '',
        pageNo: '' // 当前页
      },
      // 分页
      pagination: {
        // total: '',
        // current: 0,
        // pageSize: 0
      },
      dateFormat: 'YYYY-MM-DD'
    }
  },
  created() {
    this.getSupplyCapList() // 渲染 供应链产能报表
    // console.log('000')
  },

  methods: {
    // 供应商产能数据
    getSupplyCapList() {
      this.$nextTick(() => {
        supplyCapList(this.queryParam).then(res => {
          // console.log('111')
          if (res.success) {
            // console.log('222')
            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.getSupplyCapList()
    },
    searchReset() {
      this.queryParam = {}
      this.getSupplyCapList()
    },

    // 分页变化时触发
    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>
