<!--认证授权-->
<template>
  <div class="app-container certificationAccredit">
    <div class="page-container certificationAccreditPage">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="searchValue" label-width="80px">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="名称"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="state" label-width="60px">
            <el-select v-model="queryParams.state" clearable placeholder="请选择状态" style="width: 200px">
              <el-option :value="true" label="启用"></el-option>
              <el-option :value="false" label="停用"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="dateRange">
            <el-date-picker
              :clearable="false"
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="创建时间"
              end-placeholder="创建时间"
            ></el-date-picker>
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="dialogOpen"
             v-hasPermiRouter="['auth:license:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="tableList" ref="multipleTable">
          <el-table-column label="平台/企业" align="center" prop="customerName" show-overflow-tooltip/>
          <el-table-column label="appId" align="center" prop="appId" show-overflow-tooltip width="200">
            <template slot-scope="scope">
              <p class="clickCopy" @click="clickCopy(scope.row.appId,'appId')">{{scope.row.appId}}</p>
            </template>
          </el-table-column>
          <el-table-column label="密钥" align="center" prop="secret" show-overflow-tooltip width="300">
            <template slot-scope="scope">
              <p class="clickCopy" @click="clickCopy(scope.row.secret,'密钥')">{{scope.row.secret}}</p>
            </template>
          </el-table-column>
          <el-table-column label="授权类型" align="center" prop="grantType" show-overflow-tooltip width="120">
            <template slot-scope="scope">
              {{scope.row.grantType == 1?'指定时间':'永久有效'}}
            </template>
          </el-table-column>
          <el-table-column label="授权时间" align="center" prop="createTime" show-overflow-tooltip width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="过期时间" align="center" prop="expireTime" show-overflow-tooltip width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.expireTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="state" show-overflow-tooltip width="80">
            <template slot-scope="scope">
              {{scope.row.state?'启用':'停用'}}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" prop="deptName" width="200"
                           v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="tableButton(1,scope.row)"
                   v-hasPermiRouter="['auth:license:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(2,scope.row)"
                   v-hasPermiRouter="['auth:license:edit']"
                >重置密钥</p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['auth:license:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <!--新增弹窗-->
    <el-dialog class="certificationAccreditDialog" :title='dialogTitle' width="560px" append-to-body
               :visible.sync="dialogType" v-if="dialogType" @close="dialogOff()"
               :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form  v-loading="loading" :rules="dialogRules"
                :model="dialogForm" ref="dialogForm" label-width="100px">
        <el-form-item label="平台/企业" prop="customerId">
          <el-select
            :disabled="dialogForm.id"
            style="width:400px;"
            v-model="dialogForm.customerId"
            filterable
            remote
            clearable
            reserve-keyword
            placeholder="请输入平台/企业名称"
            :remote-method="authCustomerDropList"
            :loading="loading">
            <el-option
              v-for="item in dropList"
              :key="item.customerId"
              :label="item.customerName"
              :value="item.customerId">
            </el-option>
          </el-select>
        </el-form-item>
        <div class="form-table-box">
          <div class="form-table-title-box">
            <span>*</span>
            授权接口
          </div>
          <div class="for-api-max-box scrollbar-box">
            <el-form-item label=""  label-width="0"
                          :prop="'grantApis.'+ index +'.api'" :rules="dialogRules.api"
                          v-for="(item,index) in dialogForm.grantApis" :key="index">
              <div class="for-api-box">
                <el-input
                  v-model="item.api"
                  maxLength="100"
                  placeholder="请输入授权接口"
                  clearable
                  style="width: 300px"
                />
                <p class="el-icon-circle-plus-outline" @click="addApi"></p>
                <p class="el-icon-remove-outline" @click="delApi(index)" v-if="dialogForm.grantApis[1]"></p>
              </div>
            </el-form-item>
          </div>
        </div>
        <el-form-item label="授权类型" prop="grantType">
          <el-select v-model="dialogForm.grantType" clearable placeholder="请选择授权类型" style="width: 400px">
            <el-option v-for="item in grantTypeList" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="过期时间" prop="expireTime" v-if="dialogForm.grantType == 1">
          <el-date-picker
            v-model="dialogForm.expireTime"
            value-format="yyyy-MM-dd"
            type="date"
            style="width:400px;"
            :picker-options="pickerOptions"
            placeholder="请选择过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="state" style="width:400px;">
          <el-radio v-model="dialogForm.state" :label="true">启用</el-radio>
          <el-radio v-model="dialogForm.state" :label="false">禁用</el-radio>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="dialogForm.remark"
            maxLength="20"
            placeholder="请输入备注"
            clearable
            style="width: 400px"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="dialogOff()">取消</p>
        <p class="dialog-footer-button-primary" @click="dialogSubmit">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { authLicenseList,authLicenseDelete,authLicenseDetail,authLicenseSetup,authLicenseSecret } from "@/api/serviceCenter/index";
  import { authCustomerDropList } from "@/api/commonality/permission";
  export default {
    name: 'index',
    data(){
      return{
        tableButtonType:this.hasPermiDom(['auth:license:edit','auth:license:detail','auth:license:del']),
        loading:false,
        optionList:[{label:"启用",value:true},{label:"禁用",value:false}],
        grantTypeList:[{label:"指定时间",value:1},{label:"永久有效",value:2}],
        dropList:[],
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:"",
          state:null,
        },
        dateRange:[],
        tableList:[],
        total:0,
        //dialog弹层数据
        dialogTitle:"",
        dialogForm:{},
        dialogRules:{
          customerId: [
            { required: true, message: "请选择平台/企业", trigger: "blur" },
            { required: true, message: "请选择平台/企业", validator: this.spaceJudgment, trigger: "blur" }
          ],
          api: [
            { required: true, message: "请输入授权接口", trigger: "blur" },
            { required: true, message: "请输入授权接口", validator: this.spaceJudgment, trigger: "blur" }
          ],
          grantType: [
            { required: true, message: "请选择授权类型", trigger: "blur" },
          ],
          expireTime: [
            { required: true, message: "请选择过期时间", trigger: "blur" },
          ],
          state: [
            { required: true, message: "请选择状态", trigger: "blur" },
          ],
        },
        dialogType:false,
        // 设置只能选择当前日期及之后的日期
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7;//如果没有后面的-8.64e7就是不可以选择今天的
          }
        },
      }
    },
    created(){
      this.authCustomerDropList();
      this.getList();
    },
    mounted(){

    },
    methods:{
      //弹层关闭
      dialogOff(){
        this.$set(this,'dialogType',false);
      },
      //弹层开启
      dialogOpen(){
        this.dialogFormReset();
        this.$set(this,'dialogTitle','添加授权');
        this.$set(this,'dialogType',true);
      },
      //弹层确定
      dialogSubmit(){
        let self = this;
        this.$refs["dialogForm"].validate(valid => {
          if (valid) {
            let obj = {
              customerId:this.dialogForm.customerId,
              grantType:this.dialogForm.grantType,
              expireTime:this.dialogForm.grantType == 1 ? this.dialogForm.expireTime + 'T23:59:59' : '2099-12-31T23:59:59',
              state:this.dialogForm.state,
              remark:this.dialogForm.remark,
            };
            obj.grantApis = [];
            for(let i=0;i<self.dialogForm.grantApis.length;i++){
              obj.grantApis.push(self.dialogForm.grantApis[i].api);
            }
            obj.grantApis = obj.grantApis + '';
            if(this.dialogForm.id){
              obj.id = this.dialogForm.id
            }
            authLicenseSetup(obj).then(response => {
              this.msgSuccess(response.message)
              this.getList();
              this.$set(this,'dialogType',false);
            });
          }
        })
      },
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      resetQuery(){
        this.$set(this,'dateRange',[])
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:"",
          state:null,
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        if(this.dateRange[0]){
          obj.startTime = this.dateRange[0]+'T00:00:00'
          obj.endTime = this.dateRange[1]+'T23:59:59'
        }else{
          obj.startTime = "";
          obj.endTime = "";
        }
        authLicenseList(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'tableList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //认证信息下拉列表
      authCustomerDropList(name){
        let obj = {
          customerName:name,
        };
        this.$set(this,'loading',true);
        authCustomerDropList(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dropList',response.data);
        });
      },
      //表格按钮
      tableButton(type,row){
        let self = this;
        if(type == 1){
          authLicenseDetail({id:row.id}).then(response => {
            this.dialogFormReset();
            this.$set(this,'dialogTitle','编辑授权');
            let list = response.data.grantApis.split(',');
            let newList = [];
            for(let i=0;i<list.length;i++){
              newList.push({api:list[i]});
            }
            this.$set(this,'dialogForm',{
              id:response.data.id,
              customerId:response.data.appId,
              grantApis:newList,
              grantType:response.data.grantType,
              expireTime:response.data.expireTime.split('T')[0],
              state:response.data.state,
              remark:response.data.remark,
            });
            this.$set(this,'dialogType',true);
          });
        }else if(type == 2){
          //重置密钥
          this.$confirm('是否确认重置密钥?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            authLicenseSecret({id:row.id}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 3){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            authLicenseDelete({id:row.id}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }
      },
      dialogFormReset(){
        this.$set(this,'dialogForm',{
          customerId:'',
          grantApis:[{api:''}],
          grantType:'',
          birthday:'',
          state:true,
          remark:'',
        });
      },
      addApi(){
        if (!this.dialogForm.grantApis[99]){
          this.dialogForm.grantApis.push({api:''})
        }else{
          this.msgError('最多添加100个接口')
        }
      },
      delApi(index){
        this.dialogForm.grantApis.splice(index,1);
      },
    },
  }
</script>

<style scoped lang="scss">
.certificationAccredit{
  .certificationAccreditPage{

  }
}
</style>
<style lang="scss">
  .certificationAccreditDialog{
    .form-table-box{
      display: flex;
      .form-table-title-box{
        width:88px;
        text-align: right;
        margin-right:12px;
        line-height: 40px;
        span{
          color: #FF6666;
        }
      }
      .for-api-max-box{
        max-height:300px;
        .for-api-box{
          display: flex;
          p{
            width:40px;
            height:40px;
            font-size:18px;
            text-align: center;
            line-height:38px;
            margin-left:10px;
            cursor: pointer;
            font-weight: 700;
          }
          .el-icon-circle-plus-outline{
            color:#00a0e9;
          }
          .el-icon-remove-outline{
            color:#FF6666;
          }
        }
      }
    }
  }
</style>
