<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"
    append-to-body
  >
    <el-card style="margin-top: -20px">
      <el-row>
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            size="mini"
            label-width="90px"
            :rules="rulesThis"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="24">
                <el-form-item prop="title" label="工单标题">
                  <el-input
                    v-model="ruleForm.title"
                    maxlength="250"
                    :size="'mini'"
                    :placeholder="'工单标题'"
                    :disabled="isDetail"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item prop="company_type" label="系统类型">
                  <el-select
                    v-model="ruleForm.company_type"
                    style="width: 100%"
                    :size="'mini'"
                    clearable
                    placeholder="系统类型"
                  >
                    <el-option
                      v-for="item in company_type"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                      clearable
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item prop="model_id" label="功能区">
                  <good-class
                    :value="ruleForm.model_id"
                    :size="'mini'"
                    @handleChange="goods_class_change"
                    :disabled="ruleForm.company_type == ''"
                    :type="ruleForm.company_type"
                    :placeholder="'功能区'"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item prop="noteNo" label="业务编号">
                  <el-input
                    v-model="ruleForm.noteNo"
                    maxlength="250"
                    :size="'medium'"
                    type="textarea"
                    :autosize="{ minRows: 3, maxRows: 3 }"
                    :placeholder="'业务编号'"
                    :disabled="isDetail"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24" v-if="!loading">
                <TinymceEditor
                  ref="tinymceEditor"
                  :tinymceHtml="ruleForm.remark"
                  @input="tinput"
                ></TinymceEditor>
              </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 TinymceEditor from "@/components/TinymceEditor";
import { company_type } from "@/views/pucColumns";

// import Tinymce from "@/components/Tinymce";
export default {
  name: "adjust",
  props: ["showModel", "id", "isDetail", "type"],
  mixins: [resToken],
  components: {
    TinymceEditor,
    // Tinymce,
  },
  data() {
    function isEmojiCharacter(substring) {
      for (var i = 0; i < substring.length; i++) {
        var hs = substring.charCodeAt(i);
        if (0xd800 <= hs && hs <= 0xdbff) {
          if (substring.length > 1) {
            var ls = substring.charCodeAt(i + 1);
            var uc = (hs - 0xd800) * 0x400 + (ls - 0xdc00) + 0x10000;
            if (0x1d000 <= uc && uc <= 0x1f77f) {
              return true;
            }
          }
        } else if (substring.length > 1) {
          var ls = substring.charCodeAt(i + 1);
          if (ls == 0x20e3) {
            return true;
          }
        } else {
          if (0x2100 <= hs && hs <= 0x27ff) {
            return true;
          } else if (0x2b05 <= hs && hs <= 0x2b07) {
            return true;
          } else if (0x2934 <= hs && hs <= 0x2935) {
            return true;
          } else if (0x3297 <= hs && hs <= 0x3299) {
            return true;
          } else if (
            hs == 0xa9 ||
            hs == 0xae ||
            hs == 0x303d ||
            hs == 0x3030 ||
            hs == 0x2b55 ||
            hs == 0x2b1c ||
            hs == 0x2b1b ||
            hs == 0x2b50
          ) {
            return true;
          }
        }
      }
    }
    const validatename = (rule, value, callback) => {
      if (value === "") {
        callback();
      } else {
        let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
        if (reg.test(value)) {
          callback(new Error("业务编号不能输入汉字！"));
        } else if (isEmojiCharacter(value)) {
          callback(new Error("业务编号不能输入表情包！"));
        } else {
          callback();
        }
      }
    };
    return {
      loading: false,
      company_type,
      organizeList: [],
      showModelThis: this.showModel,
      ruleForm: {
        title: "", // 账号
        company_type: "",
        model_id: [], // 功能区
        remark: "",
        noteNo: "",
      },
      rulesThis: this.rules,
      rules: {
        title: [
          {
            required: true,
            message: "请输入标题",
            trigger: "blur",
          },
        ],
        noteNo: [
          {
            required: false,
            validator: validatename,
            trigger: "blur",
          },
        ],
        company_type: [
          {
            required: true,
            message: "请选择系统类型",
            trigger: "change",
          },
        ],
        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: "",
            noteNo: "",
            model_id: [], // 功能区
            remark: `<div style="display: inline-block"><h4>【故障现象描述：】</h4>\n<p>必填 Ctrl+V粘贴截图</p>\n<h4>【期望结果描述：】</h4>\n<p>必填 Ctrl+V粘贴截图</p></div> `,
          };
        }
      });
    },
    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));
          let remark = model.remark;
          remark = remark.replace(/\n/g, "");
          remark = remark.replace(/"/g, "'");
          model.remark = remark;

          let length = 5000;
          if (model.remark.length > length) {
            this.$message.warning("描述文字及标签总长度不能超过5000！");
            this.loading = false;
            return;
          }

          const { code, data, message } = await asyncRequest.add(model);
          this.loading = false;
          if (code === 0) {
            this.$notify.success({
              title: "工单添加成功",
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (code >= 100 && code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getimg(str) {
      console.log(str);
      let arr = str.split("<img");
      console.log(arr);
    },
    tinput(e) {
      this.ruleForm.remark = e || "";
      this.$refs.ruleForm.validateField("remark");
    },
  },
};
</script>

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