<!--危化品安全技术说明书-->
<template>
  <div class="app-container hazard-book">
    <div class="hazard-book-page" v-if="pageType == 1">
      <el-form :model="queryParamsData" ref="queryForm" :inline="true" v-show="showSearch" label-width="108px">
        <el-form-item label="化学品名称" prop="name">
          <el-input
            v-model="queryParamsData.name"
            placeholder="请输入化学品名称"
            maxLength="20"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item style="float: right;">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="pageToggle(2)"
            v-hasPermi="['laboratory:hazard_book:add']"
          >新增</el-button>
        </el-form-item>
        <el-form-item>
          <p class="inquire-button-one" @click="handleQuery">查询</p>
          <p class="reset-button-one" @click="resetQuery">重置</p>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" border :data="hazard_bookList" @selection-change="handleSelectionChange">
        <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" width="200"/>
        <el-table-column label="查看次数" align="center" prop="scanCount" width="100"/>
        <el-table-column label="二维码" align="center" width="100">
        <template slot-scope="scope">
        <div style="height:50px;" @click="dialogQrCodeOn(scope.row.qrCodeUrl)">
          <vue-qr style="height:50px;width:50px;cursor:pointer;" :text="scope.row.qrCodeUrl" :size="200"></vue-qr>
        </div>
        </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160" v-if="tableButtonType">
          <template slot-scope="scope">
            <div class="button-box">
              <!--
              @click="pageToggle(3,scope.row)"
              @click="handleUpdate(scope.row)"
              -->
              <p class="table-min-button"
                 @click="pageToggle(3,scope.row)"
                 v-hasPermiAnd="['laboratory:hazard_book:query','laboratory:hazard_book:edit']"
              >编辑</p>
              <p class="table-min-button"
                 @click="handleDelete(scope.row)"
                 v-hasPermi="['laboratory:hazard_book:remove']"
              >删除</p>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!--新增页面-->
    <add-page v-if="pageType == 2" :addPropsData="addPropsData"></add-page>
    <!-- 添加或修改危化品安全技术说明书对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="化学品名称" prop="name" style="width:498px;">
          <el-input v-model="form.name" placeholder="请输入化学品名称" maxLength="20"/>
        </el-form-item>
        <el-form-item label="化学品编号" prop="code" style="width:498px;">
          <el-input v-model="form.code" placeholder="请输入化学品编号" maxLength="20"/>
        </el-form-item>
        <el-form-item label="化学品详情" prop="content" v-if="open">
          <!--<editor v-model="form.content" :min-height="192"/>-->
          <wangEditor :content="form.content" @change="change" :min-height="192"/>
          <!--<wangEditor v-model="form.content" @change="change" :min-height="192"/>-->
        </el-form-item>
        <!--
        <el-form-item label="二维码地址" prop="qrCodeUrl">
          <el-input v-model="form.qrCodeUrl" placeholder="请输入二维码地址" />
        </el-form-item>
        <el-form-item label="查看次数" prop="scanCount">
          <el-input v-model="form.scanCount" placeholder="请输入查看次数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 二维码展示 -->
    <el-dialog title="二维码" class="qr-codeUrl-dialog" :visible.sync="dialogQrCodeType" width="300px" append-to-body>
      <vue-qr style="display: block;height:200px;width:200px;cursor:pointer;margin:0 auto;" :text="dialogQrCodeUrl" :size="200"></vue-qr>
    </el-dialog>
  </div>
</template>

<script>
import { listHazard_book, getHazard_book, delHazard_book, addHazard_book, updateHazard_book } from "@/api/laboratory/hazard_book";
import vueQr from 'vue-qr'
import addPage from "./addPage.vue"

export default {
  components:{
    vueQr,
    addPage
  },
  name: "Hazard_book",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['laboratory:hazard_book:query','laboratory:hazard_book:edit','laboratory:hazard_book:remove']),
      //页面状态
      pageType:1,
      //新增页面传参
      addPropsData:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 危化品安全技术说明书表格数据
      hazard_bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 搜索数据
      queryParamsData:{
        pageNum:1,
        pageSize:10,
      },
      // 搜索实际发送数据
      queryParams:{
        pageNum:1,
        pageSize:10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name:[
          {required: true, message: '请输入危险品名称', trigger: 'blur'},
          { required: true, message: "请输入危险品名称", validator: this.spaceJudgment, trigger: "blur" }
        ],
        code:[
          {required: true, message: '请输入编号', trigger: 'blur'},
          { required: true, message: "请输入危险品名称", validator: this.spaceJudgment, trigger: "blur" }
        ],
        content:[
          {required: true, message: '请输入详情', trigger: 'blur'},
          { required: true, message: "请输入危险品名称", validator: this.spaceJudgmentHTML, trigger: "blur" }
        ],
      },
      //二维码展示数据
      dialogQrCodeType:false,
      dialogQrCodeUrl:"",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 页面状态切换
    pageToggle(type,item){
      if(type == 0){
        this.pageType = 1;
      }else if(type == 1){
        this.pageType = 1;
        this.getList();
      }else if(type == 2){
        //新增
        this.addPropsData = {};
        this.pageType = 2;
      }else if(type == 3){
        //编辑
        getHazard_book(item.id).then( response => {
          this.addPropsData =  response.data;
          this.addPropsData.content = unescape(response.data.content);
          this.pageType = 2;
        });
      }
    },
    dialogQrCodeOn(url){
      this.dialogQrCodeUrl = url;
      this.dialogQrCodeType = true;
    },
    change(val) {
      this.$set(this.form,'content',val);
      // this.form.content = val;
      console.log(val)
    },
    /** 查询危化品安全技术说明书列表 */
    getList() {
      this.loading = true;
      this.queryParamsData = JSON.parse(JSON.stringify(this.queryParams));
      listHazard_book(this.queryParamsData).then( response => {
        this.hazard_bookList =  response.rows;
        this.total =  response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        content: null,
        qrCodeUrl: null,
        scanCount: null,
        userId: null,
        createBy: null,
        updateBy: null,
        deptId: null,
        deptName: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParamsData.pageNum = 1;
      this.queryParamsData.pageSize = 10;
      this.queryParams = JSON.parse(JSON.stringify(this.queryParamsData));
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$set(this,'queryParamsData',{});
      this.$set(this,'queryParams',{});
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHazard_book(id).then( response => {
        this.form =  response.data;
        this.form.content = unescape(response.data.content);
        this.open = true;
        this.title = "编辑";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.content = escape(this.form.content);
          if (this.form.id != null) {
            updateHazard_book(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHazard_book(this.form).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.resetQuery();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delHazard_book(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/hazard_book/export', {
        ...this.queryParams
      }, `laboratory_hazard_book.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
  .hazard-book {
    display: flex !important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    .hazard-book-page{
      flex:1;
      display: flex !important;
      flex-direction: column;
      padding:20px!important;
      overflow: hidden;
    }
    .button-box{
      margin:0 auto;
      width:190px;
      display: flex;
    }
    .el-input--small{
      width:260px;
    }
  }
</style>
