<!--学生采购申请-->
<template>
  <div class="app-container studentPurchaseRequisition">
    <div class="studentPurchaseRequisition-page" v-if="pageType == 1">
      <div class="usageRecord-min">
        <el-form :model="queryParamsData" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="关键字" prop="searchValue">
            <el-input
              maxlength="20"
              v-model="queryParamsData.searchValue"
              placeholder="编号/姓名/化学品名/实验室"
              clearable
              size="small"/>
          </el-form-item>
          <el-form-item label="状态" prop="approvalStatus" label-width="44px">
            <el-select v-model="queryParamsData.approvalStatus" clearable placeholder="请选择状态" style="width: 140px">
              <el-option label="审批中" value="0"></el-option>
              <el-option label="已通过" value="1"></el-option>
              <el-option label="已拒绝" value="2"></el-option>
              <el-option label="已撤销" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="申请时间" prop="terminalStatus">
            <el-date-picker
              :clearable="false"
              v-model="dateRangeOne"
              size="small"
              style="width: 220px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="完成时间" prop="terminalStatus">
            <el-date-picker
              :clearable="false"
              v-model="dateRangeTwo"
              size="small"
              style="width: 220px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item style="float: right;">
            <el-col :span="1.5">
              <p class="inquire-button-one"
                 style="width:100px;"
                 @click="pageToggle(3)"
              >申购</p>
            </el-col>
          </el-form-item>
          <el-form-item style="float: right;">
            <el-col :span="1.5">
              <p class="add-button-one-90"
                 style="width:100px;"
                 @click="pageToggle(2)"
              >草稿箱</p>
            </el-col>
          </el-form-item>
          <el-form-item>
            <p class="inquire-button-one" @click="handleQuery" style="margin-right:10px;">查询</p>
            <p class="reset-button-one" @click="resetQuery">重置</p>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" border :data="tableList">
          <el-table-column label="申购编号" align="center" prop="applyNum"  show-overflow-tooltip/>
          <el-table-column label="实验室" align="center" prop="subName"  show-overflow-tooltip/>
          <el-table-column label="申购信息" align="center" prop="chemicalNames"  show-overflow-tooltip/>
          <el-table-column label="申请时间" align="center" prop="createTime"  show-overflow-tooltip/>
          <el-table-column label="完成时间" align="center" prop="overTime"  show-overflow-tooltip/>
          <el-table-column label="状态" align="center" prop="approvalContent"  width="160px"/>
          <el-table-column label="审批人" align="center" prop="status" width="100px">
            <template slot-scope="scope">
              <div>
                <el-tooltip class="item" effect="dark" :content="scope.row.dqName?'当前：'+scope.row.dqName:'当前：无'" placement="top">
                  <div style="display: flex">
                    <p>当前：</p>
                    <p style="flex:1;text-align: left;display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">{{scope.row.dqName?scope.row.dqName:'无'}}</p>
                  </div>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" :content="scope.row.lsName?'历史：'+scope.row.lsName:'历史：无'" placement="top">
                  <div style="display: flex">
                    <p>历史：</p>
                    <p style="flex:1;text-align: left;display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">{{scope.row.lsName?scope.row.lsName:'无'}}</p>
                  </div>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" prop="createTime" width="230px">
            <template slot-scope="scope">
              <div class="button-box">
                <p class="table-min-button" v-show="scope.row.approvalStatus == 2 || scope.row.approvalStatus == 3" style="margin-right:10px;" @click="pageToggle(5,scope.row)">编辑</p>
                <p class="table-min-button" v-show="scope.row.approvalStatus == 0 && !scope.row.lsName" style="margin-right:10px;" @click="approvalRevoke(scope.row)">撤销</p>
                <p class="table-min-button" style="margin-right:10px;" @click="pageToggle(7,scope.row)">审批单</p>
                <p class="table-min-button" v-show="scope.row.approvalStatus == 2 || scope.row.approvalStatus == 3" @click="delHxpapply(scope.row)">删除</p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <draft-Page v-if="pageType == 2"></draft-Page>
    <list-Page :listPagePropsData="listPagePropsData" v-if="pageType == 3"></list-Page>
    <approval-page :approvalPagePropsData="approvalPagePropsData" v-if="pageType == 4"></approval-page>
    <edit-page :editPagePropsData="editPagePropsData" v-if="pageType == 5"></edit-page>
  </div>
</template>

