
<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
          <el-form-item label="快捷站点" prop="url">
            <el-input
              v-model="queryParams.url"
              placeholder="请输入快捷站点"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="页面标识" prop="pageTag">
            <el-input
              v-model="queryParams.pageTag"
              placeholder="请输入页面标识"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!-- <el-form-item label="业务" prop="appId">
            <el-input
              v-model="queryParams.appId"
              placeholder="请"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item> -->
          <el-form-item label="业务" prop="appId">
            <el-select
              v-model="queryParams.appId"
              placeholder="请选择"
              clearable
              filterable
              @change='handChange'
              value-key='id'
            >
              <el-option
                v-for="app in appList"
                :key="app.id"
                :label="app.name"
                :value="app.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="全局可用">
            <el-radio-group v-model="queryParams.overall">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0" style='margin-lelft:-12px'>否</el-radio>
            </el-radio-group>
          </el-form-item>
          <!-- <el-form-item label="类型" prop="action">
            <el-select
              v-model="queryParams.action"
              placeholder="请选择类型"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            >
              <el-option value="GET">GET</el-option>
              <el-option value="POST">POST</el-option>
              <el-option value="PUT">PUT</el-option>
              <el-option value="DELETE">DELETE</el-option>
            </el-select>
          </el-form-item> -->

          <el-form-item style="margin-left:24px">
            <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-table
          v-loading="loading"
          :data="iframeList"
        >
          <!-- <el-table-column
            label="ID"
            header-align="center"
            align="left"
            prop="id"
          >
            <template slot-scope="scope">
              <span>{{scope.row.id}}</span>
            </template>
          </el-table-column> -->
          <el-table-column header-align="center"  label="PageTag" align="center" width='200' prop="pageTag"/>
          <el-table-column header-align="center"  label="Url" align="left" prop="url"/>
          <!-- <el-table-column header-align="center"  label="鉴权信息" align="left" prop="desc"/> -->
          <el-table-column
            label="全局可用"
            align="center"
            prop="overall"
            header-align="center" 
          >
            <template slot-scope="scope">
                <el-tag v-if="scope.row.overall ==1" type="success">是</el-tag>
                <el-tag v-else type="danger">否</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="120"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" :close-on-click-modal="false" style='margin-top:0;'>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
              <el-row>
                <el-col :span="24">
                  <el-form-item label="站点地址:" prop="url">
                    <el-input v-model="form.url" placeholder="请填入需要绑定的站点URL" />
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="鉴权信息:" prop="desc">
                    <el-input v-model="form.desc" placeholder="请填入鉴权信息 (如有)" />
                  </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>

      </el-card>
    </template>
  </BasicLayout>
</template>

<script>
import { addSysApi, delSysApi, getSysApi, listSysApi, updateSysApi } from '@/api/admin/sys-api'
import { listAllIframeUrl,updateIframeUrl} from "@/api/iframe-url"
import { listApps } from '@/api/apps'

export default {
  name: 'SysApiManage',
  components: {
  },
  data() {
    return {
      appList:[],
      dialog: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      isEdit: false,
      // 类型数据字典
      typeOptions: [],
      iframeList: [],
      dateRange: [],

      // 查询参数
      queryParams: {
        pageTag:'',
        appId: 0,
        url: '',
        overall:0,
        pageIndex: 1,
        pageSize: 10
      },
      // 表单参数
      form:{
        pageTag: '',
        appId: 0,
        url: '',
        desc: '',
        overall:0,
      },
      // 表单校验
      rules:{
        // pageTag: [{ required: true, message: '页面标识不能为空', trigger: 'blur' }],
        url: [{ required: true, message: 'url不能为空', trigger: 'blur' }]
      },
    }
  },
  created() {
    this.getList()
    this.getListApps();
  },
  methods: {
    handChange(val){
      // this.queryParams.appId = val.id;
      this.queryParams.pageIndex = 1
      this.getList()
    },
    getListApps(){
      listApps({pageIndex:0,pageSize:200}).then(res=>{
        if(res.code == 200){
          this.appList = res.data.list;
        }
      })
    },
    handleClose(done) {
      if (this.loading) {
        return
      }
      done()
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true
      listAllIframeUrl(this.queryParams).then(response => {
        this.iframeList = response.data.list
        this.total = response.data.count
        this.loading = false
      }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        pageTag: '',
        appId: 0,
        url: '',
        desc: '',
        overall:0,
      }
      this.resetForm('form')
    },
    parentIdFormat(row) {
      return this.selectItemsLabel(this.parentIdOptions, row.parentId)
    },
    // 文件
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加接口管理'
      this.isEdit = false
    },
    /** 排序回调函数 */
    handleSortChang(column, prop, order) {
      prop = column.prop
      order = column.order
      if (this.order !== '' && this.order !== prop + 'Order') {
        this.queryParams[this.order] = undefined
      }
      if (order === 'descending') {
        this.queryParams[prop + 'Order'] = 'desc'
        this.order = prop + 'Order'
      } else if (order === 'ascending') {
        this.queryParams[prop + 'Order'] = 'asc'
        this.order = prop + 'Order'
      } else {
        this.queryParams[prop + 'Order'] = undefined
      }
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      this.form = row;
      this.open = true;
      // getSysApi(id).then(response => {
      //   this.form = response.data
      //   this.open = true
      //   this.title = '修改接口管理'
      //   this.isEdit = true
      // })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateIframeUrl(this.form,this.form.id).then(response => {
              if (response.code === 200) {
                this.msgSuccess(response.msg)
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addSysApi(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(response.msg)
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var Ids = (row.id && [row.id]) || this.ids

      this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delSysApi({ 'ids': Ids })
      }).then((response) => {
        if (response.code === 200) {
          this.msgSuccess(response.msg)
          this.open = false
          this.getList()
        } else {
          this.msgError(response.msg)
        }
      }).catch(function() {})
    }
  }
}
</script>
