<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <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="时间：">
         <el-date-picker
          style="margin-top:5px;"
          v-model="dateRange"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:admin:add']"
        >新增</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:admin:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="adminList" height="calc(100vh - 28vh)">
      <el-table-column label="设备编号" align="center" prop="ownerCode"  show-overflow-tooltip />
      <el-table-column label="设备名称" align="center" prop="ownerName"  show-overflow-tooltip />
      <el-table-column label="设备类型" align="center" prop="deviceType"  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=="5"?"液位" : scope.row.dwtype=="6"?"rtu" :
            scope.row.dwtype=="7"?"电气火灾" : scope.row.dwtype=="8"?"防火门" :
            scope.row.dwtype=="9"?"气体灭火" : scope.row.dwtype=="10"?"人脸识别" :
            scope.row.dwtype=="16"?"视频监控" : scope.row.dwtype=="128"?"井盖":""}}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="所属单位" align="center" prop="companyName"  show-overflow-tooltip />
      <el-table-column label="所属建筑" align="center" prop="louyu"  show-overflow-tooltip />
      <el-table-column label="安装位置" align="center" prop="posistion"  show-overflow-tooltip />
      <el-table-column label="详细地址" align="center" prop="address"  show-overflow-tooltip />
      <!-- <el-table-column label="创建人" align="center" prop="addMan" />
      <el-table-column label="创建时间" align="center" prop="addTime" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:admin:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @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="ownerCode" >
              <el-input v-model="form.ownerCode" placeholder="请填写设备编号称" @input="onInput()" :disabled="true" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="设备编号" prop="ownerCode" >
              <el-input v-model.trim="form.ownerCode" placeholder="请填写设备编号称" @input="onInput()"  maxlength="15" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="设备名称" prop="ownerName">
              <el-input v-model.trim="form.ownerName" placeholder="请填写设备名称" @input="onInput()" maxlength="15" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="安装位置" prop="unitinfo">
              <el-input v-model.trim="form.unitinfo" placeholder="请填写安装位置" @input="onInput()" maxlength="15" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="所属单位" prop="company" >
              <el-select v-model="form.company"   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-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="建筑" prop="louyu">
              <el-select v-model="form.louyu"   placeholder="请选择所属建筑建筑">
                <el-option
                  v-for="item in buildList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备类型" prop="dwtype">
              <el-select v-model="form.dwtype"   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-col>
        </el-row>
        <el-row :gutter="20">
          <!-- <el-col :span="12" >
            <el-form-item label="传输方式" prop="transferType">
              <el-select v-model="form.transferType"   placeholder="请选择传输方式">
                <el-option
                  v-for="item in transferTypeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="GPS地址" prop="posistion">
              <el-input v-model.trim="form.posistion" placeholder="请填写GPS地址" @input="onInput()" maxlength="15" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="详细地址" prop="address">
              <el-input type="textarea" v-model.trim="form.address" placeholder="请填写详细地址" @input="onInput()" maxlength="20" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="省" prop="data2">
              <el-select v-model="form.data2" filterable size="small" @change="selectProvinceFun($event)" placeholder="请选择省份">
                <el-option value="" label="请选择省份"></el-option>
                <el-option :value="item" :label="item.label" v-for="(item, index) in addr1" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="市" prop="data3">
              <el-select v-model="form.data3" filterable size="small" @change="selectCityFun($event)" placeholder="请选择城市">
                <el-option value="" label="请选择城市"></el-option>
                <el-option :value="item" :label="item.label" v-for="(item, index) in addr2" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row :gutter="20">
          <!-- <el-col :span="12">
            <el-form-item label="区/县" prop="data4">
              <el-select v-model="form.data4" filterable size="small" @change="selectAreaFun($event)" placeholder="请选择区县">
                <el-option value="" label="请选择区县"></el-option>
                <el-option :value="item" :label="item.label" v-for="(item, index) in addr3" :key="index"></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">取 消</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 { addDevice, delDevice, updateDevice, getDevice, exportDevice, getDeviceType } from "@/api/business/deviceMange";
