<!-- 化学品信息 -->
<template>
  <div class="app-container chemicalInfo">
    <div class="page-container chemicalInfoPage" v-if="pageType === 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="chemicalCategory">
            <el-select v-model="queryParams.chemicalCategory" clearable placeholder="请选择类别" style="width: 200px">
              <el-option
                v-for="dict in optionListA"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="chemicalLevel">
            <el-select v-model="queryParams.chemicalLevel" clearable placeholder="请选择级别" style="width: 200px">
              <el-option
                v-for="dict in optionListB"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="searchValue" label-width="50px">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="化学品/别名/CAS号"
              clearable
              style="width: 200px"
            />
          </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;"
             @click="tableButton(1)"
             v-hasPermiRouter="['chemical:info:add']"
          >新增</p>
          <el-form-item style="float: right;">
            <export-component :exportConfig="exportConfig" v-hasPermiRouter="['chemical:info:export']"></export-component>
          </el-form-item>
          <el-form-item style="float: right;">
            <import-component :importConfig="importConfig" v-hasPermiRouter="['chemical:info:import']"></import-component>
          </el-form-item>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList"
                  ref="table-box" @selection-change="handleSelectionChange" :row-key="getRowKeys">
          <el-table-column type="selection" width="50" :reserve-selection="true" align="center"/>
          <el-table-column label="名称" prop="chemicalName"  show-overflow-tooltip/>
          <el-table-column label="CAS号" prop="casNum" width="200" show-overflow-tooltip/>
          <el-table-column label="类别" prop="chemicalCategoryName" width="200" show-overflow-tooltip/>
          <el-table-column label="级别" prop="chemicalLevelName" width="200" show-overflow-tooltip/>
          <el-table-column label="纯度" prop="chemicalPurity" width="200" show-overflow-tooltip/>
          <el-table-column label="创建时间" prop="createTime" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="230" show-overflow-tooltip v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="tableButton(2,scope.row)"
                   v-hasPermiRouter="['chemical:bindmsds:list']"
                >MSDS</p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['chemical:info:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(4,scope.row)"
                   v-hasPermiRouter="['chemical:info:del']"
                >删除</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 :propsData="propsData" v-if="pageType === 2"></add-page>-->
    <add-chemicals ref="addChemicals"></add-chemicals>
    <msds-page v-if="pageType == 2" :propsData="propsData"></msds-page>
  </div>
</template>
<script>
  import { getDicts } from "@/api/commonality/noPermission";
  import { chemicalChemicalList,chemicalChemicalDelete } from "@/api/chemicalManage/index";
  import addChemicals from "@/views/chemicalManage/components/addChemicals.vue";
  import importComponent from "@/components/importComponent/importComponent.vue";
  import exportComponent from "@/components/exportComponent/exportComponent.vue";
  import msdsPage from "./msdsPage.vue"
  export default {
    name: 'index',
    components: {
      addChemicals,
      exportComponent,
      importComponent,
      msdsPage
    },
    data () {
      return {
        //导出配置
        exportConfig:{
          api:'/chemical/chemical/export',             //导出接口地址
          ids:'',                                       //勾选导出,勾选的IDS
          fileName:'化学品信息',                         //导出文件的命名
        },
        //导入配置
        importConfig:{
          upLoadApi:'/chemical/chemical/importData',                //上传接口地址
          downloadApi:'/chemical/chemical/importTemplate',          //下载模板接口地址
          loseApi:'/chemical/chemical/importErrorData',             //失败报表接口地址
          fileName:'化学品导入模板',                                   //下载模板命名
        },
        tableButtonType:this.hasPermiDom(['chemical:bindmsds:list','chemical:info:detail','chemical:info:edit','chemical:info:del']),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //下拉列表数据
        optionListA:[{value:true,label:'启用'},{value:false,label:'停用'}],
        optionListB:[{value:1,label:'管控'},{value:2,label:'非管控'}],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:"",
          chemicalCategory :null,
          chemicalLevel :null,
        },
        selectedNum:0,
        //列表数据
        dataList:[],
        //数据数量
        total:10,
        //组件传参
        propsData:{},
      }
    },
    created () {
      this.getDicts();
      this.getList();
    },
    mounted () {

    },
    methods: {
      //获取化学品类别
      getDicts(){
        getDicts('chemicals_category').then(response => {
          this.$set(this,'optionListA',response.data);
        });
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.$refs['table-box'].clearSelection();
        this.$set(this.exportConfig,'ids','');
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$refs['table-box'].clearSelection();
        this.$set(this,'selectedNum',0);
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:"",
          chemicalCategory :null,
          chemicalLevel :null,
        });
        this.$set(this.exportConfig,'ids','');
        this.getList();
      },
      //化学品添加成功
      submitSuccessful(){
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        chemicalChemicalList(this.queryParams).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //操作按钮
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //新增
          this.$refs.addChemicals.addChemicalsOpen('1');
        }else if(type == 2){
          //MSDS
          let obj = JSON.parse(JSON.stringify(row))
          this.$set(this,'propsData',obj);
          this.$set(this,'pageType',2);
        }else if(type == 3){
          //编辑
          this.$refs.addChemicals.addChemicalsOpen('2',row);
        }else if(type == 4){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            chemicalChemicalDelete({chemicalId:row.chemicalId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 5){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.$set(this.queryParams,'selectedNum',selection.length);
        this.$set(this.exportConfig,'ids',selection.map(item => item.chemicalId));
      },
      /*===记录勾选数据===
        需要再el-table 添加  :row-key="getRowKeys"
        需要在selection 添加 :reserve-selection="true"
      */
      getRowKeys(row) {
        return row.chemicalId
      },
    },
  }
</script>
<style scoped lang="scss">
  .chemicalInfo{
    .chemicalInfoPage{

    }
  }
</style>
