<template>
  <div>
    <el-dialog title="渠道设置" v-model="dialogVisible">
      <div>
        <el-form ref="formRef" :model="form" label-width="80px">
          <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="remark">
            <el-input
              v-model="form.remark"
              placeholder="请输入备注"
              type="textarea"
              clearable
              :rows="8"
            ></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.remark = props.rowData.remark;
        form.id = props.rowData.id;
      }
    } else {
      formRef.value.resetFields();
      form.id = undefined;
    }
  }
);
const form = reactive({
  name: "",
  remark: "",
  id: undefined
});
const formRef = ref();

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