<template>
  <div class="msdsPage">
    <div class="msds-page" v-show="!lookOpen">
      <div class="page-top-title-box">
        <p class="page-top-title-name-p" style="margin-left:20px;">MSDS化学品安全技术说明书</p>
        <p class="page-top-title-out-p" @click="backPage">返回</p>
        <p class="page-top-title-submit-p" style="margin-left:20px;" @click="upData">提交</p>
      </div>
      <div class="msdsPage-min">
        <div class="page-form-title-box" style="padding:20px 5px 0 0;">
          <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="page-inquire-common-style-button" @click="handleQuery" style="margin-right:10px;">查询</p>
              <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
            </el-form-item>
          </el-form>
        </div>
        <div class="page-content-box" style="padding: 0 0 20px 0;">
          <el-table class="table-box" 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="createByName"/>
            <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 class="selected-num-box" v-show="total>0">
            <p class="selected-num-p">
              <i class="el-icon-warning"></i>
              已选择 {{selectioned.id?"1":"0"}} 项
            </p>
            <pagination :page-sizes="[20, 30, 40, 50]"
                        :total="total"
                        style="margin:0;"
                        :page.sync="queryParams.page"
                        :limit.sync="queryParams.pageSize"
                        @pagination="getList"
            />
          </div>
        </div>
      </div>
    </div>
    <!--查看窗口-->
    <msds-info v-if="lookOpen" :dialogData="dialogData"></msds-info>
  </div>
</template>

<script>
  import { chemicalMsdsDetailByMsds,chemicalChemicalUpdateMsds,chemicalMsdsDetail } from "@/api/chemicalManage/index";
  import msdsInfo from "./msdsInfo.vue"
  export default {
    name: "msdsPage",
    components: {
      msdsInfo
    },
    props:{
      propsData:{},
    },
    data() {
      return {
        // 搜索数据
        queryParamsData:{
          page:1,
          pageSize:20,
        },
        // 搜索实际发送数据
        queryParams:{
          page: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 = {
          chemicalId:this.propsData.chemicalId,
        }
        if(this.selectioned.id){
          obj.msdsId = this.selectioned.id;
        }else{
          obj.msdsId = "";
        }
        chemicalChemicalUpdateMsds(obj).then(response => {
          if(response.code == 200){
            this.msgSuccess(response.message);
            this.$parent.tableButton(5);
          }
        });
      },
      //查看按钮
      lookItem(type,row){
        if(type){
          chemicalMsdsDetail({id: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.propsData.chemicalId;
        chemicalMsdsDetailByMsds(this.queryParamsData).then(response => {
          this.tableList = response.data.records;
          this.total = response.data.total;
          for (let i=0;i<response.data.records.length;i++){
            if (response.data.records[i].msdsId){
              setTimeout(function(){
                self.$set(self,"selectioned",response.data.records[i]);
                self.$refs.multipleTable.toggleRowSelection(response.data.records[i], true);
              },50);
            }else{
              self.$set(self,"selectioned",{});
            }
          }
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParamsData.page = 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) {
        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.tableButton(5);
      },
      /*===记录勾选数据===
        需要再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:0 20px;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        .page-form-title-box{
          border:none;
        }
      }
    }
  }
</style>
