<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="场景名称" prop="sceneName">
        <el-input
          v-model="queryParams.sceneName"
          placeholder="请输入场景名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户ID，关联hw_tenant的tenant_id" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入租户ID，关联hw_tenant的tenant_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场景类型，关联表hw_scene_mode的scene_mode_id" prop="sceneModeId">
        <el-input
          v-model="queryParams.sceneModeId"
          placeholder="请输入场景类型，关联表hw_scene_mode的scene_mode_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场景图片地址" prop="scenePic">
        <el-input
          v-model="queryParams.scenePic"
          placeholder="请输入场景图片地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否默认" prop="defaultFlag">
        <el-input
          v-model="queryParams.defaultFlag"
          placeholder="请输入是否默认"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证方式" prop="authMode">
        <el-input
          v-model="queryParams.authMode"
          placeholder="请输入认证方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场景账号" prop="modeAccount">
        <el-input
          v-model="queryParams.modeAccount"
          placeholder="请输入场景账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场景key" prop="modeKey">
        <el-input
          v-model="queryParams.modeKey"
          placeholder="请输入场景key"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场景secret" prop="modeSecret">
        <el-input
          v-model="queryParams.modeSecret"
          placeholder="请输入场景secret"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保存周期(单位：天）默认90天" prop="preserveTime">
        <el-input
          v-model="queryParams.preserveTime"
          placeholder="请输入保存周期(单位：天）默认90天"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测试环境保存周期(单位：天）默认30天" prop="testPreserveTime">
        <el-input
          v-model="queryParams.testPreserveTime"
          placeholder="请输入测试环境保存周期(单位：天）默认30天"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段，租户环境" prop="sceneEnvironment">
        <el-input
          v-model="queryParams.sceneEnvironment"
          placeholder="请输入预留字段，租户环境"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段" prop="sceneField">
        <el-input
          v-model="queryParams.sceneField"
          placeholder="请输入预留字段"
          clearable
          @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-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:scene:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:scene:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:scene:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:scene:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sceneList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="场景ID" align="center" prop="sceneId" />
      <el-table-column label="场景名称" align="center" prop="sceneName" />
      <el-table-column label="租户ID，关联hw_tenant的tenant_id" align="center" prop="tenantId" />
      <el-table-column label="场景类型，关联表hw_scene_mode的scene_mode_id" align="center" prop="sceneModeId" />
      <el-table-column label="场景图片地址" align="center" prop="scenePic" />
      <el-table-column label="是否默认" align="center" prop="defaultFlag" />
      <el-table-column label="状态" align="center" prop="sceneStatus" />
      <el-table-column label="认证方式" align="center" prop="authMode" />
      <el-table-column label="场景账号" align="center" prop="modeAccount" />
      <el-table-column label="场景key" align="center" prop="modeKey" />
      <el-table-column label="场景secret" align="center" prop="modeSecret" />
      <el-table-column label="保存周期(单位：天）默认90天" align="center" prop="preserveTime" />
      <el-table-column label="测试环境保存周期(单位：天）默认30天" align="center" prop="testPreserveTime" />
      <el-table-column label="场景描述" align="center" prop="remark" />
      <el-table-column label="预留字段，租户环境" align="center" prop="sceneEnvironment" />
      <el-table-column label="预留字段" align="center" prop="sceneField" />
      <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="['business:scene:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:scene:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改场景信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="场景名称" prop="sceneName">
          <el-input v-model="form.sceneName" placeholder="请输入场景名称" />
        </el-form-item>
        <el-form-item label="租户ID，关联hw_tenant的tenant_id" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户ID，关联hw_tenant的tenant_id" />
        </el-form-item>
        <el-form-item label="场景类型，关联表hw_scene_mode的scene_mode_id" prop="sceneModeId">
          <el-input v-model="form.sceneModeId" placeholder="请输入场景类型，关联表hw_scene_mode的scene_mode_id" />
        </el-form-item>
        <el-form-item label="场景图片地址" prop="scenePic">
          <el-input v-model="form.scenePic" placeholder="请输入场景图片地址" />
        </el-form-item>
        <el-form-item label="是否默认" prop="defaultFlag">
          <el-input v-model="form.defaultFlag" placeholder="请输入是否默认" />
        </el-form-item>
        <el-form-item label="认证方式" prop="authMode">
          <el-input v-model="form.authMode" placeholder="请输入认证方式" />
        </el-form-item>
        <el-form-item label="场景账号" prop="modeAccount">
          <el-input v-model="form.modeAccount" placeholder="请输入场景账号" />
        </el-form-item>
        <el-form-item label="场景key" prop="modeKey">
          <el-input v-model="form.modeKey" placeholder="请输入场景key" />
        </el-form-item>
        <el-form-item label="场景secret" prop="modeSecret">
          <el-input v-model="form.modeSecret" placeholder="请输入场景secret" />
        </el-form-item>
        <el-form-item label="保存周期(单位：天）默认90天" prop="preserveTime">
          <el-input v-model="form.preserveTime" placeholder="请输入保存周期(单位：天）默认90天" />
        </el-form-item>
        <el-form-item label="测试环境保存周期(单位：天）默认30天" prop="testPreserveTime">
          <el-input v-model="form.testPreserveTime" placeholder="请输入测试环境保存周期(单位：天）默认30天" />
        </el-form-item>
        <el-form-item label="场景描述" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预留字段，租户环境" prop="sceneEnvironment">
          <el-input v-model="form.sceneEnvironment" placeholder="请输入预留字段，租户环境" />
        </el-form-item>
        <el-form-item label="预留字段" prop="sceneField">
          <el-input v-model="form.sceneField" placeholder="请输入预留字段" />
        </el-form-item>
      </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>
  </div>
</template>

<script>
import { listScene, getScene, delScene, addScene, updateScene } from "@/api/business/scene";

export default {
  name: "Scene",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场景信息表格数据
      sceneList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sceneName: null,
        tenantId: null,
        sceneModeId: null,
        scenePic: null,
        defaultFlag: null,
        sceneStatus: null,
        authMode: null,
        modeAccount: null,
        modeKey: null,
        modeSecret: null,
        preserveTime: null,
        testPreserveTime: null,
        sceneEnvironment: null,
        sceneField: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sceneName: [
          { required: true, message: "场景名称不能为空", trigger: "blur" }
        ],
        sceneModeId: [
          { required: true, message: "场景类型，关联表hw_scene_mode的scene_mode_id不能为空", trigger: "blur" }
        ],
        defaultFlag: [
          { required: true, message: "是否默认不能为空", trigger: "blur" }
        ],
        sceneStatus: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        authMode: [
          { required: true, message: "认证方式不能为空", trigger: "blur" }
        ],
        modeKey: [
          { required: true, message: "场景key不能为空", trigger: "blur" }
        ],
        modeSecret: [
          { required: true, message: "场景secret不能为空", trigger: "blur" }
        ],
        preserveTime: [
          { required: true, message: "保存周期(单位：天）默认90天不能为空", trigger: "blur" }
        ],
        testPreserveTime: [
          { required: true, message: "测试环境保存周期(单位：天）默认30天不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询场景信息列表 */
    getList() {
      this.loading = true;
      listScene(this.queryParams).then(response => {
        this.sceneList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sceneId: null,
        sceneName: null,
        tenantId: null,
        sceneModeId: null,
        scenePic: null,
        defaultFlag: null,
        sceneStatus: null,
        authMode: null,
        modeAccount: null,
        modeKey: null,
        modeSecret: null,
        preserveTime: null,
        testPreserveTime: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        sceneEnvironment: null,
        sceneField: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sceneId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加场景信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sceneId = row.sceneId || this.ids
      getScene(sceneId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改场景信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sceneId != null) {
            updateScene(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScene(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sceneIds = row.sceneId || this.ids;
      this.$modal.confirm('是否确认删除场景信息编号为"' + sceneIds + '"的数据项？').then(function() {
        return delScene(sceneIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/scene/export', {
        ...this.queryParams
      }, `scene_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
