<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="系统名称" prop="identity">
        <el-input
          v-model.trim="queryParams.identity"
          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"
          v-hasPermi="['system:admin:add']"
        >新增</el-button>
        <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:admin:export']"
        >导出</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="data1"  show-overflow-tooltip />
      <el-table-column label="部署IP/域名" align="center" prop="data2" show-overflow-tooltip />
      <el-table-column label="服务器基本路径" align="center" prop="data3"  show-overflow-tooltip />
      <!-- <el-table-column label="创建时间" align="center" prop="data4"  show-overflow-tooltip /> -->
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            :disabled="!single"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:admin:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            :disabled="!single"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:admin:remove']"
          >删除</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="open1" 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="5">
            <el-upload
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-col>
          <el-col :span="16">
            <el-form-item label="系统名称" prop="data1" style="margin-top:50px">
              <el-input v-model.trim="form.data1" placeholder="请输入系统名称"  @input="onInput" maxlength="30" show-word-limit   />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-upload
            disabled
             style="opacity: 0;"
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-col>
          <el-col :span="16" >
            <el-form-item label="部署IP/域名" prop="data2" >
              <el-input v-model.trim="form.data2" placeholder="请输入部署IP/域名"  @input="onInput" maxlength="30" show-word-limit  />
            </el-form-item>
          </el-col>
          <el-col :span="5">
          </el-col>
          <el-col :span="16">
            <el-form-item label="服务器基本路径" prop="data3">
              <el-input v-model.trim="form.data3" placeholder="请输入服务器基本路径"  @input="onInput" maxlength="30" show-word-limit  />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-upload
            disabled
               style="opacity: 0;"
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-col>
          <el-col :span="16">
            <el-form-item label="参数结构" prop="data5">
              <el-input type="textarea" :rows="5" v-model.trim="form.data5" 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>
  </div>
</template>

<script>
// import { addRadio, updataRadio, delRadio, listRadio, updataStaff } from "@/api/recharge/radio";

export default {
  name: "admin",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 多选修改充值比例
      radio:{radioId:undefined},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [
        {
          data1:"安防综合管理平台",
          data2:"1021.39.1",
          data3:"http://10.21.39:8.82/#/",
          data4:"2022-5-18 15:00:00",
        },
        {
          data1:"安防综合管理平台1",
          data2:"1021.39.1",
          data3:"http://10.21.39:8.82/#/",
          data4:"2022-5-18 15:00:00",
        },
        {
          data1:"安防综合管理平台2",
          data2:"1021.39.1",
          data3:"http://10.21.39:8.82/#/",
          data4:"2022-5-18 15:00:00",
        }
      ],
      imageUrl: '',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open1: false,
      // 是否显示弹出层
      open2: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        identity: undefined,
        proportion: undefined,
      },
      // 表单参数
      form: {
        identity:'',
        proportion:''
      },
      // 表单校验
      rules: {
        identity: [
          { required: true, message: "身份信息不能为空", trigger: "blur" }
        ],
        proportion: [
          { required: true, message: "充值到账比例不能为空", trigger: "blur" }
        ],
      },
      //充值到账比例
      proportionList:[]
    };
  },
  created() {
    // this.getList();
    // this.proportionListF()
    
  },
  methods: {
    //强制el-input刷新
    onInput(){
        this.$forceUpdate();
    },
    proportionListF(){ //充值比例
      let arr = []
      for(var i = 50; i<=100;i++){
        arr.push(i)
      }
      this.proportionList = arr.map(item=>{
        return {
          value:Number((item / 100).toFixed(2))
        }
      })
    },
    forceUpdate(){ //重置form
      this.form = JSON.parse(JSON.stringify(this.form));
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listRadio(this.addDateRange(this.queryParams)).then(response => {
          this.adminList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open1 = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ids: undefined,
        name: undefined,
        aliasName: undefined,
        adminType: "Y",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open1 = true;
      this.title = "添加参数";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => {
        return{
          delFlag:0,
          staffId:item.id,
          radioId:undefined
        }
      })
      this.single = selection.length!=1
      this.multiple = !selection.length
      if(this.ids.length>1){
        this.single = false
      }else{
        this.single = true
      }

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      Object.assign(this.form, row.id ? this.adminList.find(val=>val.id === row.id) : this.adminList.find(val=>val.id === this.ids[0]))
      
      if(this.ids.length>1){
        this.open2 = true;
      }else{
        this.open1 = true;
      }
      this.title = "修改参数";
    },
    /** 提交按钮 */
    submitForm(row) {
      if(this.ids.length>1){
        this.$refs["radio"].validate(valid => {
          if (valid) {
            this.loading = true
            for(let i=0;i<this.ids.length;i++){
              this.ids[i].radioId = this.radio.radioId
            }
            updataStaff(this.ids).then(response => {
              this.loading = false
              this.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            }).catch(()=>{
              this.loading = false,
              this.open2 = false
            })
          }
        });
      }else{
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.loading = true
            if (this.form.id != undefined) {
              updataRadio(this.form).then(response => {
                this.loading = false
                this.msgSuccess("修改成功");
                this.open1 = false;
                this.getList();
              }).catch(()=>{
                this.loading = false,
                this.open1 = false
              })
            } else {
              addRadio(this.form).then(response => {
                this.loading = false
                this.msgSuccess("新增成功");
                this.open1 = false;
                this.getList();
              }).catch(()=>{
                this.loading = false,
                this.open1 = false
              })
            }
          }
        });
      }
      
      
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRadio(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
  }
};
</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>