<template>
  <div>
    <template>
      <el-table :data="apiList" >
        <el-table-column label="ID编号" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.myId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商品信息" >
          <template slot-scope="scope">
            <el-button style="margin-left: 10px" @click="getApitypeList(scope.row.apiId)">展示商品信息</el-button>
            <el-form :model="apitypeList" v-if="apitypeList.sysId==scope.row.apiId">
              <el-form-item label="图像" prop="pass">
                <el-image :src="apitypeList.sysTypes" @click="gotoMaintained(customer)">
                  <div slot="placeholder" class="image-slot">
                    加载中<span class="dot">...</span>
                  </div>
                </el-image>
              </el-form-item>
              <el-form-item label="接口名称" prop="pass">
                <span>{{apitypeList.sysIntro}}</span>
              </el-form-item>
              <el-form-item label="元/次" prop="pass">
                <span>{{apitypeList.sysNumber}}</span>
              </el-form-item>
              <el-form-item label="产品编号" prop="pass">
                <span>{{apitypeList.sysId}}</span>
              </el-form-item>
              <el-form-item label="产品备注" prop="pass">
                <span>{{apitypeList.sysBeihzu}}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="剩余次数" >
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.myNum }}</span>
          </template>
        </el-table-column>
        <el-table-column label="购买时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.myTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="使用状态"  >
          <template slot-scope="scope">
            <el-switch
              style="float: right;padding: 5px; margin: 4px 5px 0;"
              v-model="scope.row.myStates"
              active-value='Y'
              inactive-value='N'
              active-color="#13ce66"
              inactive-color="#ff4949"
              :disabled="scope.row.myNum === 0"
              @change="handleStatusChange(scope.row)"
            >
            </el-switch>
          </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-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['api:api:remove']"
            >删除</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-order"
              :disabled="scope.row.myStates === 'N'"
              @click="jkwd(scope.row)"
            >接口文档</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="商品ID" prop="apiId">
          <el-input v-model="form.apiId" placeholder="请输入商品ID" />
        </el-form-item>
        <el-form-item label="剩余次数" prop="myNum">
          <el-input v-model="form.myNum" placeholder="请输入剩余次数" />
        </el-form-item>
        <el-form-item label="购买时间" prop="myTime">
          <el-date-picker clearable
            v-model="form.myTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择购买时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用状态" prop="myStates">
          <el-input v-model="form.myStates" 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>


    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form :model="unkownvalue" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="请输入" prop="pass">
          <el-input  v-model="unkownvalue" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="test">提交</el-button>
        </el-form-item>
      </el-form>
      <span>{{jsonData}}</span>
      <span slot="footer" class="dialog-footer"></span>
    </el-dialog>


    <el-dialog title="接口文档" :visible.sync="dialogTableVisible">
      <el-form :model="apimsg" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="接口描述" prop="pass">
          <span>{{apimsg.sysBeihzu}}</span>
        </el-form-item>
        <el-form-item label="接口地址" prop="pass">
          http://10.0.2.1/prod-api/market/apitype/third/<span>{{apimsg.sysId}}</span>/**
        </el-form-item>
        <el-form-item label="请求类型" prop="pass">
          <span>GET</span>
        </el-form-item>
        <el-form-item label="请求报文格式" prop="pass">
          <span>form</span>
        </el-form-item>
        <el-form-item label="请求报文" prop="pass">
          <el-table
            :data="reqmsg"
            style="width: 100%">
            <el-table-column label="名称" width="180">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.reqName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="类型" width="180">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.reqType }}</span>
              </template>
            </el-table-column>
            <el-table-column label="是否必填" width="180">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.reqYorn }}</span>
              </template>
            </el-table-column>
            <el-table-column label="描述信息" width="180">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.reqMsg }}</span>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="返回报文" prop="pass">
          <span>{{apimsg.sysSerial}}</span>
        </el-form-item>
      </el-form>
      <el-button @click="handleTabClick()">测试</el-button>
    </el-dialog>
  </div>
</template>

<script>
import {listApi, getApi, delApi, addApi, updateApi, upd} from "/src/api/api/api";
import {getAirQuality, getApitype} from "../../../api/market/apitype";
import {listMsg} from "../../../api/reqmsg/msg";

export default {
  name: "Api",
  data() {
    return {
      disableds:false,
      jsonData:{},
      unkownvalue:"",
      sysId:"",
      apimsg:{},
      reqmsg:[],
      reqmsgs:{},
      dialogTableVisible:false,
      dialogVisible:false,
      apitypeList:{},
      queryApitypeParams: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 我的api表格数据
      apiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        apiId: null,
        myNum: null,
        myTime: null,
        myStates: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      myapi:{},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleStatusChange(row){

    },
    getreqmsg(){
      listMsg(this.reqmsgs).then(res=>{
        this.reqmsg=res.data.rows
        console.log(this.reqmsg)
      })
    },
    jkwd(customer){
      this.myapi=customer
      this.reqmsgs.sysId=customer.apiId
      this.sysId=customer.apiId
      this.dialogTableVisible=true
      getApitype(customer.apiId).then(response => {
        console.log(response);
        this.apimsg = response.data;
      });
      this.getreqmsg()
    },
    handleTabClick() {
      this.dialogVisible=true
    },
    test(){
      getAirQuality(this.sysId,this.unkownvalue).then(response =>{
        console.log(response)
        this.jsonData=response
        if(response.reason!=null){
          this.jsonData=response.reason
        }
        if(response.result!=null){
          this.jsonData=response.result
        }
        this.dialogVisible=true
      })
      upd(this.myapi).then(resp=>{
        console.log(resp)
        this.getList
      })
    },
    /** 查询我的api列表 */
    getList() {
      this.loading = true;
      listApi(this.queryParams).then(response => {
        this.apiList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
        console.log(this.apiList)
      });
    },
    getApitypeList(apiId) {
      this.loading = true;
      getApitype(apiId).then(response => {
        console.log(response);
        this.apitypeList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        myId: null,
        userId: null,
        apiId: null,
        myNum: null,
        myTime: null,
        myStates: 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.myId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加我的api";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const myId = row.myId || this.ids
      getApi(myId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改我的api";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.myId != null) {
            updateApi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApi(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const myIds = row.myId || this.ids;
      this.$modal.confirm('是否确认删除我的api编号为"' + myIds + '"的数据项？').then(function() {
        return delApi(myIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('api/api/export', {
        ...this.queryParams
      }, `api_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
