<!--危化品安全技术说明书-->
<template>
  <div class="app-container hazard-book">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="108px">
      <el-form-item label="危险品名称" prop="name">
        <el-input
          v-model="queryParams.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="handleAdd"
          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="left" prop="name" />
      <el-table-column label="编号" align="left" prop="code"/>
      <el-table-column label="创建时间" align="left" prop="createTime"/>
      <el-table-column label="查看次数" align="left" prop="scanCount"/>
      <el-table-column label="二维码" align="left" width="140">
        <template slot-scope="scope">
          <div @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">
            <p class="table-min-button"
               @click="handleUpdate(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 :page-sizes="[20, 30, 40, 50]"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改危化品安全技术说明书对话框 -->
    <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'

export default {
  components:{
    vueQr
  },
  name: "Hazard_book",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['laboratory:hazard_book:query','laboratory:hazard_book:edit','laboratory:hazard_book:remove']),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 危化品安全技术说明书表格数据
      hazard_bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        name: null,
        code: null,
        content: null,
        qrCodeUrl: null,
        scanCount: null,
        userId: null,
        deptId: null,
        deptName: null,
      },
      // 表单参数
      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: {
    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;
      listHazard_book(this.queryParams).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.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      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.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除危化品安全技术说明书编号为"' + ids + '"的数据项?', "警告", {
          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);
    padding:20px!important;
    .button-box{
      margin:0 auto;
      width:190px;
      display: flex;
    }
    .el-input--small{
      width:260px;
    }
  }
</style>
