<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="是否启用" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择是否启用"
          clearable
          filterable
        >
          <el-option
            v-for="dict in statusList"
            :key="dict.value + 'statusList'"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接口链接状态" prop="linkStatus">
        <el-select
          v-model="queryParams.linkStatus"
          placeholder="请选择接口链接状态"
          clearable
          filterable
        >
          <el-option
            v-for="dict in linkStatusList"
            :key="dict.value + 'linkStatusList'"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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:APICONFIG: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:APICONFIG: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:APICONFIG:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          icon="el-icon-upload"
          @click="openUpload = true"
          v-hasPermi="['business:APICONFIG:export']"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          size="mini"
          icon="el-icon-download"
          @click="handleDownload()"
          :disabled="radio === '' ? true : false"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      
      :data="APICONFIGList"
      @selection-change="handleSelectionChange"
      @row-click="showRow"
    >
    <el-table-column label="选择" width="70" center>
        <template scope="scope">
          <el-radio
            class="radio"
            v-model="radio"
            :label="scope.$index"
            @change.native="getCurrentRow(scope.row)"
            >{{ "" }}</el-radio
          >
        </template>
      </el-table-column>
      <el-table-column label="接口编号" align="center" prop="apiCode">
        <template slot-scope="scope">
          {{ scope.row.apiCode }}
        </template>
      </el-table-column>
      <el-table-column label="接口名称" align="center" prop="apiName">
        <template slot-scope="scope">
          {{ scope.row.apiName }}
        </template>
      </el-table-column>
      <el-table-column label="接口url" align="center" prop="apiUrl">
        <template slot-scope="scope">
          {{ scope.row.apiUrl }}
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">启用</span>
          <span v-else-if="scope.row.status == 0">不启用</span>
        </template>
      </el-table-column>
      <el-table-column label="配置协议" align="center" prop="accord">
        <template slot-scope="scope">
          {{ scope.row.accord }}
        </template>
      </el-table-column>
      <el-table-column
        label="是否启用分布式线程"
        align="center"
        prop="hadoopFlag"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.hadoopFlag == 1">启用</span>
          <span v-else-if="scope.row.hadoopFlag == 0">不启用</span>
        </template>
      </el-table-column>
      <el-table-column label="是否数据加密" align="center" prop="thickenFlag">
        <template slot-scope="scope">
          <span v-if="scope.row.thickenFlag == 1">启用</span>
          <span v-else-if="scope.row.thickenFlag == 0">不启用</span>
        </template>
      </el-table-column>
      <el-table-column label="接口链接状态" align="center" prop="linkStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.linkStatus == 1">成功</span>
          <span v-else-if="scope.row.linkStatus == 0">失败</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark">
        <template slot-scope="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>
      <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:APICONFIG:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:APICONFIG: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"
    />

    <!-- 添加或修改数据接口api配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="接口编号" prop="apiCode">
          <el-input
            v-model="form.apiCode"
            placeholder="请输入接口编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="接口名称" prop="apiName">
          <el-input
            v-model="form.apiName"
            placeholder="请输入接口名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="接口url" prop="apiUrl">
          <el-input
            v-model="form.apiUrl"
            placeholder="请输入接口url"
          ></el-input>
        </el-form-item>

        <el-form-item label="是否启用" prop="status">
          <el-select
            v-model="form.status"
            placeholder="请选择是否启用"
            clearable
            filterable
          >
            <el-option
              v-for="dict in statusList"
              :key="dict.value + 'statusList'"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="配置协议" prop="accord">
          <el-input
            v-model="form.accord"
            placeholder="请输入配置协议"
          ></el-input>
        </el-form-item>

        <el-form-item label="是否启用分布式线程">
          <el-radio-group v-model="form.hadoopFlag">
            <el-radio
              v-for="dict in hadoopFlagList"
              :key="dict.value + 'hadoopFlagList'"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否数据加密">
          <el-radio-group v-model="form.thickenFlag">
            <el-radio
              v-for="dict in thickenFlagList"
              :key="dict.value + 'thickenFlagList'"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="接口链接状态">
          <el-radio-group v-model="form.linkStatus">
            <el-radio
              v-for="dict in linkStatusList"
              :key="dict.value + 'linkStatusList'"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
        </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>


    <!-- 上传文件对话框 -->
    <el-dialog
      center
      title="导入"
      :visible.sync="openUpload"
      width="50%"
      append-to-body
    >
      <el-form
        ref="uploadForm"
        :model="uploadForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="文件" prop="linkUrl">
          <el-upload
            class="upload-demo"
            ref="upload"
            action="#"
            :http-request="httprequest"
            :before-upload="uploadFile"
            :file-list="fileList"
            :auto-upload="false"
            :limit="1"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件</el-button
            >
          </el-upload>
         
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUpload">导 入</el-button>
        <el-button @click="cancel">取 消</el-button>
        <!-- <el-button @click="cancel">取 消</el-button> -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAPICONFIG,
  getAPICONFIG,
  delAPICONFIG,
  addAPICONFIG,
  updateAPICONFIG,
  exportAPICONFIG, //导出
  uploadAPICONFIG, //导入
  upload,
} from "@/api/portal/APICONFIG/APICONFIG.js";

