<template>
  <div class="msdsPage">
    <div class="msds-page" v-if="!lookOpen">
      <div class="title-box">
        <p>MSDS化学品安全技术说明书</p>
        <p class="reset-button-one" @click="backPage"><i class="el-icon-arrow-left"></i>返回</p>
      </div>
      <div class="msdsPage-min">
        <el-form :model="queryParamsData" class="form-box" ref="queryForm" :inline="true">
          <el-form-item label="关键字" prop="name">
            <el-input
              maxLength="20"
              v-model="queryParamsData.name"
              placeholder="危险品名称"
              clearable
              size="small"
              style="width: 200px"
            />
          </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="add-button-one-90"
                 @click="upData"
              >确定</p>
            </el-col>
          </el-form-item>
        </el-form>
        <el-table  border :data="tableList" ref="multipleTable" :row-key="getRowKeys"
                   tooltip-effect="dark" @select-all="dialogCheck" @select="dialogCheck">
          <el-table-column type="selection" width="50" align="center" :reserve-selection="true"/>
          <el-table-column label="序号" type="index" align="center" width="50"/>
          <el-table-column label="化学品名称" align="center" prop="name"/>
          <el-table-column label="编号" align="center" prop="code"/>
          <el-table-column label="创建时间" align="center" prop="createTime"/>
          <el-table-column label="创建人" align="center" prop="createBy"/>
          <el-table-column label="操作" align="center" width="140" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p" @click="lookItem(true,scope.row)">查看</p>
                <p class="table-button-null"></p>
              </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>
            已选择 {{selectioned.id?"1":"0"}} 项
          </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>
    </div>
    <!--查看窗口-->
    <msds-info v-if="lookOpen" :dialogData="dialogData"></msds-info>
    <!--<el-dialog :title="dialogTitle" :visible.sync="lookOpen" width="1200px" append-to-body class="w-e-text-dialog-box">-->
      <!--<div style="width:1160px;max-height:700px;overflow-y: scroll;" class="scrollbar-box">-->
        <!--<div class="w-e-text-min-box">-->
          <!--<p>化学品名称：</p>-->
          <!--<p>{{dialogData.name}}</p>-->
        <!--</div>-->
        <!--<div class="w-e-text-min-box">-->
          <!--<p>说明书编号：</p>-->
          <!--<p>{{dialogData.code}}</p>-->
        <!--</div>-->
        <!--<div class="w-e-text-min-box-one">-->
          <!--<p>说明书详情：</p>-->
          <!--<p class="w-e-text" v-html="dialogData.content"></p>-->
        <!--</div>-->
      <!--</div>-->
      <!--<div slot="footer" class="teacher-import-dialog-button-box">-->
        <!--<p class="inquire-button-one" @click="lookItem(false)">确定</p>-->
      <!--</div>-->
    <!--</el-dialog>-->
  </div>
</template>

<script>
  import { getHazard_book } from "@/api/laboratory/hazard_book";
  import { getHazardBookJoinChemical,editChemicalByJoinHazard } from "@/api/medicUniversity-3_1/index";
  import msdsInfo from "./msdsInfo.vue"
  export default {
    name: "msdsPage",
    components: {
      msdsInfo
    },
    props:{
      msdsPropsData:{},
    },
    data() {
      return {
        // 搜索数据
        queryParamsData:{
          pageNum:1,
          pageSize:20,
        },
        // 搜索实际发送数据
        queryParams:{
          pageNum:1,
          pageSize:20,
        },
        //数据数量
        total:0,
        //数据数组
        tableList:[],
        //表格扩展选择器---需要在@selection-change绑定的方法内监控selection数组长度
        selectedNum:0,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        selectioned:{},
        //查看相关
        dialogTitle:"",
        dialogData:{},
        lookOpen:false,
      }
    },
    created() {

    },
    mounted(){
      this.getList();
    },
    methods:{
      //提交
      upData(){
        let obj = {
          id:this.msdsPropsData.id,
        }
        if(this.selectioned.id){
          obj.joinHazardId = this.selectioned.id;
        }else{
          obj.joinHazardId = "";
        }
        editChemicalByJoinHazard(obj).then(response => {
          if(response.code == 200){
            this.msgSuccess(response.msg);
            this.$parent.pageToggle(1,'get');
          }
        });
      },


      //查看按钮
      lookItem(type,row){
        if(type){
          getHazard_book(row.id).then( response => {
            this.dialogData.dialogTitle = row.name + '-安全技术说明书';
            this.dialogData.name = response.data.name;
            this.dialogData.code = response.data.code;
            this.dialogData.content = unescape(response.data.content);
            this.lookOpen = true;
          });
        }else{
          this.lookOpen = false;
        }
      },
      getList(){
        let self = this;
        this.queryParamsData = JSON.parse(JSON.stringify(this.queryParams));
        this.queryParamsData.chemicalId = this.msdsPropsData.id;
        getHazardBookJoinChemical(this.queryParamsData).then(response => {
          this.tableList = response.rows;
          this.total = response.total
          if(response.rows[0].joinHazardId&&this.queryParamsData.pageNum == 1){
            setTimeout(function(){
              self.$set(self,"selectioned",response.rows[0]);
              self.$refs.multipleTable.toggleRowSelection(response.rows[0], true);
            },50);
          }else{
            self.$set(self,"selectioned",{});
          }
        });
      },
      /** 搜索按钮操作 */
      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();
      },
      dialogCheck (selection, row) {
        console.log("selection",selection)
        console.log("row",row)
        this.$refs.multipleTable.clearSelection()
        if (selection.length === 0) {
          // 判断selection是否有值存在
          this.selectioned = {};
          return
        }
        if (row) {
          this.selectioned = row
          this.$refs.multipleTable.toggleRowSelection(row, true)
        }
      },
      backPage(){
        this.$parent.pageToggle(1);
      },
      /*===记录勾选数据===
        需要再el-table 添加  :row-key="getRowKeys"
        需要在selection 添加 :reserve-selection="true"
      */
      getRowKeys(row) {
        return row.id
      },
    }
  }
</script>

<style scoped lang="scss">
  .msdsPage{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    font-weight:500;
    p{
      margin:0;
      padding:0;
    }
    .msds-page{
      flex:1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      .title-box{
        display: flex;
        height:90px;
        border-bottom: 1px solid #D8D8D8;
        p:nth-child(1){
          flex:1;
          font-size:16px;
          line-height:90px;
          margin-left:18px;
          color:#0045AF;
        }
        p:nth-child(2){
          margin:25px 25px 0 0;
        }
      }
      .msdsPage-min{
        flex:1;
        padding:20px;
        display: flex;
        flex-direction: column;
        overflow: hidden;
      }
    }
  }
</style>
