<!--危化品安全技术说明书-->
<template>
  <div class="app-container msdsSpecification">
    <div class="page-container msdsSpecificationPage" v-if="pageType === 1">
      <div class="page-form-title-box">
        <el-form :model="queryParamsData" ref="queryForm" :inline="true" v-show="showSearch" label-width="108px">
          <el-form-item label="" prop="searchValue">
            <el-input
              v-model="queryParamsData.searchValue"
              placeholder="请输入化学品名称/编号"
              maxLength="20"
              size="small"
            />
          </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="pageToggle(2)"
             v-hasPermiRouter="['chemical:msds:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="hazard_bookList">
          <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" 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:23px;" @click="dialogQrCodeOn(scope.row)">
                <vue-qr style="height:23px;width:23px;cursor:pointer;" :text="identification+'?code='+scope.row.id+'&type=1'" :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="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="pageToggle(3,scope.row)"
                   v-hasPermiRouter="['chemical:msds:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="handleDelete(scope.row)"
                   v-hasPermiRouter="['chemical:msds:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParamsData.page"
                    :limit.sync="queryParamsData.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <!--新增页面-->
    <add-page v-if="pageType == 2" :addPropsData="addPropsData"></add-page>
    <!-- 二维码展示 -->
    <qr-code-dialog ref="qrCodeDialog" :qrCodeDialogData="qrCodeDialogData"></qr-code-dialog>
  </div>
</template>

<script>
  import qrCodeDialog from "@/components/qrCodeDialog/index.vue"
import { chemicalMsdsList,chemicalMsdsAdd,chemicalMsdsEdit,chemicalMsdsDetail,chemicalMsdsDel } from "@/api/chemicalManage/index";
import vueQr from 'vue-qr'
import addPage from "./addPage.vue"

export default {
  components:{
    vueQr,
    addPage,
    qrCodeDialog,
  },
  name: "Hazard_book",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['chemical:msds:edit','chemical:msds:detail','chemical:msds:del']),
      //页面状态
      pageType:1,
      //新增页面传参
      addPropsData:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 危化品安全技术说明书表格数据
      hazard_bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 搜索数据
      queryParamsData:{
        searchValue:'',
        page:1,
        pageSize:20,
      },
      // 表单参数
      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" }
        ],
      },
      //二维码展示数据
      qrCodeDialogData:{},
      //二维码规则 服务器域名,需与微信后台开发配置内一致.
      identification: localStorage.getItem('codeOnlineAdd'),
    };
  },
  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){
        //编辑
        chemicalMsdsDetail({id:item.id}).then( response => {
          this.addPropsData =  response.data;
          this.addPropsData.content = unescape(response.data.content);
          this.pageType = 2;
        });
      }
    },
    //展示二维码
    dialogQrCodeOn(row){
      this.$set(this,'qrCodeDialogData',{
        download:true,
        type:'1',                                                     //二维码类型 用于区分二维码功能类型
        parameter:row.id,                                             //二维码参数
        name:row.name,                                                //二维码名称用于下载时命名
        codeName:row.name,                                            //二维码图片下名称展示(非必传)
      });
      this.$refs['qrCodeDialog'].initialize();
    },
    change(val) {
      this.$set(this.form,'content',val);
      // this.form.content = val;
    },
    /** 查询危化品安全技术说明书列表 */
    getList() {
      this.loading = true;
      chemicalMsdsList(this.queryParamsData).then( response => {
        this.hazard_bookList =  response.data.records;
        this.total =  response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParamsData.page = 1;
      this.queryParamsData.pageSize = 20;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$set(this,'queryParamsData',{
        searchValue:'',
        page:1,
        pageSize:20,
      });
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let self = this;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          chemicalMsdsDel({id:row.id}).then( response => {
            self.msgSuccess(response.message);
            self.getList();
          });
        }).then(() => {
        }).catch(() => {});
    },
  }
};
</script>

<style scoped lang="scss">
  .msdsSpecification {
    .msdsSpecificationPage{
      .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>
