<template>
  <div>
    <el-dialog
      :title="title"
      :destroy-on-close="true"
      :visible.sync="Visible"
      width="30%"
      @close="handleClose"
      :close-on-click-modal="false"
      top="10vh"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="参数名称" prop="name">
          <el-input
            size="small"
            v-model="form.name"
            placeholder="请输入参数名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="一级组织" prop="firstOrganize">
          <el-input
            size="small"
            v-model="form.firstOrganize"
            placeholder="请输入一级组织"
          ></el-input>
        </el-form-item>
        <el-form-item label="二级组织" prop="secondOrganize">
          <el-input
            size="small"
            v-model="form.secondOrganize"
            placeholder="请输入二级组织"
          ></el-input>
        </el-form-item>
        <el-form-item label="参数键" prop="paramKey">
          <el-input
            size="small"
            v-model="form.paramKey"
            placeholder="请输入参数键"
          ></el-input>
        </el-form-item>
        <el-form-item label="参数值" prop="paramValue">
          <el-input
            size="small"
            v-model="form.paramValue"
            placeholder="请输入参数值"
          ></el-input>
        </el-form-item>
        <el-form-item label="参数修改状态" prop="modStatus">
          <el-select v-model="form.modStatus" placeholder="请选择参数有效状态">
            <el-option
              v-for="(v, key) in dict.modStatus"
              :key="key"
              :label="v"
              :value="Number(key)"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="展现类型" prop="displayType">
          <el-select v-model="form.displayType" placeholder="请选择展现类型">
            <el-option
              v-for="(v, key) in dict.displayType"
              :key="key"
              :label="v"
              :value="Number(key)"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参数有效状态" prop="validStatus">
          <el-radio-group v-model="form.validStatus">
            <el-radio
              v-for="(v, key) in dict.validStatus"
              :key="key"
              :label="Number(key)"
            >
              {{ v }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            resize="none"
            :autosize="{ minRows: 3, maxRows: 3 }"
            type="textarea"
            placeholder="请输入备注"
            v-model="form.remark"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleRest">重 置</el-button>
        <el-button size="small" type="primary" @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { saveParam } from "@/api/system";
export default {
  props: {
    title: {
      type: String,
      required: true,
      default: "",
    },
    addVisible: {
      type: Boolean,
      required: true,
      default: false,
    },
    dict: {
      type: Object,
      required: true,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {
        name: "", // 参数名称
        firstOrganize: "", // 一级组织
        secondOrganize: "", // 二级组织
        paramKey: "", // 参数键
        paramValue: "", // 参数值
        modStatus: "", // 参数修改状态
        displayType: "", // 展现类型
        validStatus: 1, // 参数有效状态
        remark: "", // 备注
      },
      rules: {
        name: [{ required: true, message: "请输入参数名称", trigger: "blur" }],
        paramKey: [
          { required: true, message: "请输入参数键", trigger: "blur" },
        ],
        paramValue: [
          { required: true, message: "请输入参数值", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    Visible: {
      get() {
        return this.addVisible;
      },
      set(val) {
        this.$emit("update:addVisible", val);
      },
    },
  },
  methods: {
    // 确定
    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let res = await saveParam(this.form);
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.$emit("addSuccess");
            this.handleClose();
          }
        }
      });
    },
    // 新增
    onAdd() {
      Object.assign(this.form, this.$options.data().form);
      this.form.id && this.$delete(this.form, "id");
    },
    // 编辑
    onEdit(row) {
      setTimeout(() => {
        this.form = { ...row };
      }, 10);
    },
    // 重置
    handleRest() {
      this.$resetForm("form");
    },
    // 关闭
    handleClose() {
      this.$resetForm("form");
      this.Visible = false;
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-select {
  width: 100%;
}
</style>
