<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <!-- <el-form-item label="所属系统" >
        <el-select
            v-model="queryParams.name"
            placeholder="请选择所属系统"
            @change="select"
            style="width: 150px"
            clearable
          >
            <el-option
              v-for="(item, index) in SubsystemList"
              :key="index"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
      </el-form-item> -->
      <el-form-item label="接口名称">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入接口名称"
          clearable
          size="small"
          style="width: 140px"
          @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 type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
        <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button> -->
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="adminList" @selection-change="handleSelectionChange" height="calc(100vh - 30vh)">
      <!-- <el-table-column label="系统名称" align="center" prop="name"  show-overflow-tooltip /> -->
      <el-table-column label="接口名称" align="center" prop="name" show-overflow-tooltip />
      <el-table-column label="接口链接" align="center" prop="url"  show-overflow-tooltip />
      <el-table-column label="状态" align="center" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enableFlag"
            active-color="#13ce66"
            inactive-color="#ff4949"
             @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"  show-overflow-tooltip />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <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="120px" >
        <el-row :gutter="20">
          <!-- <el-col :span="16">
            <el-form-item label="所属系统" prop="name1">
              <el-select
                v-model="form.data1"
                placeholder="请选择所属系统"
                @change="select"
                clearable
                style="width:245px"
              >
                <el-option
                  v-for="(item, index) in SubsystemList"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="16" >
            <el-form-item label="接口名称" prop="name" >
              <el-input v-model.trim="form.name" placeholder="请输入接口名称"  @input="onInput" maxlength="30" show-word-limit  />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="接口链接" prop="url">
              <el-input v-model.trim="form.url" placeholder="请输入接口连接"  @input="onInput" maxlength="30" show-word-limit  />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="接口类型" prop="data6">
              <el-switch
                  v-model="value1"
                  active-color="调用"
                  inactive-color="回调"
                ></el-switch>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="接口状态" prop="enableFlag">
              <el-switch
                  v-model="enableFlag"
                ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="参数结构" prop="params">
              <el-input type="textarea" :rows="5" v-model.trim="form.params" placeholder="请输入参数结构"  @input="onInput" maxlength="30" show-word-limit   />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!loading">确 定</el-button>
        <el-button :loading="loading" type="primary" @click="submitForm" v-else>提交中...</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 导出-->
    <a href="" download="" id="xz" style="position: absolute;z-index: -1;opacity: 0;"></a>
  </div>
</template>

<script>
import { addZkSystemDoc, delZkSystemDoc, updataZkSystemDoc, getZkSystemDoc, exportZkSystemDoc } from "@/api/business/Middleware/functionalInterface";
import { BASEPATH } from "@/assets/js/common.js";
export default {
  name: "admin",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [],
      SubsystemList:[
        {value:"安防",label:"1"},
        {value:"充值",label:"2"},
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 20,
        name:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "接口名称不能为空", trigger: "blur" }
        ],
        url: [
          { required: true, message: "接口链接不能为空", trigger: "blur" }
        ],
      },
      enableFlag:undefined
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //强制el-input刷新
    onInput(){
        this.$forceUpdate();
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getZkSystemDoc(this.queryParams).then(response => {
          this.adminList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.name = undefined
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.form =  JSON.parse(JSON.stringify(row))
      this.enableFlag = row.enableFlag
      this.title = "修改参数";
    },
    /** 提交按钮 */
    submitForm(row) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.enableFlag = this.enableFlag
          this.form.systemId =1
          this.loading = true
          if (this.form.id != undefined) {
            updataZkSystemDoc(this.form).then(response => {
              this.loading = false
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false,
              this.open = false
            })
          } else {
            addZkSystemDoc(this.form).then(response => {
              this.loading = false
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false,
              this.open = false
            })
          }
        }
      });
    },
    handleStatusChange(row){
      // 用户状态修改
      let text = row.enableFlag === true ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.name + '"接口吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          updataZkSystemDoc(row).then(response => {
          }).catch(()=>{
          })
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id
      this.$confirm('是否确认删除', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delZkSystemDoc(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      let queryParams = {...this.queryParams};
      delete queryParams.current
      delete queryParams.size
      this.exportLoading = true;
      exportZkSystemDoc(queryParams).then(response =>{
        this.exportLoading = false;
        document.getElementById("xz").href = BASEPATH() + response.data
        document.getElementById("xz").download = response.data
        document.getElementById("xz").click()
      }).catch(()=>{
        this.exportLoading = false;
      })
    }
  }
};
</script>
<style>
.el-upload{
  border: 1px dashed #ccc !important;
  width:100px;
  height:100px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #ccc !important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 50px;
    height: 50px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
