<script src="../../../api/jst/purchase_order_exception.js"></script>
<template>
  <div class="mod-config">
    <el-form  v-model="searchData">
      <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: 5px;" >
        <el-form-item :label="'采购单号：'">
          <el-input v-model="searchData.poId" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item :label="'异常分类：'">
          <el-select v-model="searchData.histType" style="width: 120px">
            <el-option label="全部" value="ALL"></el-option>
            <el-option label="采购单" value="purchaseOrder" el-selected></el-option>
            <el-option label="采购入库" value="purchaseInStock"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="' '">
          <el-button @click="refreshPageTables()" style="margin-left: 0px" type="primary">查询</el-button>
        </el-form-item>
                    <el-form-item :label="' '">
                      <download-excel
                        :fields="fields()"
                        type="xls"
                        :name="exportName"
                        :fetch="createExportData"
                        worksheet="导出信息"
                        class="el-button el-button--primary el-button--medium">
                        {{ '导出' }}
                      </download-excel>
                    </el-form-item>
                  </el-form>
                </el-form>
                <el-table
                  :height="height"
                  :data="tableData"
                  ref="purchaseTable"
                  highlight-current-row
                  @row-click="setCurrentRow"
                  @current-change="changeCurrentRow"
                  border
                  style="width: 100%">
                  <el-table-column
                    fixed="left"
                    header-align="center"
                    align="center"
                    width="75"
                    label=操作>
                    <template slot-scope="scope">
                      <a class="customer-a" @click="reSyncIfsException(scope.row)">重新同步</a>
                    </template>
                  </el-table-column>
                  <el-table-column
                    v-for="(item,index) in columnList" :key="index"
                    :sortable="item.columnSortable"
                    :prop="item.columnProp"
                    header-align="center"
                    :show-overflow-tooltip="item.showOverflowTooltip"
                    :align="item.align"
                    :fixed="item.fixed==''?false:item.fixed"
                    :min-width="item.columnWidth"
                    :label="item.columnLabel">
                    <template slot-scope="scope">
                      <span v-if="!item.columnHidden"> {{ scope.row[item.columnProp] }}</span>
                      <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]"
                                                         style="width: 100px; height: 80px"/></span>
                    </template>
                  </el-table-column>

                </el-table>
  </div>


</template>

<script>
import {
  getPurchaseOrderException,
  reSyncIfsException,
} from '@/api/jst/purchase_order_exception.js'
import Vue from 'vue'

