<template>
  <div>
    <el-dialog title="标签管理" v-model="dialogVisible">
      <div>
        <el-form ref="formRef" :model="form" label-width="120px">
          <el-form-item
            label="标题："
            prop="name"
            :rules="[
              {
                required: true,
                message: '请输入标签名称',
                trigger: 'blur'
              }
            ]"
          >
            <el-input
              v-model="form.name"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="标签用途：" prop="purpose">
            <el-input
              v-model="form.purpose"
              placeholder="请输入标签用途"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">保 存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { request } from "@/utils";
import { ElMessage } from "element-plus";
import { ref, computed, defineProps, defineEmits, watch, reactive } from "vue";

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => {}
  }
});
const emits = defineEmits(["update:show", "success"]);
const dialogVisible = computed({
  get() {
    return props.show;
  },
  set(n) {
    emits("update:show", n);
  }
});

watch(
  () => dialogVisible.value,
  n => {
    if (n) {
      if (props.rowData?.id) {
        form.name = props.rowData.name;
        form.purpose = props.rowData.purpose;
        form.id = props.rowData.id;
      }
    } else {
      formRef.value.resetFields();
      form.id = undefined;
    }
  }
);
const form = reactive({
  name: "",
  purpose: "",
  id: undefined
});
const formRef = ref();

const onSubmit = () => {
  formRef.value.validate(valid => {
    if (!valid) return;
    let api = form.id
      ? "dataService/schemeManage/tag/update"
      : "dataService/schemeManage/tag";
    request.post(api, form).then(resp => {
      ElMessage.success(resp.message);
      dialogVisible.value = false;
      emits("success");
    });
  });
};
</script>
