<!--申购登记-->
<template>
  <div class="app-container purchaseRegister">
    <div class="page-container purchaseRegister-page" v-if="pageType == 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;" label-width="90px">
          <el-form-item label="" prop="deptId">
            <el-select v-model="queryParams.deptId" placeholder="请选择学院" style="width: 130px">
              <el-option
                v-for="item in deptOptions"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="queryParams.applyStatus "
                       placeholder="请选择状态" style="width: 130px">
              <el-option
                v-for="dict in options"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-date-picker
              :clearable="false"
              v-model="dateRangeA"
              size="small"
              style="width: 214px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="申请时间"
              end-placeholder="申请时间"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="">
            <el-date-picker
              :clearable="false"
              v-model="dateRangeB"
              size="small"
              style="width: 214px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="完成时间"
              end-placeholder="完成时间"
            ></el-date-picker>
          </el-form-item>

          <el-form-item label="">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="化学品名/CAS/实验室/申购人/课题组/课题组成员"
              style="width: 332px"
            />
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;margin-left:20px;"
             v-hasPermiRouter="['chemical:apply:add']"
             @click="tableButton(1,'')"
          >申购</p>
          <p class="page-add-common-style-button"
             style="float: right;"
             v-hasPermiRouter="['chemical:apply:batch']"
             @click="tableButton(6,'')"
          >提交审核</p>
          <el-form-item style="float: right;"  v-hasPermiRouter="['chemical:apply:export']">
            <export-component :exportConfig="exportConfig"></export-component>
          </el-form-item>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList" @selection-change="handleSelectionChange" ref="table-box">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="申请时间" align="left" prop="applyTime" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.applyTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="申购人" width="100" align="left" prop="applyUserName" show-overflow-tooltip/>
          <el-table-column label="实验室" width="200" align="left" prop="subName" show-overflow-tooltip/>
          <el-table-column label="学院" width="240" align="left" prop="deptName" show-overflow-tooltip/>
          <el-table-column label="楼栋楼层" width="240" align="left" prop="buildName" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.buildName}}-{{scope.row.floorName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="完成时间" width="180" align="left" prop="createTime" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.completeTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" width="150" align="left" prop="auditNode" show-overflow-tooltip/>
          <el-table-column label="操作" width="230" align="left" v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   v-if="scope.row.applyStatus==4 || scope.row.applyStatus==5"
                   v-hasPermiRouter="['chemical:apply:detail']"
                   @click="tableButton(2,scope.row)"
                >详情</p>
                <p  class="table-button-p"
                   v-if="scope.row.applyStatus==2 || scope.row.applyStatus==3 || scope.row.applyStatus==6"
                   v-hasPermiRouter="['chemical:apply:edit']"
                   @click="tableButton(3,scope.row)"
                >编辑</p>
                <p class="table-button-p"
                   v-if="scope.row.applyStatus==2 || scope.row.applyStatus==3 || scope.row.applyStatus==6"
                   v-hasPermiRouter="['chemical:apply:del']"
                   @click="tableButton(4,scope.row)"
                >删除</p>
                <p class="table-button-p"
                   v-if="scope.row.applyStatus==4"
                   v-hasPermiRouter="['chemical:apply:reset']"
                   @click="tableButton(5,scope.row)"
                >撤销</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="selected-num-box" v-show="total>0">
          <p class="selected-num-p">
            <i class="el-icon-warning"></i>
            已选择 {{selectedNum}} 项
          </p>
          <pagination :page-sizes="[20, 30, 40, 50]"
                      :total="total"
                      :page.sync="queryParams.page"
                      :limit.sync="queryParams.pageSize"
                      @pagination="getList"
          />
        </div>
      </div>
    </div>
    <add-page v-if="pageType == 2" :propsData="propsData"></add-page>
    <info-page v-if="pageType == 3" :propsData="propsData"></info-page>
  </div>
</template>

<script>
  import { iotTypeList,iotTypeDetail,iotTypeAdd,iotTypeUpdate,iotTypeDelete } from "@/api/iotDevice";
  import addPage from "./addPage.vue"
  import infoPage from "./infoPage.vue"
  import exportComponent from "@/components/exportComponent/exportComponent.vue";
  import {listDepartments} from "@/api/commonality/noPermission";
  import {
    chemicalApplyBatchSubmit,
    chemicalApplyDelete, chemicalApplyDetail,
    chemicalApplyList,
    chemicalApplyRevoke
  } from '@/api/chemicalManage'
  import { getDeptDropList } from '@/api/commonality/permission'
  export default {
    name: 'index',
    components: {
      exportComponent,
      addPage,
      infoPage,
    },
    data(){
      return{
        tableButtonType:this.hasPermiDom(['chemical:apply:detail','chemical:apply:edit','chemical:apply:del','chemical:apply:reset']),
        loading:false,
        pageType:1,
        options:[{label:'保存草稿',value:2},{label:'撤销',value:3},{label:'审核中',value:4},{label:'已完成',value:5},{label:'驳回',value:6}],
        dateRangeA:[],
        dateRangeB:[],
        queryParams:{
          page:1,
          pageSize:20,
          deptId :"",
          applyStatus :"",
          startApplyTime :"",
          endApplyTime :"",
          startOverTime :"",
          endOverTime :"",
          searchValue :"",
        },
        dataList:[],
        total:10,
        deptOptions:[],
        // 选中数组
        selectedNum:0,
        ids:[],
        multiple:true,
        //组件传参
        propsData:{},
        exportConfig:{
          api:'chemical/apply/export',             //导出接口地址
          ids:'',                                       //勾选导出,勾选的IDS
          fileName:'申购登记导出',                         //导出文件的命名
        },
      }
    },
    created(){

    },
    mounted(){
      this.getList();
      this.getDeptDropList();
    },
    methods:{
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.$refs['table-box'].clearSelection();
        this.$set(this.exportConfig,'ids','');
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'dateRangeA',[]);
        this.$set(this,'dateRangeB',[]);
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          deptId:"",
          applyStatus :null,
          startApplyTime :"",
          endApplyTime :"",
          startOverTime :"",
          endOverTime :"",
          searchValue :"",
        });
        this.$refs['table-box'].clearSelection();
        this.$set(this.exportConfig,'ids','');
        this.getList();
      },
      //查询学院列表
      getDeptDropList(){
        getDeptDropList({deptName:"",level:2,deptType:1}).then(response => {
          this.$set(this, 'deptOptions', response.data)
        });
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        if(this.dateRangeA&&this.dateRangeA.length>0) {
          this.queryParams.startApplyTime=this.dateRangeA[0]
          this.queryParams.endApplyTime=this.dateRangeA[1]
        } else {
          this.queryParams.startApplyTime=null;
          this.queryParams.endApplyTime=null
        }
        if(this.dateRangeB&&this.dateRangeB.length>0) {
          this.queryParams.startOverTime=this.dateRangeB[0]
          this.queryParams.endOverTime=this.dateRangeB[1]
        } else {
          this.queryParams.startOverTime=null;
          this.queryParams.endOverTime=null
        }
        chemicalApplyList(this.queryParams).then(response => {
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
          this.$set(this,'loading',false);
        });
      },
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //申购
          this.$set(this,'propsData','');
          this.$set(this,'pageType',2);
        }else if(type == 2){
          //详情
          chemicalApplyDetail({applyId:row.applyId}).then(response => {
            this.$set(this,'propsData',response.data);
            this.$set(this,'pageType',3);
          });

        }else if(type == 3){
          //编辑
          chemicalApplyDetail({applyId:row.applyId}).then(response => {
            this.$set(this,'propsData',response.data);
            this.$set(this,'pageType',2);
          });
        }else if(type == 4){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            chemicalApplyDelete({applyId:row.applyId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 5){
          //撤销
          this.$confirm('是否确认撤销?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            chemicalApplyRevoke({applyId:row.applyId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 6){
          //提交审核
          chemicalApplyBatchSubmit({applyIds:this.ids}).then(response => {
            self.msgSuccess(response.message)
            self.getList();
          });
        }else if(type == 'out'){
          this.$set(this,'pageType',1);
          self.getList();
        }
      },
      /*===记录勾选数据===
        需要再el-table 添加  :row-key="getRowKeys"
        需要在selection 添加 :reserve-selection="true"
      */
      getRowKeys(row) {
        return row.applyId
      },
      //多选框选中数据
      handleSelectionChange(selection) {
        this.selectedNum = selection.length;
        this.ids = selection.map(item => item.applyId)
        this.multiple = !selection.length
        this.exportConfig.ids=this.ids
      },
    },
  }
</script>

<style scoped lang="scss">

.purchaseRegister{
  .purchaseRegister-page{

  }
}
</style>
