<template>
  <div class="org-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" inline>
        <el-form-item label="应用ID">
          <el-input
            v-model="filter.id"
            placeholder="应用ID"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="模糊查询">
          <el-input
            v-model="filter.nameStartWith"
            placeholder="名称前缀"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" icon="ios-search" @click="toPage(1)"
            >查询</el-button
          >
          <el-button
            v-if="checkPrivilege('WXAPP_INSERT')"
            type="success"
            icon="md-add"
            @click="toAdd"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column prop="id" label="ID" width="260"></el-table-column>
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="160">
          <span slot-scope="scope">{{
            scope.row.createTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="160">
          <span slot-scope="scope">{{
            scope.row.updateTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column
          v-if="checkPrivilege('WXAPP_EDIT')"
          class-name="action-column"
          label="操作"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      v-if="checkPrivilege('WXAPP_EDIT') || checkPrivilege('WXAPP_INSERT')"
      :visible.sync="visible"
      top="10vh"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="`${modifyType === 'update' ? '修改' : '新增'}小程序`"
    >
      <el-form ref="form" :model="modalForm" :rules="rules" label-width="100px">
        <el-form-item prop="id" label="应用ID：">
          <el-input
            :disabled="modifyType === 'update'"
            v-model.trim="modalForm.id"
            placeholder="请输入应用ID"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="name" label="名称：">
          <el-input
            v-model.trim="modalForm.name"
            placeholder="请输入应用名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="secret" label="应用密钥：">
          <el-input
            v-model.trim="modalForm.secret"
            placeholder="请输入应用密钥"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="danger" @click="cancel" plain>取消</el-button>
        <el-button type="primary" :disabled="isSubmit" @click="submit"
          >确认</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { getWeChatAppList, insertWeChatApp, updateWeChatApp } from "../api";

export default {
  name: "we-chat-app-manage",
  data() {
    return {
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      filter: {
        id: "",
        nameStartWith: "",
      },
      modalForm: {
        id: "",
        name: "",
        secret: "",
      },
      modifyType: "insert",
      visible: false,
      isSubmit: false,
      rules: {
        id: [{ required: true, message: "请填写小程序AppId" }],
        name: [{ required: true, message: "请填写小程序名称" }],
        secret: [{ required: true, message: "请填写小程序名称" }],
      },
    };
  },
  watch: {
    visible(v) {
      this.$refs.form?.clearValidate();
    },
  },
  created() {
    this.toPage(1);
  },
  methods: {
    async getList() {
      try {
        const data = {
          ...this.filter,
          pageNumber: this.current,
          pageSize: this.size,
        };
        const result = await getWeChatAppList(data);

        this.dataList = result.records;
        this.total = result.total;
      } catch (error) {
        console.error(error);
        error.message && this.$message.error(error.message);
      }
    },
    toAdd() {
      this.modifyType = "insert";
      this.modalForm = this.$options.data.call(this).modalForm;
      this.$nextTick(() => {
        this.visible = true;
      });
    },
    toEdit(row) {
      this.modifyType = "update";
      const { id, name, secret } = row;
      this.modalForm = { id, name, secret };
      this.visible = true;
    },
    cancel() {
      this.visible = false;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async submit() {
      if (this.isSubmit) {
        return;
      }
      this.isSubmit = true;
      const isUpdate = this.modifyType === "update";
      const api = isUpdate ? updateWeChatApp : insertWeChatApp;
      try {
        await this.$refs.form.validate();
        await api(this.modalForm);
        this.getList();
        this.$message.success(`${isUpdate ? "修改" : "新增"}成功`);
        this.visible = false;
      } catch (error) {
        error.message && this.$message.error(error.message);
      }
      this.isSubmit = false;
    },
  },
};
</script>
