<!--申购-->
<template>
  <div class="listPage">
    <div class="listPage-min" v-show="pageType == 1">
      <el-form :model="queryParamsData" class="form-box" ref="queryForm" :inline="true">
        <el-form-item label="关键字" prop="searchValue">
          <el-input
            style="width:210px;"
            maxLength="30"
            v-model="queryParamsData.searchValue"
            placeholder="化学品名/别名/CAS号"
            clearable
            size="small"
          />
        </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>
          <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="inquire-button-one"
               @click="goPage(2)"
            >确定</p>
          </el-col>
        </el-form-item>
        <el-form-item style="float: right;" v-if="!addPagePropsData.type">
          <el-col :span="1.5">
            <p class="reset-button-one"
               @click="backPage(0)"
            >返回</p>
          </el-col>
        </el-form-item>
        <el-form-item style="float: right;" v-if="addPagePropsData.type">
          <el-col :span="1.5">
            <p class="reset-button-one"
               @click="goPage(4)"
            >取消</p>
          </el-col>
        </el-form-item>
      </el-form>
      <!--<el-table  border :data="tableList" ref="multipleTable" @selection-change="handleSelectionChange" :row-key="getRowKeys">-->
      <el-table  border :data="tableList" ref="multipleTable" @select-all="handleSelectionChange" @select="handleSelectionChange" :row-key="getRowKeys">
        <el-table-column type="selection" width="50" align="center" :reserve-selection="true"/>
        <el-table-column label="ID" align="center" prop="chemicalNum" show-overflow-tooltip/>
        <el-table-column label="化学品名" align="center" prop="chemicalName" show-overflow-tooltip/>
        <el-table-column label="CAS号" align="center" prop="casNum" show-overflow-tooltip/>
        <el-table-column label="分类" align="center" prop="classifyName" show-overflow-tooltip/>
        <el-table-column label="属性" align="center" prop="classifyAttribute" show-overflow-tooltip/>
        <el-table-column label="形态" align="center" prop="chemicalShapeInfo" show-overflow-tooltip/>
        <el-table-column label="纯度" align="center" prop="purity" show-overflow-tooltip/>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page-sizes="[20, 30, 40, 50]"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <add-page :addPagePropsData="addPagePropsData"
              :multipleTableList="multipleTableList"
              :editPropsData="editPropsData"
              v-if="pageType == 2"></add-page>
  </div>
</template>

<script>
  import { classifyList } from "@/api/medicUniversity-3_1/index";
  import { hxpChemicalList } from "@/api/studentApi/chemicalManagement/index";
  import addPage from "./addPage.vue"
  export default {
    name: "listPage",
    components: {
      addPage,
    },
    data(){
      return{
        // 遮罩层
        loading:false,
        //页面状态
        pageType:1,
        // 分类
        optionsListOne:[],
        // 属性
        optionsListTwo:[],
        // 创建时间
        dateRange:[],
        // 搜索数据
        queryParamsData:{
          pageNum:1,
          pageSize:20,
        },
        // 搜索实际发送数据
        queryParams:{
          pageNum:1,
          pageSize:20,
        },
        //数据数量
        total:0,
        //数据数组
        tableList:[],
        // 选中用户组
        userIds: [],
        // 非多个禁用
        multiple: true,
        //表格扩展选择器---需要在@selection-change绑定的方法内监控selection数组长度
        selectedNum:0,
        //新增页参数
        addPagePropsData:{},
        //勾选数据
        multipleTableList:[],
        multipleTableListOne:[],
        //编辑参数
        editPropsData:{},
      }
    },
    created() {

    },
    mounted(){
      this.getDicts("hxp_classifyattribute").then(response => {
        this.optionsListTwo = response.data;
      })
      this.classifyList();
      this.getList();
    },
    methods: {
      //确定申购
      goPage(type,data){
        let self = this;
        if(type==1){
          this.pageType = 1;
          this.$set(this,'addPagePropsData',{});
          this.$set(this,'multipleTableList',[]);
          this.$refs.multipleTable.clearSelection()
        }else if(type == 2){
          if(!this.multipleTableList[0]){
            this.msgError("请勾选化学品")
            return
          }
          this.$set(this,'multipleTableListOne',this.multipleTableList);
          this.pageType = 2;
        }else if(type == 3){
          this.$set(this,'addPagePropsData',JSON.parse(JSON.stringify(data)));
          this.pageType = 1;
        }else if(type == 4){
          this.$refs.multipleTable.clearSelection();
          for(let o=0;o<self.multipleTableListOne.length;o++){
            self.$refs.multipleTable.toggleRowSelection(self.multipleTableListOne[o],true);
          }
          this.$set(this,'multipleTableList',this.multipleTableListOne);
          this.pageType = 2;
        }
      },
      //获取数据列表
      getList(){
        this.queryParamsData = JSON.parse(JSON.stringify(this.queryParams));
        hxpChemicalList(this.queryParamsData).then(response => {
          this.total = response.total;
          this.tableList = response.rows;
        });
      },
      //获取化学品分类列表
      classifyList(){
        classifyList().then(response => {
          this.optionsListOne = response.rows;
        });
      },
      /** 搜索按钮操作 */
      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();
      },
      //返回
      backPage(type){
        if(type == 0){
          this.$parent.pageToggle(0);
        }else if(type == 1){
          this.$parent.pageToggle(1);
        }
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        let self = this;
        this.$refs.multipleTable.clearSelection();
        let list = [];
        for(let i=0;i<selection.length;i++){
          if(i>19){
            self.$refs.multipleTable.toggleRowSelection(selection[i],false);
          }else{
            list.push(selection[i])
            self.$refs.multipleTable.toggleRowSelection(selection[i],true);
          }
        }
        this.$set(this,'multipleTableList',list);
        if(!this.addPagePropsData.type){
          this.$set(this,'multipleTableListOne',list);
        }
        if (selection.length>20){
          this.msgError("一次最多可申购20个化学品")
        }
      },
      /*===记录勾选数据===
        需要再el-table 添加  :row-key="getRowKeys"
        需要在selection 添加 :reserve-selection="true"
      */
      getRowKeys(row) {
        return row.id
      },
    }
  }
</script>

<style scoped lang="scss">
  .listPage{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    p{
      margin:0;
      padding:0;
    }
    .listPage-min{
      flex:1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      padding:20px;
      .button-box{
        display: flex;
      }
    }
  }
</style>
