<!-- 信标列表 -->
<template>
  <div class="app-container beaconList">
    <div class="page-container beaconListPage">
      <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="searchValue" label-width="60px">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="编号"
              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-add-common-style-button"
             style="float: right;"
             @click="tableButton(3)"
             v-hasPermi="['db:beacon:add']"
          >+  新增信标</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList">
          <el-table-column label="序号"  width="50" align="center"  type="index"/>
          <el-table-column label="信标编号" prop="beaconTag"  show-overflow-tooltip/>
          <el-table-column label="最后使用时间" prop="bindingTime" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.bindingTime,'{y}-{m}-{d} {h}:{i}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" 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(1,scope.row)"
                   v-hasPermi="['db:beacon:query']"
                >打印二维码</p>
                <p class="table-button-p"
                   @click="tableButton(2,scope.row)"
                   v-hasPermi="['db:beacon:remove']"
                >删除</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="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <el-dialog class="beaconList-dialog-box"
      :title="dialogTitle" :visible.sync="dialogType" v-if="dialogType"
      :close-on-click-modal="false" width="500px" append-to-body>
      <div v-if="!dialogAddType">
        <div v-if="dialogForm.state">
          <div class="text-box">
            <p>学院：</p>
            <p>{{dialogForm.collegeName}}</p>
          </div>
          <div class="text-box">
            <p>实验室：</p>
            <p>{{dialogForm.subjectName}}{{dialogForm.roomNum}}</p>
          </div>
          <!--<div class="text-box">-->
            <!--<p>气瓶编号：</p>-->
            <!--<p>{{dialogForm.roomNum}}</p>-->
          <!--</div>-->
          <div class="text-box">
            <p>气瓶：</p>
            <p>{{dialogForm.gasName}}</p>
          </div>
        </div>
        <div class="text-box" v-if="dialogForm.state">
          <p>最后一次绑定时间：</p>
          <p>{{ parseTime(dialogForm.bindingTime,'{y}-{m}-{d} {h}:{i}') }}</p>
        </div>
        <vue-qr style="display: block;height:100px;width:100px;margin:auto;" text="dialogForm.qrCodeUrl" :size="300"></vue-qr>
        <p class="text-p">{{dialogForm.beaconTag}}</p>
        <p class="text-p">信标</p>
      </div>
      <div v-if="dialogAddType">
        <el-form ref="dialogForm" :model="dialogForm" :rules="rules" label-width="120px">
          <el-form-item label="信标编号：" prop="beaconTag" style="margin-top:20px;">
            <el-input
              maxLength="20"
              v-model="dialogForm.beaconTag"
              placeholder="请输入名称"
              style="width: 280px"
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <p class="dialog-footer-null-p"></p>
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit" :style="dialogAddType?'width:150px;':''">{{dialogAddType?'确认并打印二维码':'打 印'}}</el-button>
        <p class="dialog-footer-null-p"></p>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import vueQr from 'vue-qr'
  import { getLodop } from "@/utils/LodopFuncs";
  import { airbottleBeaconList,airbottleBeaconAdd,airbottleBeaconFindById,airbottleBeaconDelete } from "@/api/gasBottleManage/index";
  //import { getDicts } from "@/api/commonality/noPermission";
  //import { systemUserSelect } from "@/api/commonality/permission";
  //import { getInfo } from "@/api/basicsModules/index";
  export default {
    name: 'index',
    components: {
      vueQr
    },
    data () {
      return {
        tableButtonType:this.hasPermiDom(['db:beacon:remove','db:beacon:query',]),
        //页面遮罩
        loading:false,
        //下拉列表数据
        optionList:[{value:true,label:'启用'},{value:false,label:'停用'}],
        //查询条件
        queryParams:{
          pageNum:1,
          pageSize:20,
          searchValue:"",
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //弹窗开关
        dialogType:false,
        dialogTitle:"",
        dialogForm:null,
        //打印/新增状态
        dialogAddType:false,
        //校验
        rules:{
          beaconTag:[
            { required: true, message: '请输入信标编号', trigger: 'blur' },
            { required: true, message: "请输入信标编号", validator: this.spaceJudgment, trigger: "blur" }
          ],
        },
      }
    },
    created () {

    },
    mounted () {
      this.getList();
    },
    methods: {
      //弹窗相关
      cancel(){
        this.$set(this,'dialogType',false);
      },
      //打印二维码
      submit(){
        let _this=this;
        if(!this.dialogAddType){
          if(this.needCLodop()){
            //打印
            if(this.dialogForm.beaconTag[0]){
              // let data = JSON.parse(JSON.stringify({code:this.dialogForm.beaconTag}))
              // let num = parseInt((20 - data.code.length)/2);
              // for(let i=0;i<num;i++){
              //   data.code = ' '+data.code+' ';
              // }
              this.CreateOneFormPage(this.dialogForm.beaconTag);
              this.$set(this,'dialogType',false);
            }else{
              this.msgError('参数异常')
            }
          }else{
            //提示驱动缺失
            this.$set(this,'dialogType',false);
            this.$confirm('未检测到打印服务，请联系管理员.', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function() {
            }).then(() => {
            }).catch(() => {});
          }
        }else{
          this.$refs["dialogForm"].validate(valid => {
            if (valid) {
              let obj = JSON.parse(JSON.stringify(this.dialogForm))
              airbottleBeaconAdd(obj).then(response => {
                this.$set(this,'dialogType',false);
                this.msgSuccess(response.msg)
                this.getList();
                if(this.needCLodop()){
                  //打印
                  if(obj.beaconTag[0]){
                    // let data = JSON.parse(JSON.stringify({code:obj.beaconTag}))
                    // let num = parseInt((20 - data.code.length)/2);
                    // for(let i=0;i<num;i++){
                    //   data.code = ' '+data.code+' ';
                    // }
                    this.CreateOneFormPage(obj.beaconTag);
                    this.$set(this,'dialogType',false);
                  }else{
                    this.msgError('参数异常')
                  }
                }else{
                  //提示驱动缺失
                  this.$set(this,'dialogType',false);
                  this.$confirm('未检测到打印服务，请联系管理员.', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                  }).then(function() {
                  }).then(() => {
                  }).catch(() => {});
                }
              });
            }
          })
        }
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'pageNum',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'dateRange',[])
        this.$set(this,'queryParams',{
          pageNum:1,
          pageSize:20,
          searchValue:"",
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        airbottleBeaconList(obj).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){
          //打印二维码
          airbottleBeaconFindById({id:row.id}).then(response => {
            this.$set(this,'dialogTitle','标签信息');
            this.$set(this,'dialogForm',response.data);
            this.$set(this,'dialogAddType',false);
            this.$set(this,'dialogType',true);
          });
        }else if(type == 2){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            airbottleBeaconDelete({id:row.id}).then(response => {
              self.msgSuccess(response.msg)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 3){
          //新增
          this.$set(this,'dialogTitle','新增');
          this.$set(this,'dialogForm',{beaconTag:""});
          this.$set(this,'dialogAddType',true);
          this.$set(this,'dialogType',true);
        }
      },
    },
  }
</script>
<style scoped lang="scss">
  .beaconList{
    display: flex;
    flex-direction: column;
    .beaconListPage{

    }
  }
</style>
<style lang="scss">
  .beaconList-dialog-box{
    .el-dialog__body{
      padding:10px 20px;
      .text-box{
        display: flex;
        p{
          font-size:16px;
          line-height:40px;
        }
        p:nth-child(1){
          margin-left:60px;
        }
        p:nth-child(2){

        }
      }
      .text-p{
        line-height:30px;
        font-size:16px;
        text-align: center;
      }
    }
    .dialog-footer{
      display: flex;
      .dialog-footer-null-p{
        flex:1;
      }
    }
  }
</style>
