<template>
  <div class="yqrange-index-wrapper">
    <!--<bread-crumb :curmbFirst="curmbFirst"></bread-crumb>-->
    <div class="yqrange-index-content" id="screenSet">
      <!--<div class="header-title">云鹊小圈</div>-->

      <!-- <el-form ref="settingForm" :model="settingForm"
               label-width="150px"
               label-suffix="："
               class="basic-form">

        <div v-for="(item, index) in settingForm.settingData" :key="index" display:inline>
          <el-form-item :label="item.appType">
            <el-radio-group size="small" v-model="item.showOff">
              <el-radio :label="1">开启</el-radio>
              <el-radio :label="0">关闭</el-radio>
            </el-radio-group>

            <el-input v-model="item.appName" style="width:120px;margin-left:40px;" size="small" placeholder="重命名"/>

            <el-input v-model="item.seqNo" style="width:60px;margin-left:40px;" size="small" placeholder="序号"/>
          </el-form-item>
        </div>
      </el-form> -->

      <el-table :data="settingForm.settingData" class="customer-table" style="margin-left:70px;"
        :header-cell-style="{background:'#ffffff'}">
        <el-table-column label="" prop="appType" width="120"></el-table-column>
        <el-table-column label="" width="180">
          <template slot-scope="scope">
            <el-radio-group size="small" v-model="scope.row.showOff">
              <el-radio :label="1">开启</el-radio>
              <el-radio :label="0">关闭</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
        <el-table-column label="重命名" width="150">
          <template slot-scope="scope">
            <el-input v-model="scope.row.appName" style="width:120px;" :maxlength="6" size="small" placeholder="重命名"/>
          </template>
        </el-table-column>
        <el-table-column label="序号" width="200">
          <template slot-scope="scope">
            <el-input v-model="scope.row.seqNo" style="width:60px;" size="small" @blur="BlurText($event)" @placeholder="序号"/>
          </template>
        </el-table-column>
      </el-table>

      <div style="margin-top: 60px;margin-left: 150px;">
        <el-button type="primary" @click="checkSubmitData()" center>保存</el-button>
        <!--<el-button @click="cancle()">取 消</el-button>-->
      </div>

    </div>
  </div>
</template>

<script>
import { openLoading, closeLoading } from "../../utils/utils";
import BreadCrumb from "@/components/breadcrumb.vue";
import { getRangeList,updateRangeStatus, getCircleSettingList, saveCircleSetting} from "../../utils/yqrange/yqrangeApi";
import { isEmptyUtils } from "../../utils/index";

export default {
  components: {
    BreadCrumb
  },
  data(){
    return{
      curmbFirst: '云鹊小圈',
      showAllFlag: false,
      showNewFlag: false,
      searchForm: {
        name: '',
        createdTime: '',
        // endTime: '',
        userType: "", // 用户标识 1：内部用户 2：外部用户,
        status: "",
        pageNo: 1,
        pageSize: 10,
      },
      totalRows: 0,
      tableData: [],
      dialogSettingVisible: false,
      settingForm:{
        circleId: null,
        settingData: []
      }
    }
  },
  created() {
    this.circleId = this.$route.query.id;
    this.setting();
  },
  methods: {

    // 应用配置
    setting(){
      let params = {
        circleId: this.circleId
        // circleId: 22
      }

      getCircleSettingList(params).then((res) => {
        closeLoading(this);
        if(res.code == "000000") {
          this.settingForm.settingData = res.data;
        } else {
          this.settingForm.settingData = [];
        }
      }).catch((error) => {
        this.$message.error("请重试");
      })

    },

    checkSubmitData() {
      for(let i=0; i<this.settingForm.settingData.length; i++) {
        if (isEmptyUtils(this.settingForm.settingData[i].appName)) {
          this.$message.error("重命名输入框中的值不可为空，请检查");
          return;
        }
        if (isEmptyUtils(this.settingForm.settingData[i].seqNo)) {
          this.$message.error("序号不可为空，请检查");
          return;
        }
        const r = /^\+?[1-9][0-9]*$/; // 正整数
        // 如果判断不符合正则，则不是正整数不能提交
        if (!r.test(this.settingForm.settingData[i].seqNo)) {
          return;
        }
        if (this.settingForm.settingData[i].seqNo < 1 || this.settingForm.settingData[i].seqNo > 10) {
          return;
        }
        if (this.settingForm.settingData[i].seqNo > this.settingForm.settingData.length) {
          this.$message.error("序号必须从1开始顺序排列，不能有跳序情况，请检查");
          return;
        }
        for(let j=0; j<this.settingForm.settingData.length; j++) {
          if (i == j) {
            continue;
          }
          if (this.settingForm.settingData[i].seqNo == this.settingForm.settingData[j].seqNo) {
            this.$message.error("序号不可重复，请检查");
            return;
          }
        }
      }
      this.save();
    },

    // 保存
    save(){
      let params = {
        apps: this.settingForm.settingData,
        circleId: this.circleId
      }

      saveCircleSetting(params).then((res) => {
        closeLoading(this);
        if(res.code == "000000") {
          this.$message.success("保存成功！");
          // this.$router.go(-1);
          this.setting();
        }
      }).catch((error) => {
      this.$message.error("请重试");
      })
    },

    cancle(){
      this.$router.go(-1);
    },

    BlurText(e) {
      console.log("BlurText() : e = " + e.target.value)
      let b = new RegExp("^[1-9][0-9]*$").test(e.target.value)
      if (e.target.value >= 10) {
        b = false
      }
      if (!b) {
        this.$message.warning('请输入 1~10 的正整数')
        e.target.value = ''
      }
    }
  },
}
</script>
<style lang="scss">
  .yqrange-index-wrapper{
    .yqrange-index-content{
      background: #fff;
      padding: 10px;
      .header-title{
        padding: 10px 12px;
        font-size: 12px;
        color: #449284;
        border-bottom: 1px solid #efefef;
      }
    }
    .el-button--text{
      color: #449284;
      font-size: 14px;
      &::after{
        content: '';
        position: relative;
        height: 14px;
        /*border-right: 1px solid #EBEEF5;*/
        border-right: 1px solid #aaaaaa;
        padding-right: 10px;
      }
      &:last-of-type{
        &::after{
          content: '';
          position: relative;
          width: 1px;
          height: 14px;
          border-right: none;
        }
      }
    }
  }
  /*.range-make-box{*/
    /*.el-message-box__btns{*/
      /*margin-top: 30px;*/
    /*}*/
  /*}*/
  // 去掉表格单元格边框
.customer-table th{
    border:none;
  }
.customer-table td,.customer-table th.is-leaf {
  border:none;
}
// 表格最外边框
.el-table--border, .el-table--group{
  border: none;
}
// 头部边框
.customer-table thead tr th.is-leaf{
  width: 0;
}
.customer-table thead tr th:nth-last-of-type(2){
  width: 0;
}
// 表格最外层边框-底部边框
.el-table--border::after, .el-table--group::after{
  width: 0;
}
.customer-table::before{
  width: 0;
}
.customer-table .el-table__fixed-right::before,.el-table__fixed::before{
  width: 0;
}
// 表格有滚动时表格头边框
.el-table--border th.gutter:last-of-type {
    width: 0;
}
</style>
