<template>
  <el-dialog
    v-loading="loading"
    :title="'新建需求'"
    :center="true"
    align="left"
    top="5vh"
    width="1040px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin-top: -20px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="0px"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item prop="title">
                  <el-input
                    v-model="ruleForm.title"
                    maxlength="250"
                    :size="'medium'"
                    :placeholder="'标题'"
                    :disabled="isDetail"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12" style="padding: 0 0 0 10px">
                <el-form-item prop="model_id">
                  <good-class
                    :value="ruleForm.model_id"
                    :size="'medium'"
                    @handleChange="goods_class_change"
                    :disabled="isDetail"
                    :type="'1'"
                    :placeholder="'功能区'"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24" v-if="!loading">
                <tinymce
                  v-model="ruleForm.remark"
                  :width="'100%'"
                  :height="'300px'"
                />
              </el-col>
            </el-row>
          </el-form>
        </el-col>

        <el-col :span="24" style="text-align: right; padding: 15px 0 0 0">
          <el-button
            v-if="!isDetail"
            :size="'mini'"
            type="primary"
            @click="submitForm"
            >保 存
          </el-button>
          <el-button :size="'mini'" @click="showModelThis = false">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/purchaseAndSale/adjust";
import resToken from "@/mixins/resToken";
import Tinymce from "@/components/Tinymce";
import { platformType } from "./columns";
export default {
  name: "adjust",
  props: ["showModel", "id", "isDetail", "sitem"],
  mixins: [resToken],
  components: {
    Tinymce,
  },
  data() {
    return {
      loading: false,
      organizeList: [],
      showModelThis: this.showModel,
      ruleForm: {
        title: "", // 账号
        company_type: platformType,
        model_id: [], // 功能区
        remark: "",
      },
      rulesThis: this.rules,
      rules: {
        title: [
          {
            required: true,
            message: "请输入标题",
            trigger: "blur",
          },
        ],
        model_id: [
          {
            type: "array",
            required: true,
            message: "请选择功能区",
            trigger: "change",
          },
        ],

        remark: [
          {
            required: true,
            message: "请输入描述",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  mounted() {},
  methods: {
    async initForm() {
      this.loading = true;
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();

          this.ruleForm = {
            title: "我是一个标题", // 账号
            company_type: platformType,
            model_id: [], // 功能区
            remark: `<h4>【故障现象描述：】</h4>
       <p style="font-size:12px">
       必填 Ctrl+V粘贴截图
       </p>
      <h4>【期望结果描述：】</h4>
      <p style="font-size:12px">
        必填 Ctrl+V粘贴截图
      </p>`,
          };
        }
      });
    },
    goods_class_change(e) {
      this.ruleForm.model_id = e;
      this.$refs.ruleForm.validateField("model_id");
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          model.model_id = model.model_id.toString();
          let length = 5000;
          if (model.remark.length > length) {
            this.$message.warning("描述文字及标签总长度不能超过5000！");
            this.loading = false;
            return;
          }
          let res = await asyncRequest.add(model);
          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "添加成功",
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

   <style lang="scss" scoped>
.account {
}
</style>
