<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline='true'>
        <el-form-item label="文件名称">
          <el-input v-model="searchForm.matName" @keyup.enter.native="searchData()" size="small" placeholder="请输入物料名称"
            style="width:150px"></el-input>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker v-model="searchForm.entrTime" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" size="small" style="width:400px" format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00', '23:59:59']">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="goon" size="mini" v-loading="tableLoading"
            @click="searchData">查询</el-button>
          <el-button icon="el-icon-refresh" type="info" plain size="mini" @click="resetData">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row>
      <el-col :span='12'>
        <div class="common-title-div">
          <div class="common-title-name">
            <img style="width: 25px; height: 25px" src='../../../..//assets/img/imgScreen/logo.png' />
            审核信息
          </div>
          <div>
            <!-- <el-button icon="el-icon-back" type="danger" :loading='backDataLoading' size="mini" @click="backData222">回退</el-button> -->
          </div>
        </div>
        <div class="common-table-div" style="margin-bottom:0;margin-top:0">
          <el-table v-loading="tableLoading" ref="dataTable" border :height="mainHeight" highlight-current-row
            @selection-change="handleSelectionChange" :data="tableData" :icore-filter-flag="icoreFilterFlag"
            :header-cell-style="tableHeaderCellStyle" @current-change="table1RowChange" @row-click="searchFollow"
            :cell-class-name="tableRowClassName" :summary-method="getSummaries2" show-summary>
            <el-table-column type="index" label="NO" align="center"></el-table-column>
            <el-table-column type="selection" width="50" align="center"></el-table-column>
            <el-table-column show-overflow-tooltip sortable width="60" align="center" prop="stateFlagName"
              label="状态"></el-table-column>
            <el-table-column show-overflow-tooltip sortable width="100" align="center" prop="isoName"
              label="文件名称"></el-table-column>
            <el-table-column show-overflow-tooltip sortable width="100" align="center" prop="isoTypeName"
              label="文件分类"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="filePath" label="下载/预览"
              align="center"><template slot-scope="scope">
                <el-tooltip content="预览" placement="bottom" effect="light">
                  <el-button :disabled="!scope.row.filePath" type="warning" size="mini" icon="el-icon-view" circle
                    @click="
                      imgPreView(
                        scope.row,
                        scope.row.filePath
                      )
                      ">
                  </el-button>
                </el-tooltip>
              </template>
            </el-table-column>
            <!-- <el-upload class="upload-demo" :auto-upload="true" action :on-change="handleImg">
                                <el-button size="small" type="primary">点击上传图片或者文件预览</el-button>
                            </el-upload> -->
            <!-- <img :src="imgUrl" alt="" width="100px" height="100px" />
                            <iframe width="200px" height="500px" :src="fileUrl"> </iframe>
                            <video :src="videoUrl"></video> -->
            <!-- <span @click='preView'>预览</span>
                            <iframe width="200px" height="500px" :src="this.fileUrl"> </iframe> -->
            <el-table-column show-overflow-tooltip sortable width="100" align="center" prop="deptName"
              label="管理车间"></el-table-column>
            <el-table-column show-overflow-tooltip sortable width="100" align="center" prop="createMan"
              label="创建人"></el-table-column>
            <el-table-column show-overflow-tooltip sortable width="120" align="center" prop="createTime"
              label="创建时间"></el-table-column>
          </el-table>
        </div>
        <div class="common-foot-style">
          <el-pagination @size-change="handleSizeChangeMain" @current-change="handleCurrentChangeMain"
            :current-page="pageIndex" :page-sizes="[10, 20, 30, 50, 100, 200]" :page-size="pageSize" size="mini"
            layout="total, sizes, prev, pager, next, jumper" :total="total_main" background></el-pagination>
        </div>
      </el-col>
      <el-col :span='12'>
        <div class="common-title-div">
          <div class="common-title-name">
            <img style="width: 25px; height: 25px" src='../../../..//assets/img/imgScreen/logo.png' />
            审核流程
          </div>
          <div>
            <el-button icon="el-icon-circle-check" type="primary" :loading='auditDataLoading' size="mini"
              @click="auditData11">审核</el-button>
            <el-button icon="el-icon-back" type="danger" :loading='auditDataLoading' size="mini"
              @click="backData11">回退</el-button>
          </div>
        </div>
        <div class="common-table-div" style="margin-bottom:0;margin-top:0">
          <el-table v-loading="tableLoading2" ref="dataTable2" border :height="theight" highlight-current-row
            :data="tableData2" :icore-filter-flag="icoreFilterFlag" :header-cell-style="tableHeaderCellStyle"
            @current-change="tableRowChange" :cell-class-name="tableRowClassName1" :summary-method="getSummaries2"
            show-summary>
            <el-table-column type="index" label="NO" align="center"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable align="center" prop="stateFlagName1" label="状态"
              min-width="100"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable align="center" prop="isoItemName" label="执行项目"
              min-width="100"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable align="center" prop="optNo" label="执行岗位"
              min-width="100"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable align="center" prop="createTime" label="执行时间"
              min-width="100"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable align="center" prop="doOrder" label="执行顺序"
              min-width="100"></el-table-column>
            <el-table-column :show-overflow-tooltip="true" sortable align="center" prop="memo3" label="意见"
              min-width="100"></el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
    <el-dialog @close="closeDialog" :visible.sync="recordShowFlag" :close-on-click-modal="false">
      <div slot="title" class="titleBox">
        <i class='el-icon-document' style="font-size: 20px"></i>
        <span style="margin-left:10px;font-size:18px">{{ save }}</span>
      </div>
      <el-form :inline='true'>
        <el-form-item label="意见">
          <el-input type="textarea" v-model="form.desc" style="width: 560px;height: 36px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading='auditDataLoading' size="mini"
          @click="tableType === 0 ? auditData() : backData()">确定</el-button>
        <el-button size="mini" type="default" @click="closeDialog">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getCookie, formatDate } from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  data() {
    return {
      searchForm: {
        matName: '',
        entrTime: []
      },
      form: {
        desc: ''
      },
      tableLoading: false,
      tableLoading2: false,
      auditDataLoading: false,
      tableData: [],
      tableData2: [],
      table1RowChange1: [],//表格2选中的数据
      tableRowChange1: [],//表格2选中的数据
      mainHeight: 0,
      theight: 0,
      multipleSelection: [],
      icoreFilterFlag: true,
      pageIndex: 1,
      pageSize: 50,
      total_main: 0,
      // fileUrl:'http://172.16.4.162/icore.icp.web/pass/baseManagement/v1/file/download/oss-volume-UY20220305134623262.png'
      recordShowFlag: false,//审核页面显示
      save: '',
      tableType: 0,
      optNoParameter: '',
    }
  },
  created() {
    this.mainHeight = window.innerHeight - 120
    this.theight = window.innerHeight - 120
    this.searchForm.entrTime = [this.getYNM(new Date()) + ' 00:00:00', this.getYNM(new Date()) + ' 23:59:59']
    this.getCompany();
  },
  mounted() {

  },
  methods: {
    // 关闭弹窗
    closeDialog() {
      this.save = '';
      this.form.desc = '';
      this.tableType = 0;
      this.recordShowFlag = false
    },
    getYNM(date) {
      let year = date.getFullYear().toString();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      month = month < 10 ? "0" + month : month;
      day = day < 10 ? "0" + day : day;
      return `${year}-${month}-${day}`;
    },
    //     preView(){
    //         var originUrl = 'http://172.16.4.162/icore.icp.web/pass/baseManagement/v1/file/download/'; //要预览文件的访问地址
    // 　　　   var previewUrl = originUrl + 'oss-volume-UY20220305134623262.png'
    // 　　　   window.open(previewUrl);
    // 　　},
    //     handleImg(file, fileList) {
    //         // 预览不了word 是乱码   对于excel是默认下载 对于pdf 图片都可以预览
    //         let objUrl = window.URL.createObjectURL(file.raw);
    //         this.fileUrl = objUrl;
    //         this.videoUrl = objUrl;
    //         this.imgUrl = objUrl;
    //         // 这个objUrl不再使用后 要释放掉
    //         window.URL.revokeObjectURL(objUrl);
    //     },
    //     readFile(file, type = "base64") {
    //         return new Promise((resolve) => {
    //             let fileRead = new FileReader();
    //             if (type === "base64") {
    //             fileRead.readAsDataURL(file);
    //             } else if (type === "buffer") {
    //             fileRead.readAsArrayBuffer(file);
    //             } else if (type === "string") {
    //             fileRead.readAsBinaryString(file);
    //             }
    //             fileRead.onload = (ev) => {
    //             resolve(ev.target.result);
    //             };
    //         });
    //     },
    //     async handleImgByFileReader(file, fileList) {
    //         let data = await this.readFile(file.raw); // 得到的Base64数据
    //         this.imgUrl = data;
    //         // console.log(this.imgUrl)
    //         // 查看数据
    //         let base64 = await this.readFile(file.raw);
    //         console.log(base64);
    //         let buffer = await this.readFile(file.raw, "buffer");
    //         console.log(buffer); // 返回一个ArrayBuffer的对象
    //         let BinaryString = await this.readFile(file.raw, "string");
    //         console.log(BinaryString); // 返回一个二进制的字符串
    //     },
    imgPreView(row, fileName) {
      this.axios.get('pass/baseManagement/v1/file/download/' + fileName, { responseType: 'blob' }, { headers: { 'content-type': 'application/x-www-form-urlencoded' } }).then(res => {
        let blob = new Blob([res]);
        let elink = document.createElement('a');
        elink.download = fileName;
        elink.style.display = 'none';
        elink.target = "_blank";
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL对象
        document.body.removeChild(elink);
      }).catch(e => {
        return this.$message.error(e.message)
      })
    },
    backData11() {
      if (this.tableRowChange1.length === 0 || this.tableRowChange1 == null) {
        return this.$message.warning('请先点击一条数据！')
      }
      if (this.tableRowChange1.stateFlag != 0) {
        return this.$message.warning('请点击一条待审核状态的数据！')
      }
      this.tableType = 1;
      this.save = '退回意见';
      this.recordShowFlag = true;
    },
    backData222() {
      this.axios.get('pass/baseManagement/v1/download/path=oss-volume-Yt20220506105946526.pdf', { responseType: 'blob' }, { headers: { 'content-type': 'application/x-www-form-urlencoded' } }).then(res => {
        let blob = new Blob([res]);
        let fileName = oldFileName;
        let elink = document.createElement('a');
        elink.download = fileName;
        elink.style.display = 'none';
        elink.target = "_blank";
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL对象
        document.body.removeChild(elink);
      })
    },
    backData() {
      if (this.tableRowChange1.length === 0 || this.tableRowChange1 == null) {
        return this.$message.warning('请先点击一条数据！')
      }
      if (this.tableRowChange1.stateFlag != 0) {
        return this.$message.warning('请点击一条待审核状态的数据！')
      }
      this.tableRowChange1.memo3 = this.form.desc;
      this.auditDataLoading = true;
      this.axios.post("pass/baseManagement/v1/limsisodatads/backDataD", this.tableRowChange1, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message)
          this.auditDataLoading = false;
          this.closeDialog()
          this.searchFollow(this.table1RowChange1);
        } else {
          this.$message.error(res.message)
          this.auditDataLoading = false;
        }
      }).catch(err => {
        this.auditDataLoading = false;
        this.$message.error(err.message)
      })
    },
    auditData11() {
      if (this.tableRowChange1.length === 0 || this.tableRowChange1 == null) {
        return this.$message.warning('请先点击一条数据！')
      }
      if (this.tableRowChange1.stateFlag != 0) {
        return this.$message.warning('请点击一条待审核状态的数据！')
      }
      this.tableType = 0;
      this.save = '审核意见';
      this.recordShowFlag = true;
    },
    //获取个人所属哪个公司
    getCompany() {
      this.axios.get('pass/baseManagement/v1/limslrcemps/' + userId).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.optNoParameter = res.data ? res.data.postNo : ''
          this.searchData();
        } else {
          this.$message.error(e.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    //审核
    auditData() {
      if (this.tableRowChange1.length === 0 || this.tableRowChange1 == null) {
        return this.$message.warning('请先点击一条数据！')
      }
      if (this.tableRowChange1.stateFlag != 0) {
        return this.$message.warning('请点击一条待审核状态的数据！')
      }
      this.tableRowChange1.memo3 = this.form.desc;
      this.auditDataLoading = true;
      this.axios.post("pass/baseManagement/v1/limsisodatads/auditDataD", this.tableRowChange1, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message)
          this.auditDataLoading = false;
          this.closeDialog()
          this.searchFollow(this.table1RowChange1);
        } else {
          this.$message.error(res.message)
          this.auditDataLoading = false;
        }
      }).catch(err => {
        this.auditDataLoading = false;
        this.$message.error(err.message)
      })
    },
    searchData() {
      let startTime = ''
      let endTime = ''
      if (!this.searchForm.entrTime) {
        startTime = ''
        endTime = ''
      } else {
        startTime = this.searchForm.entrTime[0]
        endTime = this.searchForm.entrTime[1]
      }
      let obj = {
        pageNum: this.pageIndex,
        pageSize: this.pageSize,
        startTime: startTime,
        endTime: endTime,
        isoName: this.searchForm.matName,
        optNo: this.optNoParameter,
        tenantNo: '1'
      }
      this.tableLoading = true;
      this.axios.post("pass/baseManagement/v1/limsisodatas/queryLimsIsoDataPages", obj, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.tableData = res.data.list
          this.total_main = res.data.total
          this.tableData.forEach(item => {
            item.stateFlagName = item.stateFlag === '0' ? '编辑' : item.stateFlag === '1' ? '提交' : ''
          })
          if (this.tableData.length > 0) {
            this.$refs.dataTable.setCurrentRow(this.tableData[0]);
            this.searchFollow(this.tableData[0])
          }
          this.tableLoading = false;
        } else {
          this.$message.error(res.message)
          this.tableLoading = false;
        }
      }).catch(err => {
        this.tableLoading = false;
        this.$message.error(err.message)
      })
    },
    searchFollow(row) {
      this.tableLoading2 = true;
      this.axios.post("pass/baseManagement/v1/limsisodatads/queryLimsIsoDataDsByIsoNo", row, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.tableData2 = res.data;
          this.tableData2.forEach(item => {
            item.stateFlagName1 = item.stateFlag === '1' ? '已审核' : item.stateFlag === '0' ? '待审核' : item.stateFlag === '2' ? '已回退' : ''
          })
          this.tableLoading2 = false;
        } else {
          this.$message.error(res.message)
          this.tableLoading2 = false;
        }
      }).catch(err => {
        this.tableLoading2 = false;
        this.$message.error(err.message)
      })
    },
    tableRowChange(row) {
      this.tableRowChange1 = row;
    },
    table1RowChange(row) {
      this.table1RowChange1 = row;
    },
    resetData() {
      this.searchForm.matName = ''
      this.searchForm.entrTime = [this.getYNM(new Date()) + ' 00:00:00', this.getYNM(new Date()) + ' 23:59:59']
      this.searchData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChangeMain(pageSize) {
      this.pageIndex = 1;
      this.pageSize = pageSize;
      this.searchData();
    },
    handleSizeChangeMain1(pageSize) {
      this.pageIndex = 1;
      this.pageSize = pageSize;
      this.searchFollow();
    },
    handleCurrentChangeMain(pageIndex) {
      this.pageIndex = pageIndex;
      this.searchData();
    },
    handleCurrentChangeMain1(pageIndex) {
      this.pageIndex = pageIndex;
      this.searchFollow();
    },
    getSummaries2(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总计";
        }
        if (index === 1) {
          sums[index] = data.length;
        } else if (column.property === "quantity") {
          let val = 0;
          for (let i in data) {
            val = this.floatComputed(
              data[i].quantity ? data[i].quantity : 0,
              val,
              "+"
            );
          }
          sums[index] = val;
        } else if (column.property === "factweight") {
          let val = 0;
          for (let i in data) {
            val = this.floatComputed(
              data[i].factweight ? data[i].factweight : 0,
              val,
              "+"
            );
          }
          sums[index] = val;
        }
      });
      return sums;
    },
    tableHeaderCellStyle() {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    },
    //停用变色
    tableRowClassName({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 1) {
        if (row.stateFlag == '1') {
          return 'error-green';
        }
      }
    },
    //停用变色
    tableRowClassName1({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 1) {
        if (row.stateFlag == '1') {
          return 'error-green';
        }
        if (row.stateFlag == '2') {
          return 'error-row';
        }
      }
    },
  }
}
</script>
