<!-- 硬件分类 -->
<template>
  <div class="app-container hardwareClassification">
    <div class="page-container hardwareClassificationPage" v-if="pageType === 1">
      <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">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="请输入名称/标识"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="state">
            <el-select v-model="queryParams.state" placeholder="请选择状态" style="width: 200px">
              <el-option
                v-for="dict in optionList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </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="dialogFormReset"
             v-hasPermiRouter="['iot:hardwareType: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="名称" prop="hardwareTypeName"  show-overflow-tooltip/>
          <el-table-column label="硬件标识" prop="hardwareTypeKey" width="200" show-overflow-tooltip/>
          <el-table-column label="物联分类" prop="deviceTypeName" width="200" show-overflow-tooltip/>
          <el-table-column label="物联标识" prop="deviceTypeKey" width="200" show-overflow-tooltip/>
          <el-table-column label="小图标" align="left" prop="icon" width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              <img v-if="scope.row.icon" style="height:24px;width:24px;" :src="fileBrowseEnvironment+scope.row.icon">
              <span v-else>--</span>
            </template>
          </el-table-column>
          <el-table-column label="大图标" align="left" prop="exceptionIcon" width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              <img v-if="scope.row.exceptionIcon" style="height:24px;width:24px;" :src="fileBrowseEnvironment+scope.row.exceptionIcon">
              <span v-else>--</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="state" width="100" show-overflow-tooltip
                           v-hasPermiRouter="['iot:hardwareType:edit']">
            <template slot-scope="scope">
              <el-switch
                @click.native="tableButton(5,scope.row)"
                class="switch captcha-img"
                :active-value="true"
                :inactive-value="false"
                active-color="#0183FA"
                inactive-color="#999"
                v-model="scope.row.state"
                active-text="启用"
                inactive-text="停用"
                disabled
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{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(2,scope.row)"
                   v-hasPermiRouter="['iot:hardwareType:detail']"
                >详情</p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['iot:hardwareType:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(4,scope.row)"
                   v-hasPermiRouter="['iot:hardwareType: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="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <infoPage v-if="pageType == 2" :propsData="propsData"></infoPage>
    <!--新增弹窗-->
    <el-dialog class="iotClassification-dialog" :title='dialogTitle' width="540px" append-to-body
               :visible.sync="dialogType" v-if="dialogType" @close="dialogOff()"
               :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="dialogForm" ref="dialogForm" :inline="true"
               :rules="dialogRules" class="addCheckPage-min" label-width="120px">
        <!--<el-form-item label="名称" prop="hardwareTypeName">-->
          <!--<el-input v-model="dialogForm.hardwareTypeName" disabled-->
                    <!--placeholder="请输入名称" maxLength="10" style="width:320px;"/>-->
        <!--</el-form-item>-->
        <el-form-item label="硬件标识" prop="hardwareTypeKey">
          <el-select v-model="dialogForm.hardwareTypeKey" @change="hardwareChange" placeholder="请选择硬件标识" style="width: 320px">
            <el-option
              v-for="dict in keyOptionList"
              :key="dict.code"
              :label="dict.name"
              :value="dict.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="dialogForm.state">
            <el-radio :label="true" style="margin-left:20px;">启用</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="物联标识" prop="deviceTypeKey">
          <el-select v-model="dialogForm.deviceTypeKey" clearable @change="deviceChange" placeholder="请选择物联标识" style="width: 320px">
            <el-option
              v-for="dict in iotTypeOptions"
              :key="dict.typeKey"
              :label="dict.typeName"
              :value="dict.typeKey"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="小图标：" prop="icon">
          <el-upload
            style="width:100px;"
            class="iotClassification-dialog-upload"
            :action="uploadImgUrl"
            :show-file-list="false"
            :on-success="(res)=>handleAvatarSuccess(res,'icon')"
            :headers="headers"
            :before-upload="(file)=>beforeAvatarUpload(file)">
            <div class="center-img" style="margin-left:20px;">
              <img v-if="dialogForm.icon" :src="fileBrowseEnvironment+dialogForm.icon" style="max-width:40px;max-height:40px;">
              <p v-if="!dialogForm.icon" class="el-icon-upload" style="text-align: center;line-height:40px;font-size:30px;color:#0183FA;"></p>
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="大图标：" prop="exceptionIcon">
          <el-upload
            style="width:100px;"
            class="iotClassification-dialog-upload"
            :action="uploadImgUrl"
            :show-file-list="false"
            :on-success="(res)=>handleAvatarSuccess(res,'exceptionIcon')"
            :headers="headers"
            :before-upload="(file)=>beforeAvatarUpload(file)">
            <div class="center-img" style="margin-left:20px;">
              <img v-if="dialogForm.exceptionIcon" :src="fileBrowseEnvironment+dialogForm.exceptionIcon" style="max-width:40px;max-height:40px;">
              <p v-if="!dialogForm.exceptionIcon" class="el-icon-upload" style="text-align: center;line-height:40px;font-size:30px;color:#E65D6E;"></p>
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="dialogOff()">取消</p>
        <p class="dialog-footer-button-primary" @click="dialogSubmit">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
    <!--<add-page :propsData="propsData" v-if="pageType === 2"></add-page>-->
  </div>
</template>
<script>
  import { getToken } from "@/utils/auth";
  import {
    iotHardwareTypeAdd,
    iotHardwareTypeUpdate,
    iotHardwareTypeList,
    iotHardwareTypeDelete,
    iotHardwareTypeGetTypeKey,
    iotTypeGetAllTypes
  } from '@/api/iotDevice/index'
  import infoPage from "./infoPage.vue"
  export default {
    name: 'index',
    components: {
      infoPage
    },
    data () {
      return {
        //上传相关
        uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
        headers: {
          Authorization:getToken(),
        },
        tableButtonType:this.hasPermiDom(['iot:hardwareType:edit','iot:hardwareType:del',]),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //下拉列表数据
        optionList:[{value:true,label:'启用'},{value:false,label:'停用'}],
        keyOptionList:[],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:"",
          state :null,
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //组件传参
        propsData:{},
        //弹窗相关
        dialogTitle:'',
        dialogType:false,
        dialogForm:{},
        dialogRules:{
          hardwareTypeName: [
            { required: true, message: "请输入名称", trigger: "blur" },
            { required: true, message: "请输入名称", validator: this.spaceJudgment, trigger: "blur" }
          ],
          hardwareTypeKey: [
            { required: true, message: "请输入标识", trigger: "blur" },
            { required: true, message: "请输入标识", validator: this.spaceJudgment, trigger: "blur" }
          ],
          state: [
            { required: true, message: "请选择状态", trigger: "blur" },
            { required: true, message: "请选择状态", validator: this.spaceJudgment, trigger: "blur" }
          ],
        },
        //物联分类下拉列表
        iotTypeOptions:[],
        // 文件服务地址
        fileBrowseEnvironment:localStorage.getItem('fileBrowseEnvironment'),
      }
    },
    created () {

    },
    mounted () {
      this.iotTypeGetAllTypes();
      this.iotHardwareTypeGetTypeKey();
      this.getList();
    },
    methods: {
      //硬件选中
      hardwareChange(val){
        let self = this;
        this.keyOptionList.forEach((item) => {
          if(val == item.code){
            self.$set(self.dialogForm,'hardwareTypeKey',item.code);
            self.$set(self.dialogForm,'hardwareTypeName',item.name);
          }
        });
      },
      //物联选中
      deviceChange(val){
        let self = this;
        if(val){
          this.iotTypeOptions.forEach((item) => {
            if(val == item.typeKey){
              self.$set(self.dialogForm,'deviceTypeId',item.typeId);
              self.$set(self.dialogForm,'deviceTypeKey',item.typeKey);
              self.$set(self.dialogForm,'deviceTypeName',item.typeName);
            }
          });
        }else{
          this.$set(this.dialogForm,'deviceTypeId','');
          this.$set(this.dialogForm,'deviceTypeKey','');
          this.$set(this.dialogForm,'deviceTypeName','');
        }
      },
      //获取物联分类下拉列表
      iotTypeGetAllTypes(){
        iotTypeGetAllTypes({}).then(response => {
          this.$set(this,'iotTypeOptions',response.data);
        });
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:"",
          state :null,
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        iotHardwareTypeList(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 == 2){
          //详情
          this.$set(this,'propsData',JSON.parse(JSON.stringify(row)));
          this.$set(this,'pageType',2);
        }if(type == 3){
          //编辑
          this.$set(this, 'dialogTitle', '编辑')
          let obj = JSON.parse(JSON.stringify(row))
          this.$set(this,'dialogForm',obj);
          this.$set(this,'dialogType',true);
        }else if(type == 4){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            iotHardwareTypeDelete({hardwareTypeId:row.hardwareTypeId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 5){
          //启用&停用
          let text = row.state  ? "停用" : "启用";
          this.$confirm('是否确认' + text + '?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            iotHardwareTypeUpdate({hardwareTypeId:row.hardwareTypeId,state:!row.state,}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 6){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }
      },
      //获取分类标识
      iotHardwareTypeGetTypeKey(){
        iotHardwareTypeGetTypeKey({}).then(response => {
          this.$set(this,'keyOptionList',response.data);
        });
      },
      //**************************弹窗相关**************************
      //弹层关闭
      dialogOff(){
        this.$set(this,'dialogType',false);
      },
      //弹层新增
      dialogFormReset(){
        this.$set(this,'dialogTitle','新增');
        this.$set(this,'dialogForm',{
          hardwareTypeName:"",
          hardwareTypeKey:"",
          deviceTypeId:"",
          deviceTypeName:"",
          deviceTypeKey:"",
          state:"",
          icon:"",
          exceptionIcon:"",
        });
        this.$set(this,'dialogType',true);
      },
      //dialog提交按钮
      dialogSubmit(){
        this.$refs["dialogForm"].validate(valid => {
          if (valid) {
            let obj = {
              hardwareTypeName:this.dialogForm.hardwareTypeName,
              hardwareTypeKey:this.dialogForm.hardwareTypeKey,
              deviceTypeId:this.dialogForm.deviceTypeId,
              deviceTypeName:this.dialogForm.deviceTypeName,
              deviceTypeKey:this.dialogForm.deviceTypeKey,
              state:this.dialogForm.state,
              icon:this.dialogForm.icon,
              exceptionIcon:this.dialogForm.exceptionIcon,
            }
            if(this.dialogForm.hardwareTypeId){
              obj.hardwareTypeId = this.dialogForm.hardwareTypeId;
              iotHardwareTypeUpdate(obj).then(response => {
                this.msgSuccess(response.message)
                this.dialogOff();
                this.getList();
              });
            }else{
              iotHardwareTypeAdd(obj).then(response => {
                this.msgSuccess(response.message)
                this.dialogOff();
                this.getList();
              });
            }
          }
        })
      },
      //图片上传相关
      handleAvatarSuccess(res,type) {
        if(type == 'icon'){
          this.$set(this.dialogForm,'icon',res.data.url);
        }else if(type == 'exceptionIcon'){
          this.$set(this.dialogForm,'exceptionIcon',res.data.url);
        }
      },
      beforeAvatarUpload(file) {
        return new Promise((resolve, reject) => {
          if (file.type == 'image/svg+xml') {
            resolve()
          }else{
            this.msgError('只能上传svg格式图片')
            reject();
          }
        })
      },
    },
  }
</script>
<style scoped lang="scss">
  .hardwareClassification{
    .hardwareClassificationPage{

    }
  }
</style>
