<!--
 * @Author: your name
 * @Date: 2021-11-29 09:18:04
 * @LastEditTime: 2022-02-28 10:46:33
 * @LastEditors: Please set LastEditors
 * @Description: 新增/编辑应用
 * @FilePath: \Foshan4A2.0\src\views\authorityManagement\components\addApp.vue
-->
<template>
  <div class="addApp flex-wrap">
    <!--新增应用-->
    <div class="addApp-form r24">
      <div class="addApp-form-title flex">
        <div class="manageTitle">
          编辑应用
          <!-- <span class="isqy">
            <span>是否启用</span>
            <el-radio-group v-model="radio">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </span> -->
        </div>
        <div class="btn">
          <!-- <el-button @click="deleteApp" class="r24" type="danger">删除</el-button> -->
          <el-button size="small" @click="editApp" type="primary">保存</el-button>
        </div>
      </div>
      <div class="addApp-form-content dialog-public-background">
        <el-form ref="form" class="form" :rules="rules" :model="form">
          <div class="flex">
            <el-form-item prop="name" label="应用名称">
              <el-input placeholder="请输入应用名称" maxlength="32" v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="APPID">
              <el-input placeholder="请输入APPID" disabled v-model="form.id"></el-input>
            </el-form-item>
            <el-form-item label="APPSECRET">
              <el-input placeholder="请输入APPSECRET" disabled v-model="form.app"></el-input>
            </el-form-item>
          </div>
          <el-form-item class="url" prop="url" label="应用地址">
            <el-input placeholder="请输入应用地址" maxlength="200" v-model="form.url"></el-input>
          </el-form-item>
          <el-form-item prop="content" class="content" label="描述">
            <el-input type="textarea" maxlength="200" rows="3" placeholder="请输入描述" v-model="form.content"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--上传logo-->
    <div class="addApp-upload">
      <div class="flex">
        <div class="upload">
          <div class="title">上传Logo</div>
          <el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
        </div>
        <div class="preview">
          <div class="title">预览</div>
          <div class="box"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  GetAppDetails,
  EditApp,
  UpdateAppStatus,
  DeleteApp,
} from "@/api/apiAuthority";
export default {
  name: "Addapp",
  data () {
    return {
      form: {
        //应用表单
        name: "",
        id: "",
        app: "",
        url: "",
        content: "",
      },
      rules: {
        //表单验证
        name: [{ required: true, message: "请输入应用名称", trigger: "blur" }],
        content: [{ required: true, message: "请输入描述", trigger: "blur" }],
      },
      radio: 1,
      AppId: null,
      type: null, //参数类型
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  created () {
    const { AuthId, Status } = this.$route.query;
    this.radio = Status;
    this.AppId = AuthId;
    this.getAppDetails(AuthId);
  },
  methods: {
    handleRemove (file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //获取应用详情
    async getAppDetails (id) {
      try {
        const res = await GetAppDetails({
          AppId: id,
        });
        if (res.code === 0) {
          const { AppName, AppShowId, AppShowSecret, AppDesc, AppUrl } =
            res.returnData;
          this.form.name = AppName;
          this.form.id = AppShowId;
          this.form.app = AppShowSecret;
          this.form.content = AppDesc;
          this.form.url = AppUrl;
        } else {
          this.$message.error(res.message);
        }
      } catch (error) {
        console.log("出错了", error.message || error);
      }
    },
    //应用状态变更
    async handleChange (val) {
      try {
        const res = await UpdateAppStatus({
          AppId: this.AppId,
          Status: val,
        });
        if (res.code === 0) {
          this.$message.success(res.message);
        } else {
          this.$message.error(res.message);
        }
      } catch (error) {
        console.log("出错了", error.message || error);
      }
    },
    //应用状态变更
    async editApp () {
      try {
        const res = await EditApp({
          AppId: this.AppId,
          AppDesc: this.form.content,
          AppName: this.form.name,
          AppUrl: this.form.url,
        });
        if (res.code === 0) {
          this.$message.success(res.message);
          this.$store.dispatch("tagsView/delView", this.$route);
          this.$router.push("/authority");
        } else {
          this.$message.error(res.message);
        }
      } catch (error) {
        console.log("出错了", error.message || error);
      }
    },
    //应用删除
    async deleteApp () {
      try {
        const res = await DeleteApp({
          AppId: this.AppId,
        });
        if (res.code === 0) {
          this.$message.success(res.message);
          setTimeout(() => {
            this.$router.push("/authority");
          }, 2000);
        } else {
          this.$message.error(res.message);
        }
      } catch (error) {
        console.log("出错了", error.message || error);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.addApp {
  padding: 24px;
  .addApp-form {
    flex: 1;
    background: #ffffff;
    border-radius: 4px;
    padding: 30px 68px 32px 24px;
    .title {
      font-size: 24px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #303133;
      .isqy {
        span {
          font-weight: 400;
          font-size: 14px;
          margin: 0 28px 0 42px;
        }
      }
    }
    .addApp-form-title {
      line-height: 32px;
    }
    .addApp-form-content {
      margin-top: 40px;
      ::v-deep .form {
        .el-form-item {
          display: flex;
        }
        .el-input__inner {
          height: 32px;
          line-height: 32px;
        }
        .url {
          margin-left: 10px;
          .el-form-item__content {
            width: calc(100% - 70px);
          }
        }
        .content {
          .el-form-item__content {
            flex: 1;
          }
          .el-form-item__label {
            margin-left: 10px;
          }
        }
        textarea {
          resize: none;
        }
      }
    }
  }
  .addApp-upload {
    width: 425px;
    background: #ffffff;
    border-radius: 4px;
    padding: 32px 32px 40px 32px;
    .title {
      font-size: 24px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #303133;
      margin-bottom: 22px;
    }
    ::v-deep .el-upload--picture-card {
      height: 160px;
      line-height: 160px;
      width: 160px;
      background: #f5f7fa;
      border: 1px dashed #2d67e3;
      border-radius: 4px;
      .el-icon-plus {
        font-size: 51px;
        color: #2d67e3;
      }
    }
    .preview {
      .box {
        width: 160px;
        height: 160px;
        background: #3b3c3d;
        border-radius: 4px;
      }
    }
  }
}
</style>
