<!--化学品信息-->
<template>
  <div class="app-container ChemicalInfo">
    <!--列表页面-->
    <div class="ChemicalInfo-page" v-if="pageType == 1">
      <el-form :model="queryParamsData" class="form-box" ref="queryForm" :inline="true">
        <el-form-item label="关键字" prop="searchValue">
          <el-input
            maxLength="20"
            v-model="queryParamsData.searchValue"
            placeholder="化学品名/别名/CAS号"
            clearable
            size="small"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="化学品分类" prop="chemicalClassify">
          <el-select v-model="queryParamsData.chemicalClassify" clearable placeholder="请选择化学品分类" style="width:180px;">
            <el-option
              v-for="item in optionsListOne"
              :key="item.id"
              :label="item.classifyName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="属性" prop="classifyAttribute">
          <el-select v-model="queryParamsData.classifyAttribute" clearable placeholder="请选择属性" style="width:180px;">
            <el-option
              v-for="item in optionsListTwo"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="标签类型" prop="labelType">-->
          <!--<el-select v-model="queryParamsData.labelType" clearable placeholder="请选择标签类型" style="width:180px;">-->
            <!--<el-option-->
              <!--v-for="item in optionsList"-->
              <!--:key="item.id"-->
              <!--:label="item.name"-->
              <!--:value="item.id">-->
            <!--</el-option>-->
          <!--</el-select>-->
        <!--</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" v-hasPermi="['chemical:hxpChemical:add']">
            <p class="add-button-one-90"
               @click="pageToggle(2)"
            ><i class="el-icon-plus"></i>新增</p>
          </el-col>
        </el-form-item>
        <el-form-item style="float: right;">
          <el-dropdown @command="exportButton" v-hasPermi="['chemical:hxpChemical:export']">
            <div class="form-dropdown-box">
              <img src="@/assets/ZDimages/personnelManagement/icon_jzgxx_sc.png">
              <p>导出</p>
              <img src="@/assets/ZDimages/personnelManagement/icon_jzggl_xljt.png">
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item style="border-bottom:1px solid #E0E0E0;margin:0 10px;color:#333;" :command="{command:1}">导出全部数据</el-dropdown-item>
              <el-dropdown-item style="margin:0 10px;color:#666;" :command="{command:2}">导出选中数据</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item>
        <el-form-item style="float: right;">
          <el-dropdown @command="importButton" v-hasPermi="['chemical:hxpChemical:import']">
            <div class="form-dropdown-box">
              <img src="@/assets/ZDimages/personnelManagement/icon_jzgxx_dr.png">
              <p>导入</p>
              <img src="@/assets/ZDimages/personnelManagement/icon_jzggl_xljt.png">
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item style="border-bottom:1px solid #E0E0E0;margin:0 10px;color:#333;" :command="{command:1}">下载模板</el-dropdown-item>
              <el-dropdown-item style="margin:0 10px;color:#666;" :command="{command:2}">导入数据</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item>
      </el-form>
      <el-table  border :data="tableList" @selection-change="handleSelectionChange" ref="multipleTable" :row-key="getRowKeys">
        <el-table-column type="selection" width="50" align="center" :reserve-selection="true"/>
        <el-table-column label="ID" align="center" prop="chemicalNum" width="123px"/>
        <el-table-column label="化学品名" align="center" prop="chemicalName" show-overflow-tooltip/>
        <el-table-column label="CAS号" align="center" prop="casNum" width="140px"/>
        <el-table-column label="分类" align="center" prop="classifyName" width="130px"/>
        <el-table-column label="属性" align="center" prop="classifyAttribute" width="150px" show-overflow-tooltip/>
        <el-table-column label="形态" align="center" prop="chemicalShapeInfo" width="100px"/>
        <!--<el-table-column label="标签类型" align="center" prop="labelContent" width="100px"/>-->
        <!--<el-table-column label="验证方式" align="center" prop="verificationContent" width="150px"/>-->
        <el-table-column label="创建时间" align="center" prop="createTime" width="170px"/>
        <el-table-column label="创建人" align="center" prop="createBy" width="130px"/>
        <el-table-column label="操作" align="center" width="280" class-name="small-padding fixed-width" v-if="tableButtonType">
          <template slot-scope="scope">
            <div class="button-box">
              <p class="table-min-button" v-hasPermi="['chemical:hxpChemical:msdsList']" @click="pageToggle(4,scope.row)">MSDS</p>
              <p class="table-min-button" v-hasPermi="['chemical:hxpChemical:joinList']" @click="pageToggle(3,scope.row)">关联化学品柜</p>
              <el-dropdown @command="moreClick" v-hasPermi="['chemical:hxpChemical:query','chemical:hxpChemical:edit','chemical:hxpChemical:remove']">
                <p class="table-min-button">更多>></p>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item style="border-bottom:1px solid #E0E0E0;margin:0 10px;" :command="{row:scope.row,command:1}" v-hasPermiAnd="['chemical:hxpChemical:query','chemical:hxpChemical:edit']">编辑</el-dropdown-item>
                  <el-dropdown-item style="margin:0 10px;" :command="{row:scope.row,command:2}" v-hasPermi="['chemical:hxpChemical:remove']">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex;height:32px;margin:5px 0 15px;">
        <!--<p style="flex:4;"></p>-->
        <p style="text-align: left;margin:0;line-height:32px;margin-top:15px;margin-right:20px;font-size:14px;color:#999;">
          <i class="el-icon-warning" style="color:#0183FA;"></i>
          已选择 {{selectedNum}} 项
        </p>
        <div style="flex:5;">
          <pagination :page-sizes="[20, 30, 40, 50]"
            v-show="total>0"
            :total="total"
            style="margin:0;"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>
    </div>
    <!--新增化学品-->
    <add-page v-if="pageType == 2" :addPropsData="addPropsData"></add-page>
    <!--关联化学品柜-->
    <list-page v-if="pageType == 3" :listPropsData="listPropsData"></list-page>
    <msds-page v-if="pageType == 4" :msdsPropsData="msdsPropsData"></msds-page>
    <!--导入窗口-->
    <el-dialog title="导入数据" :visible.sync="importOpen" @close="importOpenOff" width="600px" append-to-body class="import-dialog-box">
      <div class="el-upload__text" style="margin:0 0 20px 58px;">提示：导入后的数据将覆盖原有数据</div>
      <el-upload
        class="import-dialog-upLoad-box"
        :drag="true"
        :data="upImportData"
        :action="uploadImgUrl"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :headers="headers"
        :before-upload="beforeAvatarUpload">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <!--<div class="el-upload__tip" slot="tip">-->
          <!--<el-checkbox v-model="upImportData.upDate">是否更新已经存在的数据</el-checkbox>-->
        <!--</div>-->
        <div class="el-upload__tip" slot="tip">数据导入模板.xlsx<span style="color:#0183FA;cursor: pointer" @click="importTemplate">下载</span></div>
        <div class="el-upload-text-box" slot="tip" v-if="getImportData.downFile">
          <div class="img-text-box">
            <img src="@/assets/ZDimages/personnelManagement/icon_dr_wj.png">
            <p>{{getImportData.textName}}</p>
          </div>
          <p class="text-p">数据导入成功 <span style="color:#0183FA;">{{getImportData.successNum}}</span> 条，失败 <span style="color:#FF6464 ;">{{getImportData.failureNum}}</span> 条</p>
          <div class="img-text-box">
            <img src="@/assets/ZDimages/personnelManagement/icon_dr_sj.png">
            <p style="color:#0183FA;cursor: pointer;" @click="failureExcel">点击下载失败数据报表</p>
          </div>
        </div>
      </el-upload>
      <div slot="footer" class="teacher-import-dialog-button-box">
        <p class="inquire-button-one" @click="importOpenOff">确定</p>
      </div>
      <div class="teacher-import-dialog-position-box" v-if="loading"></div>
    </el-dialog>
  </div>
