<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="通道名称:" prop="name">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入通道名称"
          clearable
          size="small"
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型:" prop="dwtype">
        <el-select v-model="queryParams.dwtype"  size="small" style="width: 160px"  placeholder="请选择传输方式">
          <el-option
            v-for="item in dwtypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="right">
        <el-button plain size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
      <br>
      <el-button
        type="primary"
        size="mini"
        @click="handleAdd"
        v-hasPermi="['system:admin:add']"
      >新增</el-button>
      <el-button
        plain
        size="mini"
        :loading="exportLoading"
        @click="handleExport"
        v-hasPermi="['system:admin:export']"
      >导出</el-button>
    </el-form>
    <el-table v-loading="loading" :data="adminList" style="margin-top:20px" border>
      <el-table-column label="通道名称" align="left" prop="passagewayName"  show-overflow-tooltip />
      <el-table-column label="设备类型" align="left" prop="dwtype"  show-overflow-tooltip >
        <template slot-scope="scope">
          <span>{{ scope.row.dwtype=="1" ? "用户传输装置": scope.row.dwtype=="2"?"喷淋水压" : scope.row.dwtype=="3" ? "独立烟感" :
            scope.row.dwtype=="4" ? "消火栓压力":   scope.row.dwtype=="7" ? "电器火灾监控":  scope.row.dwtype=="5" ?
            "消防水箱液位" : scope.row.dwtype=="6" ? "RTU": scope.row.dwtype=="16" ? "消防视频监控": ""}}</span>
        </template>
      </el-table-column>
      <el-table-column label="通道类型" align="left" prop="passagewayType"  show-overflow-tooltip >
        <template slot-scope="scope">
          <span>
            {{scope.row.dwtype=="1" && scope.row.passagewayType=="1" ?"待定义" :

            scope.row.dwtype=="2" && scope.row.passagewayType=="1" ?"设备状态" : scope.row.dwtype=="2" && scope.row.passagewayType=="2" ?"剩余电量"
            : scope.row.dwtype=="2" && scope.row.passagewayType=="3" ?"无线信号" : scope.row.dwtype=="2" && scope.row.passagewayType=="4" ?"压力值" :

            scope.row.dwtype=="5" && scope.row.passagewayType=="1" ?"状态状态" : scope.row.dwtype=="5" && scope.row.passagewayType=="2" ?"剩余电量" :
            scope.row.dwtype=="5" && scope.row.passagewayType=="3" ?"无线信号" : scope.row.dwtype=="5" && scope.row.passagewayType=="4" ?"液位值" :

            scope.row.dwtype=="6" && scope.row.passagewayType=="1" ?"1~16待定" : scope.row.dwtype=="6" && scope.row.passagewayType=="2" ?"65~72待定" :
            scope.row.dwtype=="16" && scope.row.passagewayType=="1" ?"占到检测告警" : scope.row.dwtype=="16" && scope.row.passagewayType=="2" ?"火点检测告警" : ""

            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位名称" align="left" prop="companyName"  show-overflow-tooltip />
      <el-table-column label="创建人" align="left" prop="addPeople"  show-overflow-tooltip />
      <el-table-column label="创建时间" align="left" prop="addTime"  show-overflow-tooltip />
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            class="lans"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:admin:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            class="lans"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:admin:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改、新增对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :before-close="cancel" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="通道名称" prop="passagewayName">
              <el-input v-model.trim="form.passagewayName" placeholder="请填写通道名称" @input="onInput()" maxlength="30" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="设备类型" prop="dwtype">
              <el-select v-model="form.dwtype"  placeholder="请选择设备类型" @change="dwtype">
                <el-option
                  v-for="item in dwtypeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="通道类型" prop="passagewayType">
              <el-select v-model="form.passagewayType"  placeholder="请选择系统类型" @change="selectPassagewayType">
                  <el-option v-if="form.dwtype == '1'" value="1" label="待定" />
                  <el-option v-if="form.dwtype == '2'" value="1" label="设备状态" />
                  <el-option v-if="form.dwtype == '2'" value="2" label="剩余电量" />
                  <el-option v-if="form.dwtype == '2'" value="3" label="无线信号" />
                  <el-option v-if="form.dwtype == '2'" value="4" label="压力值" />

                  <el-option v-if="form.dwtype == '5'" value="1" label="设备状态" />
                  <el-option v-if="form.dwtype == '5'" value="2" label="剩余电量" />
                  <el-option v-if="form.dwtype == '5'" value="3" label="无线信号" />
                  <el-option v-if="form.dwtype == '5'" value="4" label="液位值" />

                  <el-option v-if="form.dwtype == '6'" value="1" label="1~16待定" />
                  <el-option v-if="form.dwtype == '6'" value="2" label="65~72待定" />

                  <el-option v-if="form.dwtype == '16'" value="1" label="占到检测告警" />
                  <el-option v-if="form.dwtype == '16'" value="2" label="火点检测告警" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="所属单位" prop="companyCode">
              <el-select v-model="form.companyCode"  placeholder="请选择所属单位">
                <el-option
                  v-for="item in companyList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" plain>取 消</el-button>
      </div>
    </el-dialog>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
    <!-- 导出-->
    <a href="" download="" id="xz" style="position: absolute;z-index: -1;opacity: 0;"></a>
  </div>
</template>
<script>
import { addDeviceChannel, delDeviceChannel, updateDeviceChannel, getDeviceChannel, exportDeviceChannel, getDeviceType } from "@/api/business/deviceMange";
import { listChannel } from "@/api/system/product";
import { BASEPATH } from "@/assets/js/common.js";
export default {
  name: "Device",
  data() {
    return {
      fileList:[],
      loading:true,
      exportLoading:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: undefined,
        dwtype:undefined,
      },
      dateRange:[],
      // 表单参数
      form: {
        passagewayType:undefined
      },
      // 表单校验
      rules: {
        passagewayName: [
          { required: true, message: "通道名称不能为空", trigger: "blur" }
        ],
        // deviceCode: [
        //   { required: true, message: "设备编号不能为空", trigger: "blur" }
        // ],
        dwtype: [
          { required: true, message: "通道类型不能为空", trigger: ["blur",'change'] }
        ],
        passagewayType: [
          { required: true, message: "通道类型不能为空", trigger: ["blur",'change'] }
        ],
        companyCode: [
          { required: true, message: "所属单位不能为空", trigger: ["blur",'change'] }
        ],

      },
      // 整个省市县数据
      // addr1: city,
      // 被选中的市数据
      addr2: [],
      // 被选中的县数据
      addr3: [],
      selectProvince: {},
      selectCity: {},
      selectArea: {},
      transferTypeList:[
        {id:1,name:"NB-IOT",},
        {id:2,name:"Lord-IOT",},
        {id:3,name:"2G/4G/5G",},
      ],
      // dwtypeList:[
      //   {id:1,name:"火系统",},
      //   {id:2,name:"水系统",},
      //   {id:3,name:"烟感系统",},
      //   {id:4,name:"消防栓",},
      //   {id:5,name:"液位",},
      //   {id:6,name:"rtu",},
      //   {id:7,name:"电气火灾",},
      //   {id:8,name:"防火门",},
      //   {id:9,name:"气体灭火",},
      //   {id:10,name:"人脸识别",},
      //   {id:16,name:"视频监控",},
      //   {id:128,name:"井盖",},
      // ],
      dwtypeList:[],
      companyList:[],
      passagewayTypeList:undefined,
      passagewayType:undefined,
        // {id:1,name:"火系统"},
        // {id:2,name:"水系统"},
        // {id:3,name:"烟感"},

    };
  },
  created() {
    this.getList();//初始化table
    this.init()
  },
  methods: {
    //强制el-input刷新
    onInput(){
        this.$forceUpdate();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        name: undefined,
        channelTypeName:undefined,
        current:1,
        size:10
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 查询参数列表 */
    getList() {
      getDeviceChannel(this.queryParams).then(response => {
        this.adminList = response.data.records;
        for(let i = 0;i<this.adminList.length;i++){
          this.adminList[i].dwtype = String(this.adminList[i].dwtype)
          this.adminList[i].passagewayType = String(this.adminList[i].passagewayType)
        }
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "修改";
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form = {}
      this.open = true;
      this.title = "新增";
    },
    /** 提交按钮 */
    submitForm(row) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          if (this.form.id != undefined) {
            updateDeviceChannel(this.form).then(response => {
              this.loading = false,
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false
            })
          } else {
            this.form.deviceCode = ""
            addDeviceChannel(this.form).then(response => {
              this.loading = false,
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDeviceChannel(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      let queryParams = {...this.queryParams};
      delete queryParams.current
      delete queryParams.size
      queryParams.dwType = queryParams.dwtype
      this.exportLoading = true;
      exportDeviceChannel(queryParams).then(response =>{
        this.exportLoading = false;
        document.getElementById("xz").href = BASEPATH()  + response.data
        document.getElementById("xz").download = response.data
        document.getElementById("xz").click()
      })
    },
    init(){
      /* 公司列表 */
      listChannel(this.queryParams).then(response => {
        if(response.data.records.length>0){
          this.companyList = response.data.records.map(val =>{
            return {
              id:val.ownerId,
              name:val.ownerName,
              address:val.ownerAddress,
            }
          })
        }
      });
      getDeviceType({current:1,size:100}).then(response =>{
        this.dwtypeList = response.data.records.map(val=>{
          return {
            id:val.typeValue,
            name:val.typeName
          }
        });
      })
    },
    dwtype(){
      //类型与系统联动
      this.form.passagewayType = undefined
    },
    selectPassagewayType(){
      this.form.passagewayType = this.form.passagewayType
    }
  }
};
</script>
