<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList"
      :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel"
      @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
      <template slot="menuLeft">
        <!-- <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.iotaepproduct_delete"
                   @click="handleDelete">删 除
        </el-button> -->
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {
    getList,
    getDetail,
    add,
    update,
    remove
  } from "@/api/system/terminal/iotaepproduct";
  import {
    mapGetters
  } from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          // addBtn:false,
          delBtn: false,
          // editBtn: false,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          dialogWidth: "30%",
          column: [{
              label: "平台",
              prop: "platform",
              type: "select",
              row: true,
              span: 24,
              dicData: [
                // {
                //   label: "aep",
                //   value: "aep"
                // },
                {
                  label: "scepctwing",
                  value: "scepctwing"
                }
              ],

              rules: [{
                required: true,
                message: "请输入平台：scepctwing（感知平台）  ；aep",
                trigger: "blur"
              }]
            },
            {
              label: "产品ID",
              prop: "productId",
              row: true,
              span: 24,
              rules: [{
                required: true,
                message: "请输入产品ID",
                trigger: "blur"
              }]
            },
            {
              label: "产品名称",
              prop: "productName",
              span: 24,
              row: true,
              rules: [{
                required: true,
                message: "请输入产品名称（必填）：产品名称最多 64 个字符，产品名称必须包含数字或字母或汉字,产品名称租户内不能重复",
                trigger: "blur"
              }]
            },
            {
              label: "MasterKey",
              prop: "apiKey",
              row: true,
              span: 24,
              labelWidth: 100,
              rules: [{
                required: true,
                message: "请输入MasterKey",
                trigger: "blur"
              }]
            },
            {
              label: "厂商编号",
              prop: "manufacturersNo",
              row: true,
              span: 24,
              // labelWidth: 100,
              rules: [{
                required: true,
                message: "请输入厂商编号",
                trigger: "blur"
              }]
            },

            {
              label: "设备类型",
              prop: "deviceType",
              span: 24,
              row: true,
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=device_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入MasterKey",
                trigger: "blur"
              }]
            },
            {
              label: "接入类型",
              prop: "accessType",
              type: "select",
              hide: true,
              display: false,
              // dataType: "number",
              dicData: [{
                  label: "设备直连",
                  value: "1"
                },
                {
                  label: "网关接入",
                  disabled: true,
                  value: "2"
                },
                {
                  label: "南向云接入",
                  value: "3",
                  disabled: true
                },
              ],
              rules: [{
                required: true,
                message: "请输入接入类型（必填）: 1.设备直连，2.网关接入，3.南向云接入（只支持1）",
                trigger: "blur"
              }]
            },
            {
              label: "认证方式",
              prop: "authType",
              type: "select",
              hide: true,
              display: false,
              // dataType: "number",
              dicData: [{
                  label: "特征串认证",
                  value: "1"
                },
                {
                  label: "SM9认证",
                  value: "2"
                },
                {
                  label: "证书认证",
                  value: "3"
                },
                {
                  label: "IMEI认证",
                  value: "4"
                },
                {
                  label: "SIMID认证",
                  value: "5"
                },
                {
                  label: "SM2认证",
                  value: "6"
                },
                {
                  label: "IPV6标识认证",
                  value: "7"
                }
              ],
              rules: [{
                required: true,
                message: "请输入认证方式 1:特征串认证,2:SM9认证,3:证书认证,4:IMEI认证，5:SIMID认证，6:SM2认证，7:IPV6标识认证 ",
                trigger: "blur"
              }]
            },
            {
              label: "数据加密方式",
              prop: "dataEncryption",
              type: "select",
              hide: true,
              display: false,
              // dataType: "number",
              dicData: [{
                  label: "sm1",
                  value: "1"
                },
                {
                  label: "sm2",
                  value: "2"
                },
                {
                  label: "sm4",
                  value: "3"
                },
                {
                  label: "dtls",
                  value: "4"
                },
                {
                  label: "明文（只支持MQTT/LWM2M）",
                  value: "5"
                },

              ],
              rules: [{
                required: true,
                message: "请输入dataEncryption：数据加密方式 1:sm1,2:sm2,3:sm4,4:dtls,5:明文（只支持MQTT/LWM2M）",
                trigger: "blur"
              }]
            },
            // {
            //   label: "设备型号",
            //   prop: "deviceModel",
            //   rules: [{
            //     required: true,
            //     message: "请输入T/T808协议 设备型号",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "安全类型(只支持MQTT,TCP协议) ",
              prop: "encryptionType",
              type: "select",
              hide: true,
              display: false,
              // dataType: "number",
              dicData: [{
                  label: "一型一密",
                  value: "1"
                },
                {
                  label: "一机一密",
                  value: "0"
                },

              ],
              rules: [{
                required: true,
                message: "请输入encryptionType：安全类型(只支持MQTT,TCP协议) 0:一机一密，1:一型一密",
                trigger: "blur"
              }]
            },
            {
              label: "Endpoint格式",
              prop: "endpointFormat",
              type: "select",
              hide: true,
              display: false,
              // dataType: "number",
              dicData: [{
                  label: "IMEI",
                  value: "1"
                },
                {
                  label: "URN:IMEI",
                  value: "2"
                },
                {
                  label: "URN:IMEI-IMSI",
                  value: "3"
                },
                {
                  label: "URN:IMEI+SM9",
                  value: "4"
                },


              ],


              rules: [{
                required: true,
                message: "请输入Endpoint格式（LWM2M协议必填）:1.IMEI2.URN:IMEI:3.URN:IMEI-IMSI: 4.URN:IMEI+SM9",
                trigger: "blur"
              }]
            },
            {
              label: "eDRX模式时间窗",
              prop: "lwm2mEdrxTime",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入eDRX模式时间窗(LWM2M协议,当省电模式为3时,必填):20 ～ 10485.76 间的值,精确到小数点后两位",
                trigger: "blur"
              }]
            },
            {
              label: "JT/T808协议 制造商ID",
              prop: "manufacturerId",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入JT/T808协议 制造商ID",
                trigger: "blur"
              }]
            },
            {
              label: "网络类型",
              prop: "networkType",
              type: "select",
              hide: true,
              display: false,
              // dataType: "number",
              dicData: [{
                  label: "WIFI",
                  value: "1"
                },
                {
                  label: "移动蜂窝数据",
                  value: "2"
                },
                {
                  label: "NB-IoT",
                  value: "3"
                },
                {
                  label: "以太网",
                  value: "4"
                },
                {
                  label: "蓝牙",
                  value: "5",
                  disabled: true
                },
                {
                  label: "ZigBee",
                  value: "6",
                  disabled: true
                },

              ],
              rules: [{
                required: true,
                message: "请输入网络类型（必填）:1.WIFI,2.移动蜂窝数据3.NB-IoT,4.以太网,5.蓝牙,6.ZigBee（只支持1/2/3/4）",
                trigger: "blur"
              }]
            },
            {
              label: "节点类型",
              prop: "nodeType",
              type: "select",
              hide: true,
              display: false,
              // dataType: "number",
              dicData: [{
                  label: "设备",
                  value: "1"
                },
                {
                  label: "网关",
                  value: "2"
                },

              ],
              rules: [{
                required: true,
                message: "请输入节点类型（必填）：1.设备 ，2.网关",
                trigger: "blur"
              }]
            },
            {
              label: "消息格式",
              prop: "payloadFormat",
              type: "select",
              hide: true,
              display: false,
              // dataType: "number",
              dicData: [{
                  label: "json",
                  value: "1"
                },
                {
                  label: "紧凑二进制",
                  value: "2"
                },


              ],
              rules: [{
                required: true,
                message: "请输入消息格式 1:json，2:紧凑二进制",
                trigger: "blur"
              }]
            },
            {
              label: "省电模式",
              prop: "powerModel",
              type: "select",
              hide: true,
              display: false,
              // dataType: "number",
              dicData: [{
                  label: "PSM",
                  value: "1"
                },
                {
                  label: "DRX",
                  value: "2"
                },
                {
                  label: "eDRX",
                  value: "3"
                },


              ],
              rules: [{
                required: true,
                message: "请输入省电模式（LWM2M协议必填）：1.PSM 2.DRX 3.eDRX",
                trigger: "blur"
              }]
            },
            {
              label: "产品描述 ",
              prop: "productDesc",
              hide: true,
              display: false,
              rules: [{
                required: false,
                message: "请输入产品描述（选填）：产品描述最多100个字符",
                trigger: "blur"
              }]
            },

            {
              label: "通讯协议",
              prop: "productProtocol",
              type: "select",
              row: true,
              span: 24,
              // dataType: "number",
              dicData: [{
                  label: "NB",
                  value: "1"
                },
                {
                  label: "LWM2M",
                  value: "2"
                },
                {
                  label: "HTTP",
                  value: "3"
                },

                {
                  label: "TCP",
                  value: "4"
                },
                {
                  label: "MQTT",
                  value: "5"
                },
                {
                  label: "厂商协议",
                  value: "6"
                },

              ],
              rules: [{
                required: true,
                message: "请输入产品协议（必填）：只支持 1.T-LINK协议 2.MQTT协议 3.LWM2M协议 5.HTTP协议 6.JT/T808 7.TCP协议 10.网关MQTT协议",
                trigger: "blur"
              }]
            },
            {
              label: "产品分类（必填）",
              prop: "productType",
              hide: true,
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入产品分类（必填）",
                trigger: "blur"
              }]
            },
            {
              label: "二级分类（必填）",
              prop: "secondaryType",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入二级分类（必填）",
                trigger: "blur"
              }]
            },
            {
              label: "三级分类（必填）",
              prop: "thirdType",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入三级分类（必填）",
                trigger: "blur"
              }]
            },
            {
              label: "是否透传",
              prop: "tupisThrough",
              type: "select",
              hide: true,
              display: false,
              // dataType: "number",
              dicData: [{
                  label: "不透传",
                  value: "1"
                },
                {
                  label: "透传",
                  value: "0"
                },


              ],
              rules: [{
                required: true,
                message: "请输入是否透传：0.透传，1不透传（JT/T808，tcp协议，只有透传，消息格式必须只能传null）",
                trigger: "blur"
              }]
            },

              {
                label: "设备型号",
                prop: "deviceSpecld",
                span: 24,
                row: true,
                rules: [{
                  required: true,
                  message: "请输入设备具体型号",
                  trigger: "blur"
                }]
              },
            {
              label: "设备具体型号",
              prop: "tupDeviceModel",
              span: 24,
              labelWidth: 110,
              row: true,
              rules: [{
                required: true,
                message: "请输入设备具体型号",
                trigger: "blur"
              }]
            },


          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.iotaepproduct_add, false),
          viewBtn: this.vaildData(this.permission.iotaepproduct_view, false),
          delBtn: this.vaildData(this.permission.iotaepproduct_delete, false),
          editBtn: this.vaildData(this.permission.iotaepproduct_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