</template>

<script>
  import { hxpChemicalList,classifyList,getHxpChemicalInfo,delHxpChemicalInfo } from "@/api/medicUniversity-3_1/index";
  import { getToken } from "@/utils/auth";
  import addPage from "./addPage.vue"
  import listPage from "./listPage.vue"
  import msdsPage from "./msdsPage.vue"
  export default {
    name: "ChemicalInfo",
    components: {
      addPage,
      listPage,
      msdsPage
    },
    data() {
      return {
        // table操作按钮校验
        tableButtonType:this.hasPermiDom(['chemical:hxpChemical:query','chemical:hxpChemical:edit','chemical:hxpChemical:remove','chemical:hxpChemical:joinList','chemical:hxpChemical:msdsList']),
        //上传相关配置
        uploadImgUrl: window.location.href.split('://')[0]+'://' + process.env.VUE_APP_BASE_API + "/chemical/hxpChemical/importData", // 上传地址
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        // 遮罩层
        loading:false,
        //页面状态
        pageType:1,
        // 搜索数据
        queryParamsData:{
          pageNum:1,
          pageSize:20,
        },
        // 搜索实际发送数据
        queryParams:{
          pageNum:1,
          pageSize:20,
        },
        //数据数量
        total:0,
        //数据数组
        tableList:[],
        //表格扩展选择器---需要在@selection-change绑定的方法内监控selection数组长度
        selectedNum:0,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        //化学品分类列表
        optionsListOne:[],
        //属性列表
        optionsListTwo:[],
        //学院列表
        optionsList:[{id:'1', name:"RFID",}, {id:'2', name:"二维码",}],
        //导入弹层开关
        importOpen:false,
        //导入数据
        upImportData:{
          sysUserEnable:true,
          upDate:false,
        },
        getImportData:{
          downFile:false,
          successNum:0,
          failureNum:0,
          textName:"",
        },
        //新增页面传参
        addPropsData:{},
        //MSDS页面传参
        msdsPropsData:{},
        //化学品存放页面传参
        listPropsData:{},
      };
    },
    created() {

    },
    mounted(){
      this.getDicts("hxp_classifyattribute").then(response => {
        this.optionsListTwo = response.data;
      })
      this.classifyList();
      this.getList();
    },
    methods: {
      //获取化学品分类列表
      classifyList(){
        classifyList().then(response => {
          this.optionsListOne = response.rows;
        });
      },
      //操作按钮
      pageToggle(type,item){
        if(type == 1){
          this.pageType = 1;
          if(item == 'refresh'){
            this.resetQuery();
          }else if(item == 'get'){
            this.getList();
          }
        }else if(type == 2){
          //新增
          this.$set(this,'addPropsData',{});
          this.pageType = 2;
        }else if(type == 3){
          //关联化学品柜
          console.log("item.id",item.id)
          this.listPropsData.id = item.id;
          this.listPropsData.chemicalName = item.chemicalName;
          this.listPropsData.chemicalUnit = item.chemicalUnit;
          this.pageType = 3;
        }else if(type == 4){
          //MSDS
          this.msdsPropsData.id = item.id;
          this.pageType = 4;
        }
        this.handleSelectionChangeDel();
      },
      moreClick(data){
        let self = this;
        if(data.command == 1){
          //编辑
          getHxpChemicalInfo(data.row.id).then(response => {
            this.addPropsData = response.data;
            this.pageType = 2;
          });
        }else if(data.command == 2){
          // this.msgError('该化学试剂有存放库存,无法删除,请先操作出库.');
          this.$confirm('确认要删除化学试剂信息吗?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            delHxpChemicalInfo(data.row.id).then(response => {
              if(response.code == 200){
                self.msgSuccess(response.msg)
                self.pageToggle(1,'get');
              }
            });
          }).then(() => {
          }).catch(() => {});
        }
      },
      //获取数据列表
      getList(){
        this.queryParamsData = JSON.parse(JSON.stringify(this.queryParams));
        hxpChemicalList(this.queryParamsData).then(response => {
          this.tableList = response.rows;
          this.total = response.total
        });
      },
      /** 搜索按钮操作 */
      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.handleQuery();
      },
      //导入页面关闭
      importOpenOff(){
        this.importOpen = false;
        this.getImportData.downFile = false;
        this.getImportData.successNum = 0;
        this.getImportData.failureNum = 0;
        this.getImportData.textName = "";
      },
      /** 导入按钮操作 */
      importButton(item){
        if(item.command == 1){
          //  下载模板
          this.download('/chemical/hxpChemical/importTemplate', {}, `导入模板.xlsx`)
        }else if(item.command == 2){
          //  导入数据
          console.log('导入数据');
          this.importOpen = true;
        }
      },
      /** 当前时间 */
      getCurrentTime () {
        const yy = new Date().getFullYear()
        const mm = new Date().getMonth() + 1
        const dd = new Date().getDate()
        const hh = new Date().getHours()
        const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
        const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
        return yy + '-' + mm + '-' + dd
      },
      /** 导出按钮操作 */
      exportButton(item) {
        let self = this;
        let currentDate = this.getCurrentTime()
        if(item.command == 1){
          self.$confirm(`确认导出全部数据?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(async () => {
            self.download('/chemical/hxpChemical/export/', {...self.queryParamsData}, '化学品信息-'+currentDate+'.xlsx')
          }).catch(() => {})
        }else if(item.command == 2){
          if(self.ids.length>0) {
            self.$confirm(`确认导出选中数据?`, "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(async () => {
              let ids = self.ids.join(',');
              let obj = {
                ids :ids
              }
              console.log("obj",obj)
              self.download(`/chemical/hxpChemical/export/`,obj, '化学品信息-'+currentDate+'.xlsx')
            }).catch(() => {})
          }else {
            this.msgError('请选择要导出的数据')
          }
        }
      },
      /** 下载模板操作 */
      importTemplate() {
        this.download('/chemical/hxpChemical/importTemplate', {}, `导入模板.xlsx`)
      },
      /*下载失败列表*/
      failureExcel(){
        this.download('/chemical/hxpChemical/importErrorData', {}, `失败报表.xlsx`)
      }
      ,//****************************************导入功能**************************************
      handleAvatarSuccess(res, file) {
        if(res.code == 200){
          if(res.data.downFile){
            this.getImportData.downFile = res.data.downFile;
            this.getImportData.successNum = res.data.successNum;
            this.getImportData.failureNum = res.data.failureNum;
          }else{
            this.msgSuccess(res.msg);
            this.importOpen = false;
            this.resetQuery();
          }
        }else{
          this.msgError(res.msg);
        }
        this.loading = false;
      },
      beforeAvatarUpload(file) {
        let type = false;
        console.log('file',file);
        if (file.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || file.type == 'application/vnd.ms-excel') {
          this.getImportData.textName = file.name;
          type = true;
          this.loading = true;
        }else{
          this.msgError('只能上传xls/xlsx格式文件')
          type = false;
        }
        return type;
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.selectedNum = selection.length;
        this.ids = selection.map(item => item.id);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      handleSelectionChangeDel(){
        this.selectedNum = 0;
        this.ids = [];
        this.single = true;
        this.multiple = true;
      },
      /*===记录勾选数据===
        需要再el-table 添加  :row-key="getRowKeys"
        需要在selection 添加 :reserve-selection="true"
      */
      getRowKeys(row) {
        return row.id
      },
    }
  }
</script>

<style scoped lang="scss">
  .ChemicalInfo{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden!important;
    p{
      margin:0;
      padding:0;
    }
    .ChemicalInfo-page{
      flex:1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      padding:20px;
      .form-box{
        .form-dropdown-box{
          display: flex;
          margin:0;
          padding:0 10px;
          cursor: pointer;
          height:40px;
          img:nth-child(1){
            width:16px;
            height:16px;
            margin-top:12px;
          }
          p{
            width:47px;
            text-align: center;
            font-size:14px;
            margin:0;
            line-height:40px;
          }
          img:nth-child(3){
            width:10px;
            height:6px;
            margin-top:17px;
          }
        }
      }
      .button-box{
        display: flex;
      }
    }
  }
</style>
<style lang="scss">
  .import-dialog-box{
    .import-dialog-upLoad-box{
      .el-upload{
        width:450px;
        margin:0 55px;
        .el-upload-dragger{
          width:450px;
          .el-icon-upload{
            font-size:100px;
            color:#CBE6FE;
          }
          .el-upload__text{
            margin-top:20px;
          }
        }
      }
      .el-upload__tip{
        margin-left:60px;
        font-size:14px;
        margin-top:10px;
      }
      .el-upload-text-box{
        background: #F5F5F5;
        border-radius: 10px;
        margin:15px 30px 0;
        padding:0 20px 15px;
        overflow: hidden;
        *{
          margin:0;
        }
        .img-text-box{
          display: flex;
          margin-top:15px;
          img{
            width:16px;
            height:16px;
            margin-right:13px;
          }
          p{
            height:16px;
            line-height:16px;
            font-size:12px;
          }
        }
        .text-p{
          margin-top:15px;
          margin-left:29px;
          height:16px;
          line-height:16px;
          font-size:12px;
        }
      }
    }
    .teacher-import-dialog-button-box{
      display: flex;
      width:190px;
      margin:0 auto;
      p{
        width:70px;
        height:30px;
        line-height:30px;
        font-size:14px;
        margin:0 auto;
      }
    }
    .teacher-import-dialog-position-box{
      width:100%;
      height:100%;
      position: absolute;
      top:0;
      left:0;
      z-index: 999;
      background: rgba(255,255,255,0.4);
      border-radius:20px;
    }
  }
</style>
