<template>
  <el-dialog
    class="modify-model"
    :visible.sync="modalIsShow"
    :title="title"
    top="3vh"
    width="1000px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="180px"
    >
      <el-form-item prop="appType" label="应用类型：">
        <NormalSelect
          v-model="modalForm.appType"
          :func="appTypeList"
          valueStr="code"
          :disabled="true"
        ></NormalSelect>
      </el-form-item>
      <el-form-item prop="remark" label="备注：">
        <el-input
          v-model.trim="modalForm.remark"
          placeholder="请输入备注"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="" style="margin-bottom: 5px">
        <el-radio-group v-model="radioType" size="mini">
          <el-radio-button label="user">user角色</el-radio-button>
          <el-radio-button label="system">system角色</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <el-form-item prop="user" v-if="radioType == 'user'" label="模板内容：">
        <el-input
          type="textarea"
          v-model.trim="modalForm.user"
          placeholder="请输入user角色提示词模版"
          clearable
          :rows="25"
        ></el-input>
      </el-form-item>

      <el-form-item
        prop="system"
        v-if="radioType == 'system'"
        label="模板内容："
      >
        <el-input
          type="textarea"
          v-model.trim="modalForm.system"
          placeholder="请输入system角色提示词模版"
          clearable
          :rows="25"
        ></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>
</template>

<script>
import { cueWrdInsertOrUpdate, appTypeList } from "../../../api";
import NormalSelect from "@/components/NormalSelect.vue";

const initModalForm = {
  id: "",
  supplierId: "",
  appType: "",
  system: "",
  user: "",
  remark: "",
};

export default {
  name: "modifyCueWordTpl",
  components: { NormalSelect },
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
    appType: {
      type: String,
      default: "",
    },
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return "模型提示词" + (this.isEdit ? "编辑" : "新增");
    },
  },
  data() {
    return {
      appTypeList,
      radioType: "user",
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      rules: {
        appType: [
          {
            required: true,
            message: "请选择应用类型",
          },
        ],
        // system: [
        //   {
        //     required: true,
        //     message: "请输入system角色提示词模版",
        //   },
        // ],
        // user: [
        //   {
        //     required: true,
        //     message: "请输入user角色提示词模版",
        //   },
        // ],
        remark: [
          {
            required: true,
            message: "请输入备注",
          },
        ],
      },
    };
  },
  methods: {
    initData(val) {
      this.modalForm = this.$objAssign(initModalForm, val);

      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    visibleChange() {
      this.initData(this.instance);
      if (!this.isEdit) {
        this.modalForm.appType = this.appType;
      }
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;

      const datas = { ...this.modalForm };
      datas.system = datas.system || " ";
      datas.user = datas.user || " ";
      if (this.isEdit) {
        delete datas.appType;
      }
      const data = await cueWrdInsertOrUpdate(datas).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

      this.$message.success((this.isEdit ? "修改" : "新增") + "成功！");
      this.cancel();
      this.$emit("modified");
    },
  },
};
</script>