import { saveAs } from "file-saver";
import { getToken } from "@/utils/auth";
import axios from "axios";
//我好困

export default {
  name: "APICONFIG",
  data() {
    return {
      fileList:[],
      uploadForm:{
        fileName:''
      },
      openUpload: false,
      //是否启用(1=启用,0=不启用)
      statusList: [
        { label: "启用", value: "1" },
        { label: "不启用", value: "0" },
      ],
      //接口链接状态(1=成功,0=失败)
      linkStatusList: [
        { label: "成功", value: "1" },
        { label: "失败", value: "0" },
      ],
      //是否启用分布式线程
      hadoopFlagList: [
        { label: "启用", value: "1" },
        { label: "不启用", value: "0" },
      ],
      //是否数据加密
      thickenFlagList: [
        { label: "启用", value: "1" },
        { label: "不启用", value: "0" },
      ],
      //接口链接状态
      thickenFlagList: [
        { label: "成功", value: "1" },
        { label: "失败", value: "0" },
      ],

      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      rowData: {},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据接口api配置表格数据
      APICONFIGList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        deptId: null,
        apiCode: null,
        apiName: null,
        apiUrl: null,
        request: null,
        status: null,
        accord: null,
        hadoopFlag: null,
        thickenFlag: null,
        linkStatus: null,
        remark: null,
      },
      radio:'',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "$comment不能为空", trigger: "blur" }],
        delFlag: [
          { required: true, message: "删除标识不能为空", trigger: "blur" },
        ],
        deptId: [
          { required: true, message: "部门标识不能为空", trigger: "blur" },
        ],
        apiCode: [
          { required: true, message: "接口编号不能为空", trigger: "blur" },
        ],
        apiName: [
          { required: true, message: "接口名称不能为空", trigger: "blur" },
        ],
        apiUrl: [
          { required: true, message: "接口url不能为空", trigger: "blur" },
        ],
        request: [
          { required: true, message: "请求参数示例不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "是否启用(1=启用,0=不启用)不能为空",
            trigger: "blur",
          },
        ],
        accord: [
          { required: true, message: "配置协议不能为空", trigger: "blur" },
        ],
        hadoopFlag: [
          {
            required: true,
            message: "是否启用分布式线程(1=启用,0=不启用)不能为空",
            trigger: "blur",
          },
        ],
        thickenFlag: [
          {
            required: true,
            message: "是否数据加密(1=启用,0=不启用)不能为空",
            trigger: "blur",
          },
        ],
        linkStatus: [
          {
            required: true,
            message: "接口链接状态(1=成功,0=失败)不能为空",
            trigger: "blur",
          },
        ],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    submitUpload() {
      this.$refs.upload.submit();
    },
    //导入
    uploadFile(file) {
      let formData = new FormData();
      formData.append("file", file);
      formData.append("type", this.uploadForm.type);
      uploadAPICONFIG(formData)
      this.cancel()
      this.openUpload = false
    },
    httprequest() {},
    /** 查询数据接口api配置列表 */
    getList() {
      this.loading = true;
      listAPICONFIG(this.queryParams).then((response) => {
        this.APICONFIGList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openUpload = false;
      this.reset();
    },
    getCurrentRow(val) {
      console.log(val);
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        deptId: null,
        apiCode: null,
        apiName: null,
        apiUrl: null,
        request: null,
        status: null,
        accord: null,
        hadoopFlag: null,
        thickenFlag: null,
        linkStatus: null,
        remark: null,
      };
      this.fileList = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据接口api配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAPICONFIG(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据接口api配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAPICONFIG(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAPICONFIG(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('确定删除吗?')
        .then(function () {
          return delAPICONFIG(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 点击导入按钮操作 */
    handleupload() {
      uploadAPICONFIG(this.uploadForm.fileName)
      this.cancel()
      this.openUpload = false
    },
    /** 导出按钮操作 */
    // handleExport(id) {
    //   exportAPICONFIG(id).then(res=>{
        
    //   })
    // },
      /** 导出按钮操作 */
      showRow(row) {
      //赋值给radio
      this.radio = this.APICONFIGList.indexOf(row);
      this.rowData = row;
    },
    handleDownload() {
      let row = this.rowData;
      axios({
        url: process.env.VUE_APP_BASE_API + "/APICONFIG/export/",
        method: "post",
        responseType: "blob",
        data:row,
        headers: {
          Authorization: "Bearer " + getToken(),
        },
      }).then((res) => {
        // const temp = res.headers["content-disposition"]
        //   .split(";")[1]
        //   .split("filename=")[1];
        // const fileName = decodeURIComponent(temp);
        const blob = new Blob([res.data]);
        saveAs(blob, row.apiName+'.xlsx');
        this.getList();
      });
    },
    
  },
};
</script>