import { getBuilding } from "@/api/business/buildMange";
import { listChannel } from "@/api/system/product";
import { BASEPATH } from "@/assets/js/common.js";
export default {
  name: "Device",
  data() {
    return {
      update:false,
      add:false,
      loading:true,
      exportLoading:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 20,
        name: undefined,
      },
      dateRange:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ownerCode: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        ownerName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        unitinfo: [
          { required: true, message: "安装位置不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
        posistion: [
          { required: true, message: "GPS坐标不能为空", trigger: "blur" }
        ],
        dwtype: [
          { required: true, message: "设备类型不能为空", trigger: ["blur",'change'] }
        ],
        // transferType: [
        //   { required: true, message: "传输方式不能为空", trigger: ["blur",'change'] }
        // ],
        canpany: [
          { required: true, message: "单位名称不能为空", trigger: ["blur",'change'] }
        ],
        louyu: [
          { required: true, message: "建筑名称不能为空", trigger: ["blur",'change'] }
        ],
        // addr1: [
        //   { required: true, message: "省不能为空", trigger: ["blur",'change'] }
        // ],
        // addr2: [
        //   { required: true, message: "市不能为空", trigger: ["blur",'change']}
        // ],
        // addr3: [
        //   { 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:"井盖",},
      ],
      companyList:[],
      buildList :[]
    };
  },
  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,
        current:1,
        size:20
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 查询参数列表 */
    getList() {
      let data = this.addDateRange(this.queryParams,this.dateRange)
      if(JSON.stringify(data.params) == "{}"){
        delete data.params
      }else{
        data.endTime = data.params.endTime
        data.startTime = data.params.beginTime
      }
      getDevice(data).then(response => {
        this.adminList = response.data.records;
        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.open = true;
      this.title = "新增";
    },
    /** 提交按钮 */
    submitForm(row) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          this.form.addr1 = 1
          this.form.addr2 = 2
          this.form.addr3 = 3
          // this.form.addr1 = this.form.data2.value
          // this.form.addr2 = this.form.data3.value
          // this.form.addr3 = this.form.data4.value
          // this.form.data2 = this.form.data2.label
          // this.form.data3 = this.form.data3.label
          // this.form.data4 = this.form.data4.label
          if (this.form.id != undefined) {
            this.update = true
            this.add = false
            updateDevice(this.form).then(response => {
              this.loading = false,
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false
            })
          } else {
            this.update = false
            this.add = true
            addDevice(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 delDevice(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },


    // 省份 市 县联动
    // selectProvinceFun (event) {
    //   if (event) {
    //     this.addr2 = event.children
    //   } else {
    //     this.addr2 = []
    //   }
    //   this.addr3 = []
    //   this.$emit('getLawyerListInfo', [event.label, 'province'])
    // },
    // selectCityFun (event) {
    //   if (event) {
    //     this.addr3 = event.children
    //   } else {
    //     this.addr3 = []
    //   }
    //   this.$emit('getLawyerListInfo', [event.label, 'city'])
    // },
    // selectAreaFun (event) {
    //   this.$emit('getLawyerListInfo', [event.label, 'area'])
    // },
    /** 导出按钮操作 */
    handleExport() {
      let queryParams = {...this.queryParams};
      delete queryParams.current
      delete queryParams.size
      this.exportLoading = true;
      exportDevice(queryParams).then(response =>{
        this.exportLoading = false;
        document.getElementById("xz").href = BASEPATH() + "archive/" + 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,
            }
          })
        }
      });
      /* 建筑列表 */
      getBuilding(this.queryParams).then(response => {
        this.buildList = (response.data).records.map(val =>{
          return {
            id:val.id,
            name:val.buildName
          }
        })
      });
      getDeviceType({current:1,size:100}).then(response =>{
        this.dwtypeList = response.data.records.map(val=>{
          return {
            id:val.id,
            name:val.typeName
          }
        });
      })
    }
  }
};
</script>