export default {
    name: 'purchaseOrder',
    data () {
      return {
        // 导出+动态列 start
        exportData: [],
        userDev: this.$store.state.user.userDev,
        exportName: '异常记录表' + this.getStrDate(),
        columnList: [
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'poId',
            columnLabel: '采购单号',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 90,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'ioId',
            columnLabel: '入库单号',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 90,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'poDate',
            columnLabel: '采购时间',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 120,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'ioDate',
            columnLabel: '入库时间',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 120,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'skuId',
            columnLabel: '商品编码',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 90,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'supplierId',
            columnLabel: '供应商编码',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 120,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'seller',
            columnLabel: '供应商名称',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 60,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'purchaserName',
            columnLabel: '采购员',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 90,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'labels',
            columnLabel: '标签',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 80,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'right'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'syncTime',
            columnLabel: '同步时间',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 120,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'exceptionRemark',
            columnLabel: '异常信息',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 180,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
          {
            tableId: 'purchaseOrderHist',
            tableName: this.$route.meta.title,
            columnProp: 'histType',
            columnLabel: '业务分类',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            columnWidth: 90,
            format: null,
            functionId: this.$route.meta.menuId,
            sortLv: 0,
            status: true,
            fixed: '',
            serialNumber: null,
            columnType: null,
            align: 'left'
          },
        ],
        currentRow:{},
        visible: false,
        // 导出 end
        height: 200,
        tableData: [],
        searchData: {
          poId: '',
          histType: 'ALL',
        },
      }
    },
    mounted () {
      this.$nextTick(() => {
        this.height = window.innerHeight - 150
      })
    },
    methods: {
      refreshPageTables () {
        getPurchaseOrderException(this.searchData).then(({data}) => {
          this.tableData = data.rows;
          if(data.total > 0){
            //设置选中行
            this.$refs.purchaseTable.setCurrentRow(this.tableData[0]);
          }
        })
      },

      //excel转导入的数据
      reSyncIfsException(){
        reSyncIfsException(this.currentRow).then(({data}) =>{
          if(data.code === 200){
            this.$message.success('操作成功！');
            //刷新当前页面数据
            this.refreshPageTables();
          }else{
            this.$message.error(data.msg)
          }
        });
      },

      /*设置工艺的行*/
      setCurrentRow(row, column, event) {
        this.currentRow = row;
      },

      // 选中记录
      /*当前值发生变化的时候修改*/
      changeCurrentRow(row, oldRow){
        //判断是否是获取焦点的事件
        if(row){
          this.currentRow = JSON.parse(JSON.stringify(row));
        }
      },



      // 导出+动态列
      fields () {
        let json = '{'
        this.columnList.forEach((item, index) => {
          if (index === this.columnList.length - 1) {
            json += '"' + item.columnLabel + '"' + ':' + '"' + item.columnProp + '"'
          } else {
            json += '"' + item.columnLabel + '"' + ':' + '"' + item.columnProp + '"' + ','
          }
        })
        json += '}'
        return eval('(' + json + ')')
      },
      createExportData () {
        // 点击导出按钮之后,开始导出数据之前的执行函数,返回值为需要下载的数据
        return this.tableData
      },
      getStrDate () {
        let dd = new Date()
        let Y = dd.getFullYear()
        let M = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1)// 获取当前月份的日期，不足10补0
        let D = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate()// 获取当前几号，不足10补0
        let H = dd.getHours() < 10 ? '0' + dd.getHours() : dd.getHours()
        let MM = dd.getMinutes() < 10 ? '0' + dd.getMinutes() : dd.getMinutes()
        let S = dd.getSeconds() < 10 ? '0' + dd.getSeconds() : dd.getSeconds()
        return Y + M + D + H + MM + S
      },
      userSetting () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs.column.init(this.queryTable)
        })
      },

      /*新增文件的modal*/
      addUploadFileModal(){
        //打开组件 去做新增业务
        this.$nextTick(() => {
          this.$refs.comSalesOrderUploadFile.init();
        })
      },

      /*导出销售单明细的数据*/
      exportSalesOrder(){
        downSalesOrder('download').then(({data}) => {
          // 不限制文件下载类型
          const blob = new Blob([data], {type: 'application/octet-stream;charset=utf-8'})
          // 下载文件名称
          const fileName = '销售单明细.xlsx';
          // a标签下载
          const linkNode = document.createElement('a')
          linkNode.download = fileName // a标签的download属性规定下载文件的名称
          linkNode.style.display = 'none'
          linkNode.href = URL.createObjectURL(blob) // 生成一个Blob URL
          console.log(linkNode)
          // if(val == 'Y'){
          //   this.pdfVisible = true
          //   this.pdfUrl = linkNode.href
          // }else {
          document.body.appendChild(linkNode)
          linkNode.click() // 模拟在按钮上的一次鼠标单击
          URL.revokeObjectURL(linkNode.href) // 释放URL 对象
          document.body.removeChild(linkNode)
        })

        /*axios.get('/proxyApi/jst/downSalesOrder?remark=销售明细', {
          responseType: 'blob',
          headers: {
            'Content-Type': 'application/json',
            'token': Vue.cookie.get('token')
          }
        }).then(({data}) => {
          // 不限制文件下载类型
          const blob = new Blob([data], {type: "application/octet-stream"})
          // 下载文件名称
          const fileName = "销售明细.xlsx"
          console.info(fileName);
          // a标签下载
          const linkNode = document.createElement('a')
          linkNode.download = fileName // a标签的download属性规定下载文件的名称
          linkNode.style.display = 'none'
          linkNode.href = URL.createObjectURL(blob) // 生成一个Blob URL
         debugger;
          document.body.appendChild(linkNode)
          linkNode.click() // 模拟在按钮上的一次鼠标单击
          URL.revokeObjectURL(linkNode.href) // 释放URL 对象
          document.body.removeChild(linkNode)
          // }
        })*/
        /*let url = '/api/ftp/file//downSalesOrder'
        let token = Vue.cookie.get('token');
        axios.get(url,{
          headers: {
            'Content-Type': 'application/json',
            'token': token
          },
          responseType: 'blob',//二进制流
        }).then(function (res) {
          if (!res) return
          let blob = new Blob([res.data], {type: 'application/vnd.ms-excel;charset=utf8'})
          var downloadElement = documentcreateElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.href = href;
          downloadElement.download = '销售明细导出.xlsx'; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeobjectURL(href); //释放掉blob对象
        }).catch(function (error){
          console.log(error)
        })*/
        /*let url = '/proxyApi/jst/downSalesOrder?remark=销售明细';
        const xhr = new XMLHttpRequest()
        xhr.open('get', url)
        xhr.responseType = 'blob'//设置后端返回类型为二进制流，不设置会返回乱码
        xhr.setRequestHeader('token', Vue.cookie.get('token')) // 若依的鉴权token
        xhr.onreadystatechange = function() {
          if (this.readyState === 4 && this.status === 200) {
            const name = '销售明细';
            // 支持IE浏览器
            if (window.navigator && window.navigator.msSaveOrOpenBlob) {
              window.navigator.msSaveOrOpenBlob(this.response, name)
              return
            }
            const href = window.URL.createObjectURL(this.response)
            const link = document.createElement('a')
            link.style.display = 'none'
            link.href = href
            link.setAttribute('download', name)
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
            window.URL.revokeObjectURL(href) // 释放掉blob对象
          }
        }
        xhr.send();*/
      },



    },

    created() {
      this.refreshPageTables();
      // this.getLanguageList()
    }
  }
</script>

<style scoped>

</style>