<script>
  import { getHxpapplyMyList,approvalRevoke,getHxpapply,delHxpapply } from "@/api/studentApi/chemicalManagement/index";
  import draftPage from "./draftPage.vue"
  import listPage from "./listPage.vue"
  import approvalPage from "./approvalPage.vue"
  import editPage from "./editPage.vue"
  export default {
    name: "index",
    components: {
      draftPage,
      listPage,
      approvalPage,
      editPage
    },
    data() {
      return {
        // 传参数据
        listPagePropsData:{},
        approvalPagePropsData:{},
        editPagePropsData:{},
        pageType:1,
        // 申请时间
        dateRangeOne:[],
        // 完成时间
        dateRangeTwo:[],
        // 搜索实际发送数据
        queryParams:{
          pageNum:1,
          pageSize:20,
        },
        queryParamsData:{
          pageNum:1,
          pageSize:20,
        },
        //数据数量
        total:0,
        tableList:[],
        loading:false,
      };
    },
    created() {

    },
    mounted(){
      this.getList();
    },
    methods: {
      //操作
      pageToggle(type,row){
        if(type == 0){
          //返回列表
          this.pageType = 1;
        }else if(type == 1){
          //返回列表重置搜索项
          this.pageType = 1;
          this.resetQuery();
        }else if(type == 2){
          //去草稿箱
          this.pageType = 2
        }else if(type == 3){
          //新增-去选择列表
          this.pageType = 3
        }else if(type == 4){
          //查看详情
          this.pageType = 4
        }else if(type == 5){
          //编辑
          getHxpapply(row.id).then(response => {
            console.log("row,id",row.id);
            let maxObj = {
              id:row.id,
              subId:parseInt(response.data.subId),
              applyReason:response.data.applyReason,
              maxList:[],
            }
            for(let i=0;i<response.data.actHxpapplyDetailList.length;i++){
              let num = 0;
              for(let o=0;o<maxObj.maxList.length;o++){
                if(maxObj.maxList[o].chemicalId == response.data.actHxpapplyDetailList[i].chemicalId){
                  num++
                }
              }
              if(num == 0){
                let obj = {
                  chemicalId:parseInt(response.data.actHxpapplyDetailList[i].chemicalId),
                  chemicalName:response.data.actHxpapplyDetailList[i].chemicalName,
                  list:[],
                }
                maxObj.maxList.push(obj);
              }
            }
            for(let i=0;i<response.data.actHxpapplyDetailList.length;i++){
              for(let o=0;o<maxObj.maxList.length;o++){
                if(maxObj.maxList[o].chemicalId == response.data.actHxpapplyDetailList[i].chemicalId){
                  let minObj = {
                    cabinetId:parseInt(response.data.actHxpapplyDetailList[i].cabinetId),
                    applyNum:parseInt(response.data.actHxpapplyDetailList[i].applyNum),
                    applyText:response.data.actHxpapplyDetailList[i].remainingStorage,
                  };
                  maxObj.maxList[o].list.push(minObj);
                }
              }
            }
            this.$set(this,'editPagePropsData',maxObj);
            this.pageType = 5;
          });
        }else if(type == 6){
          //审批
          let obj = {
            id:row.id,
            type:1,
            userType:row.userType
          };
          this.$set(this,'approvalPagePropsData',obj);
          this.pageType = 4;
        }else if(type == 7){
          //审批单
          let obj = {
            id:row.id,
            type:2,
            userType:row.userType
          };
          this.$set(this,'approvalPagePropsData',obj);
          this.pageType = 4;
        }
      },
      //撤销
      approvalRevoke(row){
        let self = this;
        this.$confirm('是否确认撤销?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          approvalRevoke({taskId:row.id}).then(response => {
            self.msgSuccess(response.msg);
            self.resetQuery();
          });
        }).then(() => {
        }).catch(() => {});
      },
      //删除
      delHxpapply(row){
        let self = this;
        this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          delHxpapply(row.id).then(response => {
            self.msgSuccess(response.msg);
            self.resetQuery();
          });
        }).then(() => {
        }).catch(() => {});
      },
      //获取数据列表
      getList(){
        this.queryParamsData = JSON.parse(JSON.stringify(this.queryParams));
        if(this.dateRangeOne[0]){
          this.queryParamsData.beginCreateTime = this.dateRangeOne[0];
          this.queryParamsData.endCreateTime = this.dateRangeOne[1];
        }else {
          this.queryParamsData.beginCreateTime = null
          this.queryParamsData.endCreateTime = null
        }
        if(this.dateRangeTwo[0]){
          this.queryParamsData.beginOverTime = this.dateRangeTwo[0];
          this.queryParamsData.endOverTime = this.dateRangeTwo[1];
        }else {
          this.queryParamsData.beginOverTime = null
          this.queryParamsData.endOverTime = null
        }
        getHxpapplyMyList(this.queryParamsData).then(response => {
          let userId = localStorage.getItem('userId')
          this.total = response.total;
          for(let i=0;i<response.rows.length;i++){
            response.rows[i].lsName = "";
            response.rows[i].dqName = "";
            for(let o=0;o<response.rows[i].currentUserList.length;o++){
              if( response.rows[i].currentUserList[o].id == userId){
                if(o == 0){
                  response.rows[i].lsName = response.rows[i].lsName + '我';
                }else{
                  response.rows[i].lsName = response.rows[i].lsName + '、我';
                }
              }else{
                if(o == 0){
                  response.rows[i].lsName = response.rows[i].lsName + response.rows[i].currentUserList[o].name;
                }else{
                  response.rows[i].lsName = response.rows[i].lsName + '、' + response.rows[i].currentUserList[o].name;
                }
              }
            }
            for(let o=0;o<response.rows[i].historyUserList.length;o++){
              if( response.rows[i].historyUserList[o].id == userId){
                if(o == 0){
                  response.rows[i].dqName = response.rows[i].dqName + '我';
                }else{
                  response.rows[i].dqName = response.rows[i].dqName + '、我';
                }
              }else{
                if(o == 0){
                  response.rows[i].dqName = response.rows[i].dqName + response.rows[i].historyUserList[o].name;
                }else{
                  response.rows[i].dqName = response.rows[i].dqName + '、'+ response.rows[i].historyUserList[o].name;
                }
              }

            }
          }
          this.tableList = response.rows;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParamsData.pageNum = 1;
        this.queryParamsData.pageSize = 20;
        this.queryParams = JSON.parse(JSON.stringify(this.queryParamsData));
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$set(this,'queryParamsData',{});
        this.$set(this,'queryParams',{});
        this.$set(this,'dateRangeOne',[]);
        this.$set(this,'dateRangeTwo',[]);
        this.handleQuery();
      },
    },
  }
</script>

<style scoped lang="scss">
  .studentPurchaseRequisition{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden!important;
    p{
      margin:0;
      padding:0;
    }
    .studentPurchaseRequisition-page{
      flex:1;
      display: flex;
      flex-direction: column;
      overflow: hidden!important;
      .usageRecord-min{
        flex:1;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        padding:20px;
        .button-box{
          display: flex;
        }
      }
    }
  }
</style>
