<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="类型参数：" prop="typeValue">
        <el-input
          v-model.trim="queryParams.typeValue"
          placeholder="请输入厂家名称"
          clearable
          size="small"
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </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="typeName"  show-overflow-tooltip />
      <el-table-column label="类型参数" align="center" prop="typeValue"  show-overflow-tooltip />
      <el-table-column label="传输方式" align="center" prop="transmissionModel"  show-overflow-tooltip >
        <template slot-scope="scope">
          <span >{{scope.row.transmissionModel == 1 ? "NB-IOT" : scope.row.transmissionModel == 2 ? "Lord-IOT" : scope.row.transmissionModel == 3 ? "2G/4G/5G" :""}}</span>
        </template>
      </el-table-column>
      <el-table-column label="厂家名称" align="center" prop="business"  show-overflow-tooltip />
      <el-table-column label="所属单位" align="center" prop="companyName"  show-overflow-tooltip />
      <el-table-column label="创建人" align="center" prop="addPeople"  show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="addTime"  show-overflow-tooltip />
      <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="typeName">
              <el-input v-model.trim="form.typeName" placeholder="请输入类型名称" maxlength="15" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="类型参数" prop="typeValue">
              <el-input v-model.trim="form.typeValue" placeholder="请输入类型参数" 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="transmissionModel">
              <el-select v-model="form.transmissionModel"   placeholder="请选择传输方式">
                <el-option
                  v-for="item in transmissionModelyList"
                  :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="business">
              <el-input v-model.trim="form.business" placeholder="请输入厂家名称" 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="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">取 消</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 { addDeviceType, delDeviceType, updateDeviceType, getDeviceType, exportDeviceType } from "@/api/business/deviceMange";
import { listChannel } from "@/api/system/product";
import { BASEPATH } from "@/assets/js/common.js";
export default {
  name: "DeviceType",
  data() {
    return {
      companyList:[],
      piliang:false,
      exportLoading:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 20,
        name: undefined,
        typeValue:undefined
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeName: [
          { required: true, message: "类型名称不能为空", trigger: "blur" }
        ],
        typeValue: [
          { required: true, message: "类型参数不能为空", trigger: "blur" }
        ],
        transmissionModel: [
          { required: true, message: "传输方式不能为空", trigger: ["blur",'change'] }
        ],
        business: [
          { required: true, message: "厂家名称不能为空", trigger: "blur" }
        ],
        companyCode: [
          { required: true, message: "所属单位不能为空", trigger: ["blur",'change'] }
        ],
      },
      transmissionModelyList:[
        {id:1,name:"NB-IOT",},
        {id:2,name:"Lord-IOT",},
        {id:3,name:"2G/4G/5G",},
      ]
    };
  },
  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,
        typeVaule:undefined
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      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
      }
      getDeviceType(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
          if (this.form.id != undefined) {
            this.form.alarmStatus = 1
            updateDeviceType(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.getList();
              this.loading = false,
              this.open = false
            }).catch(()=>{
              this.loading = false
            })
          }else{
            addDeviceType(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.getList();
              this.loading = false
              this.open = false
            }).catch(()=>{
              this.loading = false
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDeviceType(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },


    /** 导出按钮操作 */
    handleExport() {
      let queryParams = {...this.queryParams};
      delete queryParams.current
      delete queryParams.size
      this.exportLoading = true;
      exportDeviceType(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,
            }
          })
        }
      });
    }
  }
};
</script>

