<template>
  <div class="app-container iot-model">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :inline="true"
      :model="queryParams"
      class="main-search-card"
      label-width="68px"
    >
      <el-form-item label="型号名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          clearable
          placeholder="型号名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品PK" prop="prodKey">
        <el-input
          v-model="queryParams.prodKey"
          clearable
          placeholder="产品PK"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="协议类型" prop="protocolType">
        <el-select
          v-model="queryParams.protocolType"
          clearable
          placeholder="请选择协议类型"
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="(keys, vals) in protocolTypeOpt"
            :key="keys"
            :label="keys"
            :value="vals"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-search"
          size="mini"
          type="primary"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="main-content-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['iot:model:add']"
            icon="el-icon-plus"
            plain
            size="mini"
            type="primary"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="modelList"
        :default-sort="{ prop: 'createTime', order: 'descending' }"
        @sort-change="sortChange"
      >
        <el-table-column
          :index="indexFormatter"
          align="center"
          label="序号"
          type="index"
          width="80px"
        ></el-table-column>
        <el-table-column
          align="left"
          label="型号名称"
          prop="modelName"
          width="200px"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="handleUpdate(scope.row)">{{
              scope.row.modelName
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="left"
          label="厂商名称"
          prop="vendorName"
          width="200px"
        />
        <el-table-column
          align="left"
          label="产品PK"
          prop="prodKey"
          width="200px"
        />
        <el-table-column align="left" label="产品密钥" prop="prodSecret">
          <template slot-scope="scope">
            <span :title="scope.row.prodSecret" class="lay-table-textarea">
              {{ scope.row.prodSecret }}
            </span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="设备类型"
          prop="deviceTypeName"
          width="120px"
        >
          <!--        <template slot-scope="scope">-->
          <!--          <span v-text="deviceTypeList[scope.row.deviceType]"></span>-->
          <!--        </template>-->
        </el-table-column>

        <el-table-column
          align="center"
          label="协议类型"
          prop="protocolType"
          width="120px"
        >
          <template slot-scope="scope">
            <span v-text="protocolTypeOpt[scope.row.protocolType]"></span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="设备状态"
          prop="modelStatus"
          width="100px"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.modelStatus === '0'" type="success"
              >启用</el-tag
            >
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label=" 创建时间"
          prop="createTime"
          sortable="custom"
          width="150px"
        />
        <el-table-column
          align="center"
          class-name="small-padding fixed-width"
          label="操作"
          width="160px"
        >
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['iot:model:edit']"
              icon="el-icon-edit"
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              v-hasPermi="['iot:model:remove']"
              icon="el-icon-delete"
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改型号对话框 -->
    <dialog-template
      :title="title"
      :visible="open"
      width="1000px"
      @close="open = false"
    >
      <el-form
        ref="form"
        slot="dialog-center"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="厂商：" prop="vendorId">
              <el-input
                v-model="form.vendorName"
                placeholder="点击选择厂商"
                @focus="openTableSelectDialog()"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="协议类型：" prop="protocolType">
              <el-select
                v-model="form.protocolType"
                placeholder="请选择协议类型"
                size="small"
                style="width: 100%"
              >
                <el-option
                  v-for="(keys, vals) in protocolTypeOpt"
                  :key="vals"
                  :label="keys"
                  :value="vals"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品PK：" prop="prodKey">
              <el-input
                v-if="form.protocolType === 'OFFICIAL'"
                v-model="form.prodKey"
                :disabled="
                  form.modelId !== undefined &&
                    form.modelId !== null &&
                    form.modelId !== ''
                "
                placeholder="请填写产品PK"
              ></el-input>
              <el-input
                v-else
                v-model="form.prodKey"
                :disabled="form.modelId || form.modelId === 0"
                placeholder="点击选择产品"
                @focus="openProductTableSelectDialog()"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号名称：" prop="modelName">
              <el-input v-model="form.modelName" placeholder="请输入型号名称" />
            </el-form-item>
          </el-col>

          <el-col
            :span="24"
            style="
              display: flex;
              justify-content: flex-end;
              height: 0;
              z-index: 99;
              position: relative;
              top: -10px;"
          >
            <el-button
              icon="el-icon-s-platform"
              style="height: 40px;"
              type="primary"
              @click="openModelDialog"
              >物模型</el-button
            >
            <el-button
              icon="el-icon-menu"
              style="height: 40px;"
              type="primary"
              @click="openTableSelectScriesDialog"
              >选择型号系列</el-button
            >
          </el-col>

          <el-col :span="24">
            <el-form-item
              v-if="form.protocolType === 'OFFICIAL'"
              label="产品json："
              prop="prodJson"
            >
              <attribute-view
                v-if="open"
                ref="attributeref"
                :arttributeList="form.prodJson"
                :groupList="form.remark"
                @handleEvent="handleViewEvent"
              ></attribute-view>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="设备类型：" prop="deviceType">
              <el-select
                v-model="form.deviceType"
                clearable
                placeholder="请选择设备类型"
                size="small"
                style="width: 100%"
              >
                <el-option
                  v-for="(keys, vals) in deviceTypeList"
                  :key="vals"
                  :label="keys"
                  :value="vals"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item v-show="form.deviceType" label="参数设置：">
              <span style="color: red; font-size: 12px"
                >*注意：锁定即参数不可修改；未锁则可以修改。</span
              >
              <div class="form-params-wrap">
                <param-wrap
                  ref="paramWrap"
                  :typeKeys="form.deviceType"
                ></param-wrap>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="dialog-footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </dialog-template>

    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="selectTableShow"
      class="select-table-dialog"
      title="选择"
      top="10vh"
      width="75%"
    >
      <select-table-wrap
        v-if="selectTableShow"
        :otherOption="tableSelectOption.otherOption"
        :queryOption="tableSelectOption.queryOpt"
        :tableList="tableSelectOption.tableList"
        :tableOption="tableSelectOption.tableOpt"
        @parentGetList="childGetList($event)"
        @returnEvent="returnEvent($event)"
      />

      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="resuleClick"
          >确 定</el-button
        >
        <el-button
          size="mini"
          @click="
            () => {
              selectTableShow = false;
            }
          "
          >取 消</el-button
        >
      </div>
    </el-dialog>

    <dialog-template
      :title="componentTitle"
      :visible="AttributeViewShow"
      width="750px"
      @close="
        () => {
          AttributeViewShow = false;
        }
      "
    >
      <component
        :is="componectVal"
        v-if="AttributeViewShow"
        ref="componentref"
        slot="dialog-center"
        :other="{
          action: '',
          prodPK: form.prodKey,
          cmdKey: ''
        }"
        :paramIdx="childOpt.paramIdx"
        :paramsList="functionList"
        :tempType="childOpt.type"
        @ok="compEventOk"
      ></component>

      <div slot="dialog-footer">
        <el-button size="mini" type="primary" @click="submitAttribute"
          >确 定</el-button
        >
        <el-button
          size="mini"
          @click="
            () => {
              AttributeViewShow = false;
            }
          "
          >取 消</el-button
        >
      </div>
    </dialog-template>

    <el-dialog
      :visible.sync="modelOpen"
      class="params-eldialog"
      title="物模型"
      top="5vh"
      width="800px"
    >
      <params-json-wrap
        v-if="modelOpen === true"
        :other="{
          action: '',
          prodPK: this.form.prodKey,
          cmdKey: ''
        }"
        :paramsList="modelValue"
      ></params-json-wrap>
    </el-dialog>
  </div>
