<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: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="充值到账比例" prop="proportion">
        <el-input
          v-model="queryParams.proportion"
          placeholder="请输入充值到账比例"
          clearable
          size="small"
          style="width: 240px"
          @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:admin: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:admin: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:admin:remove']"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:admin:export']"
        >导出</el-button>
      </el-col>-->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="adminList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="身份信息" align="center" prop="identity" />
      <el-table-column label="充值到账比例" align="center" prop="proportion"/>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </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"
            :disabled="!single"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:admin:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            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-form-item label="身份信息" prop="identity">
          <el-input v-model.trim="form.identity" placeholder="请输入身份信息"  @input="forceUpdate" />
        </el-form-item>
        <el-form-item label="充值到账比例" prop="proportion">
          <!-- <el-input v-model.trim="form.proportion" placeholder="请输入充值到账比例(例:0.95)"  @input="forceUpdate"/> -->
          <el-select v-model="form.proportion"  placeholder="请选择充值到账比例" clearable size="small" @input="forceUpdate">
            <el-option :label="index.value"  :value="index.value" v-for = "(index, item) in proportionList" :key="item" />
          </el-select>
        </el-form-item>
      </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>
    <el-dialog  :title="title" :visible.sync="open2" width="600px" append-to-body :before-close="cancel" :close-on-click-modal="false">
      <el-form ref="radio" :model="radio" :rules="rules" label-width="120px" >
        <el-form-item label="充值到账比例" prop="radioId">
          <el-select v-model="radio.radioId"  placeholder="请选择充值到账比例" clearable size="small" @input="forceUpdate">
            <el-option :label="index.value"  :value="index.value" v-for = "(index, item) in proportionList" :key="item" />
          </el-select>
        </el-form-item>
      </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: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 多选修改充值比例
      radio:{radioId:undefined},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [],
      // 弹出层标题
      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: {
    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>