<template>
  <el-dialog
    width="1260px"
    title="同步"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="handlereset"
    style="height: auto;"
  >
    <div style="margin-bottom: 60px;">
      <el-form
        style="height: auto;"
        class="addFormBox"
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        label-width="80px"
      >
        <el-form-item label="token" prop="token">
          <el-input
            v-model="dataForm.token"
            type="textarea"
            :rows="4"
            placeholder="token"
          ></el-input>
        </el-form-item>
        <el-form-item label="cookie" prop="cookie">
          <el-input
            v-model="dataForm.cookie"
            type="textarea"
            :rows="4"
            placeholder="cookie"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="handlereset">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import global from "../../common/common.vue";
export default {
  data() {
    return {
      baseUrl: global.baseUrl,
      visible: false,
      dataForm: {
        token: "",
        cookie: ""
      },
      dataRule: {
        token: [
          { required: true, message: "token不能为空", trigger: "blur" }
        ],
        cookie: [
          { required: true, message: "cookie不能为空", trigger: "blur" }
        ]
      },
      urlList: {
        add: "/common/wxPublicAccount/addWxPublicAccountArticle"
      }
    };
  },
  methods: {
    init() {
      this.dataForm = {
        token: "",
        cookie: ""
      };
      this.visible = true;
      this.$nextTick(() => {
        if (this.$refs["dataForm"]) {
          this.$refs["dataForm"].clearValidate();
        }
      });
    },
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            fullscreen: true
          });
          this.$http
            .request({
              url: this.$http.adornUrl(this.urlList.add),
              method: "POST",
              data: this.dataForm,
              header: {
                "Content-Type": "application/json"
              }
            })
            .then(({ data }) => {
              loading.close();
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            })
            .catch(() => {
              loading.close();
            });
        }
      });
    },
    handlereset() {
      if (this.$refs["dataForm"]) {
        this.$refs["dataForm"].clearValidate();
      }
      this.visible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/ .addFormBox.el-form-item {
  margin-bottom: 10px !important;
}
</style>
