<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="name">
        <el-input
          v-model="queryParams.name"
          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="['system:foreign: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="['system:foreign: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="['system:foreign: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="['system:foreign:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="foreignList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单位名称" align="center" prop="name" />
      <el-table-column label="秘钥" align="center" prop="secretKey" />
      <el-table-column label="放行IP" align="center" prop="ip" />
      <el-table-column label="能获取的设备数据id" align="center" prop="equipId" />
      <el-table-column label="每天可调用次数(-1时为不限次数）" align="center" prop="number" width="128"/>
      <el-table-column label="每天调用剩余次数" align="center" prop="remainingNumber" />
      <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="['system:foreign:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:foreign: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="140px">
        <el-row>
          <el-col :span="24">
          <el-form-item label="单位名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入单位名称" />
          </el-form-item>
          </el-col>
          <el-col :span="24"  v-for="(item, index) in form.equipIdList" :key="index">
            <el-col :span="22">
              <el-form-item label="能获取的设备数据id" >
                <el-input v-model="item.equipId"  placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          <el-col :span="2">
            <div class="hyeoa" style="margin-top: 10px;">
            	 <el-tooltip class="item" effect="dark" content="新增能获取的设备数据id信息,请慎重操作" placement="top-start">
            	      <img src="../../../assets/images/icon_tc_add.png" alt="" class="hueyde" @click="tijea(index)" />
            	</el-tooltip>
            		<el-tooltip class="item" effect="dark" content="删除无法恢复,请慎重操作" placement="top-start">
            		     <img src="../../../assets/images/delei.png" alt="" class="hueydele" @click="naeyrfakjf(index, item)" />
            		</el-tooltip>
            </div>
          </el-col>
          </el-col>
          <el-col :span="24"  v-for="(item, index) in form.ipList" :key="item.ip">
            <el-col :span="22">
              <el-form-item label="放行IP(必填)" >
                <el-input v-model="item.ip" placeholder="请输入放行IP" />
              </el-form-item>
            </el-col>
          <el-col :span="2">
            <div class="hyeoa" style="margin-top: 10px;">
            	 <el-tooltip class="item" effect="dark" content="新增放行IP,请慎重操作" placement="top-start">
            	      <img src="../../../assets/images/icon_tc_add.png" alt="" class="hueyde" @click="tijeaip(index)" />
            	</el-tooltip>
            		<el-tooltip class="item" effect="dark" content="删除无法恢复,请慎重操作" placement="top-start">
            		     <img src="../../../assets/images/delei.png" alt="" class="hueydele" @click="naeyrfakjfip(index, item)" />
            		</el-tooltip>
            </div>
          </el-col>
          </el-col>
          <el-col :span="24">
          <el-form-item label="每天可调用次数" prop="number">
            <el-input type="number" v-model="form.number" placeholder="请输入每天可调用次数(-1时为不限次数）" />
          </el-form-item>
          </el-col>
          <el-col :span="24">
          <el-form-item label="每天调用剩余次数" prop="remainingNumber">
            <el-input type="number" v-model="form.remainingNumber" 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>
  </div>
</template>

<script>
import { listForeign, getForeign, delForeign, addForeign, updateForeign,delseeug } from "@/api/system/foreign";

export default {
  name: "Foreign",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对外接口调用配置表格数据
      foreignList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },

      // 表单参数
      form: {
        ipList:[{ip:''}],
        equipIdList:[{equipId:''}],
        },
      // 表单校验
      rules: {
        name:[{ required: true, message: '不能为空', trigger: 'blur' }],
        number:[{ required: true, message: '不能为空', trigger: 'blur' }],
        remainingNumber:[{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      jdourdjoaj:false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询对外接口调用配置列表 */
    getList() {
      this.loading = true;
      listForeign(this.queryParams).then(response => {
        this.foreignList = response.rows;
        this.foreignList.filter( router=>{
         console.log(router.equipIdList)
        })
        console.log( this.foreignList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        secretKey: null,
        ipList:[{ip:''}],
        equipIdList:[{equipId:''}],
        number: null,
        remainingNumber: 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.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.jdourdjoaj = true
      this.title = "添加对外接口调用配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getForeign(id).then(response => {
        this.form = response.data;
        this.open = true;
         this.jdourdjoaj = false
        this.title = "修改对外接口调用配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateForeign(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addForeign(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除对外接口调用配置编号为"' + ids + '"的数据项？').then(function() {
        return delForeign(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/foreign/export', {
        ...this.queryParams
      }, `foreign_${new Date().getTime()}.xlsx`)
    }
    ,
    // 点击新增
    tijea(index) {
    	console.log(index+ 1)
    	this.form.equipIdList.push({equipId:''});
    },
    // 点击删除
    naeyrfakjf(index, item) {
    	var that = this
    	this.$confirm('是否确认删除信息?', '警告', {
    		confirmButtonText: '确定',
    		cancelButtonText: '取消',
    		type: 'warning'
    	})
    		.then(function() {
    			return that.delseeug(index,item)
    		})
    		.then(() => {
    			// this.getList();
    			// this.msgSuccess('删除成功');
    		});

    },
    delseeug(index,item){
    	console.log(234)
    	if (this.form.equipIdList.length == 1) {
        this.$modal.msgSuccess('只剩一条数据，无法删除');
    		return false;
    	}
    	if (this.jdourdjoaj == true) {
    		// 新增
    		this.form.equipIdList.splice(index, 1);
    	} else {
    		// 修改
    		this.form.equipIdList.splice(index, 1);
    	}
      this.$modal.msgSuccess('删除成功');
    },
    // 点击新增
    tijeaip(index) {
    	console.log(index+ 1)
    	this.form.ipList.push({ip:''});
    },
    // 点击删除
    naeyrfakjfip(index, item) {
    	var that = this
    	this.$confirm('是否确认删除信息?', '警告', {
    		confirmButtonText: '确定',
    		cancelButtonText: '取消',
    		type: 'warning'
    	})
    		.then(function() {
    			return that.delseeugip(index,item)
    		})
    		.then(() => {
    			// this.getList();
    			// this.msgSuccess('删除成功');
    		});

    },
    delseeugip(index,item){
    	console.log(234)
    	if (this.form.ipList.length == 1) {
        this.$modal.msgSuccess('只剩一条数据，无法删除');
    		return false;
    	}
    	if (this.jdourdjoaj == true) {
    		// 新增
    		this.form.ipList.splice(index, 1);
    	} else {
    		// 修改
    		this.form.ipList.splice(index, 1);
    	}
      this.$modal.msgSuccess('删除成功');
    },
  }
};

</script>
<style type="text/css">
  .hyeoa {
  	height: 100%;
  	display: flex;
  	align-items: center;
  }
  .hueyde {
  	width: 18px;
  	height: 18px;
  	margin: 0 5px;
  	margin-right: 10px;
  }
  .hueydele {
  	width: 15px;
  	height: 18px;
  }
</style>
