<!--草稿箱-->
<template>
  <div class="draftPage">
    <div class="draftPage-min" v-if="pageType == 1">
      <el-form :model="queryParamsData" class="form-box" ref="queryForm" :inline="true">
        <el-form-item label="关键字" prop="searchValue">
          <el-input
            style="width:210px;"
            maxLength="30"
            v-model="queryParamsData.searchValue"
            placeholder="编号/化学品名/实验室"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="terminalStatus">
          <el-date-picker
            :clearable="false"
            v-model="dateRange"
            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>
          <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-item style="float: right;">
          <el-col :span="1.5">
            <p class="reset-button-one"
               @click="backPage"
            >返回</p>
          </el-col>
        </el-form-item>
      </el-form>
      <el-table  border :data="tableList"ref="multipleTable">
        <el-table-column label="申购编号" align="center" prop="applyNum" width="210" show-overflow-tooltip/>
        <el-table-column label="实验室" align="center" prop="subName" width="307" show-overflow-tooltip/>
        <el-table-column label="申购信息" align="center" prop="chemicalNames" show-overflow-tooltip/>
        <el-table-column label="创建时间" align="center" prop="createTime" width="200" show-overflow-tooltip/>
        <el-table-column label="操作" align="center" prop="operator" width="130">
          <template slot-scope="scope">
            <div class="button-box">
              <p class="table-min-button"
                 style="margin:0!important;"
                 @click="pageToggle(2,scope.row.id)"
              >编辑</p>
              <p class="table-min-button"
                 style="margin:0!important;"
                 @click="delHxpapply(scope.row.id)"
              >删除</p>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page-sizes="[20, 30, 40, 50]"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <edit-page :editPagePropsData="editPagePropsData" v-if="pageType == 2"></edit-page>
  </div>
</template>

<script>
  import editPage from "./editPage.vue"
  import { getHxpapplyDraftsList,delHxpapply,getHxpapply } from "@/api/studentApi/chemicalManagement/index";
  export default {
    name: "draftPage",
    components: {
      editPage
    },
    data(){
      return{
        // 遮罩层
        loading:false,
        //页面状态
        pageType:1,
        // 创建时间
        dateRange:[],
        // 搜索数据
        queryParamsData:{
          pageNum:1,
          pageSize:20,
        },
        // 搜索实际发送数据
        queryParams:{
          pageNum:1,
          pageSize:20,
        },
        //数据数量
        total:0,
        //数据数组
        tableList:[],
        //详情数据
        editPagePropsData:{}
      }
    },
    created() {

    },
    mounted(){
      this.getList();
    },
    methods: {
      //编辑页面
      pageToggle(type,id){
        if(type == 0){
          this.pageType = 1;
        }else if(type == 1){
          this.pageType = 1;
          this.resetQuery();
        }else if(type == 2){
          this.getHxpapply(id);
        }
      },
      //查询详情
      getHxpapply(id){
        getHxpapply(id).then(response => {
          let maxObj = {
            id: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,
                applyUnit:response.data.actHxpapplyDetailList[i].applyUnit,
                list:[],
              }
              maxObj.maxList.push(obj);
            }
          }
          for(let i=0;i<response.data.actHxpapplyDetailList.length;i++){
            for(let o=0;o<maxObj.maxList.length;o++){
              console.log('maxObj.maxList[o].chemicalId',maxObj.maxList[o].chemicalId)
              console.log('response.data.actHxpapplyDetailList[i].chemicalId',response.data.actHxpapplyDetailList[i].chemicalId)
              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 = 2;
        });
      },
      //删除
      delHxpapply(id){
        let self = this;
        this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          delHxpapply(id).then(response => {
            self.msgSuccess(response.msg);
            self.resetQuery();
          });
        }).then(() => {
        }).catch(() => {});
      },
      //获取数据列表
      getList(){
        this.queryParamsData = JSON.parse(JSON.stringify(this.queryParams));
        if(this.dateRange[0]){
          this.queryParamsData.beginCreateTime = this.dateRange[0];
          this.queryParamsData.endCreateTime = this.dateRange[1];
        }else {
          this.queryParamsData.beginCreateTime = null
          this.queryParamsData.endCreateTime = null
        }
        getHxpapplyDraftsList(this.queryParamsData).then(response => {
          this.total = response.total;
          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,'dateRange',[]);
        this.handleQuery();
      },
      //返回
      backPage(){
        this.$parent.pageToggle(1);
      },
    }
  }
</script>

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