</template>

<script>
import {
  listModel,
  getModel,
  delModel,
  addModel,
  updateModel,
  exportModel,
  listProductList,
  listModelType
} from "@/api/iot/model";
import { listVendor } from "@/api/iot/vendor";
import { listDeviceTypeList } from "@/api/iot/device";
import SelectTableWrap from "@/components/SelectTable/index";
import ParamWrap from "@/components/ParamWrap/deviceParam";
import AttributeView from "@/views/profile/attribute/index";

import DialogTemplate from "@/components/DialogTemplate";

import attributeForm from "@/views/profile/attribute/attributeForm";
import groupForm from "@/views/profile/attribute/groupForm";
import paramsJson from "@/views/profile/attribute/paramsJson";
import { listModelSeries, getModelSeries } from "@/api/iot/modelSeries";
import ParamsJsonWrap from "@/views/profile/attribute/paramsJson";

const deviceStartsOpt = {
  0: "禁用",
  1: "启用"
};

export default {
  name: "Model",
  components: {
    SelectTableWrap,
    ParamWrap,
    AttributeView,
    DialogTemplate,
    attributeForm,
    groupForm,
    paramsJson,
    ParamsJsonWrap
  },
  data() {
    return {
      modelOpen: false,
      modelValue: "",
      componectVal: "attributeForm",
      childOpt: {
        type: "add",
        paramIdx: 0
      },
      protocolTypeOpt: {},
      AttributeViewShow: false,
      AttributeFormViewShow: false,
      selectTableShow: false,
      tableSelectOption: {},
      selectResult: {},
      deviceStartsOpt,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 型号表格数据
      modelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vendorId: null,
        modelName: null,
        modelCode: null,
        prodKey: null,
        prodSecret: null,
        protocolType: null,
        orderByColumn: "createTime",
        isAsc: "desc"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vendorId: [
          { required: true, message: "厂商不能为空", trigger: "change" }
        ],
        prodKey: [
          { required: true, message: "产品PK不能为空", trigger: "change" }
        ],
        modelName: [
          { required: true, message: "型号名称不能为空", trigger: "blur" }
        ],
        deviceType: [
          { required: true, message: "设备类型不能为空", trigger: "blur" }
        ]
      },
      functionList: [],
      deviceTypeList: {},
      componentTitle: ""
    };
  },
  created() {
    this.getModelTypeList();
    this.getDeviceTypeList();
    this.getList();
  },
  methods: {
    handleViewEvent(data) {
      console.log(data);
      this.componentTitle = data.title;
      (this.childOpt.type = data.type),
        (this.childOpt.paramIdx = data.paramIdx);
      this.AttributeViewShow = true;
      this.functionList = JSON.parse(
        JSON.stringify({
          properties: this.$store.getters.attributeList,
          functions: this.$store.getters.groupList
        })
      );
      this.componectVal = data.component;
    },
    submitAttribute() {
      if (this.childOpt.type === "param") {
        this.AttributeViewShow = false;
        return;
      }
      if (this.$refs.componentref) {
        this.$refs.componentref.submitForm();
      }
    },
    compEventOk() {
      //执行
      this.AttributeViewShow = false;
      this.$forceUpdate();
      this.$refs.attributeref.forceUpdate(this.componectVal);
    },
    getModelTypeList() {
      listModelType().then(res => {
        this.protocolTypeOpt = res.data;
      });
    },
    sortChange(column) {
      const sort = {
        isAsc: column.order === "descending" ? "desc" : "asc",
        orderByColumn: column.prop
      };
      this.queryParams = Object.assign(this.queryParams, sort);
      this.handleQuery();
    },
    indexFormatter(val) {
      return (
        val + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    },
    // 查询设备类型列表
    getDeviceTypeList() {
      listDeviceTypeList().then(response => {
        this.deviceTypeList = response.data;
      });
    },
    openProductTableSelectDialog() {
      this.selectResult = {};
      this.tableSelectOption = {
        otherOption: {
          tableType: "product"
        },
        queryOpt: {
          disable: false,
          labelWidth: "68px",
          params: {
            protocolType: this.form.protocolType
          },
          page: {
            pageSize: 10,
            pageNum: 1,
            total: 0
          },
          inline: true,
          queryChilds: []
        },
        tableOpt: {
          loading: false,
          rowKey: "prodId",
          selection: false,
          maxHeight: "45vh",
          childs: [
            {
              style: "",
              label: "产品名称",
              type: "",
              prop: "prodName",
              align: "left",
              width: "",
              "show-overflow-tooltip": false,
              tempType: "span"
            },
            {
              style: "",
              label: "产品PK",
              type: "",
              prop: "prodId",
              align: "left",
              width: "",
              "show-overflow-tooltip": false,
              tempType: "span"
            }
          ],
          tableList: {
            type: Array
          }
        },
        tableList: []
      };
      this.selectTableShow = true;
    },
    openModelDialog() {
      this.modelOpen = true;
      this.modelValue = JSON.parse(
        JSON.stringify({
          properties: this.$store.getters.attributeList,
          functions: this.$store.getters.functionList,
          events: this.$store.getters.eventList
        })
      );
    },
    openTableSelectScriesDialog() {
      this.selectResult = {};
      this.tableSelectOption = {
        otherOption: {
          tableType: "scries"
        },
        queryOpt: {
          disable: false,
          labelWidth: "68px",
          params: {
            seriesName: "",
            prodKey: ""
          },
          page: {
            pageSize: 10,
            pageNum: 1,
            total: 0
          },
          inline: true,
          queryChilds: [
            {
              style: "",
              placeholder: "系列名称",
              clearable: true,
              label: "系列名称",
              type: "input",
              key: "seriesName",
              size: "small",
              value: ""
            },
            {
              style: "",
              placeholder: "产品PK",
              clearable: true,
              label: "产品PK",
              type: "input",
              key: "prodKey",
              size: "small",
              value: ""
            }
          ]
        },
        tableOpt: {
          loading: false,
          rowKey: "deviceId",
          selection: false,
          maxHeight: "45vh",
          childs: [
            {
              style: "",
              label: "系列名称",
              type: "",
              prop: "seriesName",
              align: "left",
              width: "",
              "show-overflow-tooltip": false,
              tempType: "span"
            },
            {
              style: "",
              label: " 产品PK",
              type: "",
              prop: "prodKey",
              align: "left",
              width: "",
              "show-overflow-tooltip": false,
              tempType: "span"
            },
            {
              style: "",
              label: "创建时间",
              type: "",
              prop: "createTime",
              align: "left",
              width: "",
              "show-overflow-tooltip": false,
              tempType: "span"
            }
          ],
          tableList: {
            type: Array
          }
        },
        tableList: []
      };
      this.selectTableShow = true;
    },
    // 打开厂商选择窗口 ——表格
    openTableSelectDialog() {
      this.selectResult = {};
      this.tableSelectOption = {
        otherOption: {
          tableType: "vendor"
        },
        queryOpt: {
          disable: false,
          labelWidth: "68px",
          params: {
            vendorName: "",
            vendorAddress: "",
            vendorContact: ""
          },
          page: {
            pageSize: 10,
            pageNum: 1,
            total: 0
          },
          inline: true,
          queryChilds: [
            {
              style: "",
              placeholder: "厂商名称",
              clearable: true,
              label: "厂商名称",
              type: "input",
              key: "vendorName",
              size: "small",
              value: ""
            },
            {
              style: "",
              placeholder: "厂商地址",
              clearable: true,
              label: "厂商地址",
              type: "input",
              key: "vendorAddress",
              size: "small",
              value: ""
            },
            {
              style: "",
              placeholder: "联系方式",
              clearable: true,
              label: "联系方式",
              type: "input",
              key: "vendorContact",
              size: "small",
              value: ""
            }
          ]
        },
        tableOpt: {
          loading: false,
          rowKey: "deviceId",
          selection: false,
          maxHeight: "45vh",
          childs: [
            {
              style: "",
              label: "厂商名称",
              type: "",
              prop: "vendorName",
              align: "left",
              width: "",
              "show-overflow-tooltip": false,
              tempType: "span"
            },
            {
              style: "",
              label: " 联系方式",
              type: "",
              prop: "vendorContact",
              align: "left",
              width: "",
              "show-overflow-tooltip": false,
              tempType: "span"
            },
            {
              style: "",
              label: "厂商地址",
              type: "",
              prop: "vendorAddress",
              align: "left",
              width: "",
              "show-overflow-tooltip": false,
              tempType: "span"
            }
          ],
          tableList: {
            type: Array
          }
        },
        tableList: []
      };
      this.selectTableShow = true;
    },
    // 查询回调
    childGetList(data) {
      switch (data.otherOption.tableType) {
        case "vendor":
          this.vendorChildList(data);
          break;

        case "product":
          this.productChildList(data);
          break;

        case "scries":
          this.scriesChildList(data);
          break;
      }
    },
    productChildList(data) {
      listProductList(
        Object.assign(data.page, data.param, { selected: 1 })
      ).then(response => {
        this.tableSelectOption.tableList = response.data;
        // this.tableSelectOption.queryOpt.page.total = Number(response.total);
      });
    },
    vendorChildList(data) {
      listVendor(Object.assign(data.page, data.param, { selected: 1 })).then(
        response => {
          this.tableSelectOption.tableList = response.rows;
          this.tableSelectOption.queryOpt.page.total = Number(response.total);
        }
      );
    },
    scriesChildList(data) {
      listModelSeries(Object.assign(data.page, data.param)).then(response => {
        this.tableSelectOption.tableList = response.rows;
        this.tableSelectOption.queryOpt.page.total = Number(response.total);
      });
    },
    // 点击或者双击数据回调
    returnEvent(data) {
      console.log("弹窗回调", data);
      this.selectResult = {};
      if (data.type === "dblclick") {
        switch (data.otherOption.tableType) {
          case "vendor":
            this.form.vendorId = data.value.vendorId;
            this.form.vendorName = data.value.vendorName;
            break;

          case "product":
            this.form.prodKey = data.value.prodId;
            this.form.prodSecret = data.value.prodSecret;
            break;

          case "scries":
            this.form.prodJson = data.value.prodJson;
            this.form.remark = data.value.remark;
            this.form.deviceType = data.value.deviceType;
            this.form.seriesKey = data.value.prodKey;
            try {
              this.form.paramList = JSON.parse(data.value.paramJson);
            } catch (error) {
              this.form.paramList = [];
            }
            let _this = this;
            setTimeout(() => {
              _this.$refs.paramWrap.setList(_this.form.paramList || []);
              _this.$refs.attributeref.handleInitData();
              _this.$refs.attributeref.forceUpdateList("groupForm");
              _this.$refs.attributeref.forceUpdateList("attributeForm");
            }, 100);

            break;
        }
        this.selectTableShow = false;
      } else if (data.type === "click") {
        switch (data.otherOption.tableType) {
          case "vendor":
            this.selectResult.vendorId = data.value.vendorId;
            this.selectResult.vendorName = data.value.vendorName;
            break;

          case "product":
            this.selectResult.prodKey = data.value.prodId;
            this.selectResult.prodSecret = data.value.prodSecret;
            break;

          case "scries":
            this.selectResult.prodJson = data.value.prodJson;
            this.selectResult.remark = data.value.remark;
            this.selectResult.deviceType = data.value.deviceType;
            this.selectResult.seriesKey = data.value.deviceType;
            try {
              this.selectResult.paramList = JSON.parse(data.value.paramJson);
            } catch (error) {
              this.selectResult.paramList = [];
            }
            console.log("row:", data.value);
            break;
        }

        this.selectResult.tableType = data.otherOption.tableType;
      }
    },
    // 点击确定按钮
    resuleClick() {
      debugger;
      switch (this.selectResult.tableType) {
        case "vendor":
          this.form.vendorId = this.selectResult.vendorId;
          this.form.vendorName = this.selectResult.vendorName;
          break;

        case "product":
          this.form.prodKey = this.selectResult.prodKey;
          this.form.prodSecret = this.selectResult.prodSecret;
          break;

        case "scries":
          this.form.prodJson = this.selectResult.prodJson;
          this.form.remark = this.selectResult.remark;
          this.form.deviceType = this.selectResult.deviceType;
          this.form.paramList = this.selectResult.paramList;
          let _this = this;
          setTimeout(() => {
            _this.$refs.paramWrap.setList(_this.form.paramList || []);
            _this.$refs.attributeref.handleInitData();
            _this.$refs.attributeref.forceUpdateList("groupForm");
            _this.$refs.attributeref.forceUpdateList("attributeForm");
            _this.$refs.attributeref.forceUpdateList("functionForm");
          }, 100);

          break;
      }
      this.selectTableShow = false;
    },
    /** 查询型号列表 */
    getList() {
      this.loading = true;
      listModel(this.queryParams).then(response => {
        this.modelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vendorId: null,
        modelName: null,
        vendorName: "",
        prodKey: "",
        deviceType: "",
        paramList: [],
        protocolType: "OFFICIAL",
        prodJson: "",
        seriesKey: ""
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.modelId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加型号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const modelId = row.modelId || this.ids;
      const _this = this;
      getModel(modelId).then(response => {
        _this.form = response.data;
        _this.open = true;
        _this.title = "修改型号";
        setTimeout(() => {
          _this.$refs.paramWrap.setList(response.data.paramList || []);
        }, 100);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.protocolType === "OFFICIAL") {
            let attrList = [];
            if (
              this.$store.getters.attributeList &&
              this.$store.getters.attributeList.length > 0
            ) {
              attrList = this.$store.getters.attributeList.map(v => {
                v["sourceId"] = this.form.prodKey;
                return v;
              });
              attrList.sort((a, b) => a.sort - b.sort);
            }

            let groupList = [];
            if (
              this.$store.getters.groupList &&
              this.$store.getters.groupList.length > 0
            ) {
              groupList = this.$store.getters.groupList.map(v => {
                v["sourceId"] = this.form.prodKey;
                return v;
              });
              groupList.sort((a, b) => a.sort - b.sort);
            }

            let functionList = [];
            if (
              this.$store.getters.functionList &&
              this.$store.getters.functionList.length > 0
            ) {
              functionList = this.$store.getters.functionList;
            }

            let eventList = [];
            if (
              this.$store.getters.eventList &&
              this.$store.getters.eventList.length > 0
            ) {
              eventList = this.$store.getters.eventList;
            }

            this.form.prodJson =
              JSON.stringify({
                properties: attrList,
                functions: functionList,
                events: eventList
              }) || null;
            this.form.remark = JSON.stringify(groupList) || null;
          }

          this.form.paramList = this.$refs.paramWrap.getResult();
          if (this.form.modelId != null) {
            updateModel(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModel(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const modelIds = row.modelId || this.ids;
      this.$confirm("是否删除该选项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delModel(modelIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有型号数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportModel(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    }
  }
};
</script>
<style lang="scss">
.iot-model {
  .eldialog-wrap {
    .el-dialog__header {
    }
    .el-dialog__body {
      padding: 0px;
    }
    .el-form {
      padding: 20px;
      padding-right: 40px;
    }
    .el-dialog__footer {
      height: 60px;

      text-align: right;
      width: 100%;
      padding: 0px;
      padding-top: 15px;
      .el-button + .el-button {
        margin-right: 10px;
      }
      .el-button {
        padding-top: 8px;
      }
    }
    .form-params-wrap {
      height: 100%;
      width: calc(100% + 110px);
      position: relative;
      top: 15px;
      left: -90px;
      max-height: 250px;
      overflow: auto;
      padding: 10px;
      border: 1px solid #009688;
      border-radius: 5px;
    }
  }
}
.form-params-wrap::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 8px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 5px;
}
.form-params-wrap::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 10px;
  box-shadow: inset 0 0 5px #c4c4c4;
  background: #dededea6;
}
.form-params-wrap::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  box-shadow: inset 0 0 5px #f6f6f6;
  border-radius: 10px;
  background: #ffffff;
}
</style>
