<template>
  <div>
    <el-form :model="dialogForm" ref="addForm" :rules="rules_check" label-width="120px" label-position="left">
      <el-form-item label="项目阶段" prop="lc_jd">
        <el-select v-model="dialogForm.lc_jd" placeholder="请选择" style="width:100%">
          <el-option v-for="item in selectObj.lc_jd.option" :key="item.v" :label="item.n" :value="item.v"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目事项" prop="lc_sx">
        <el-input v-model="dialogForm.lc_sx"></el-input>
      </el-form-item>
      <el-form-item label="事项权值" prop="lc_sxqz">
        <el-input v-model="dialogForm.lc_sxqz" type="number"></el-input>
      </el-form-item>
      <el-form-item label="上传附件标注" prop="lc_fj_bz">
        <el-input v-model="dialogForm.lc_fj_bz"></el-input>
      </el-form-item>
      <el-form-item label="必填项" prop="lc_bt">
        <el-radio-group v-model="dialogForm.lc_bt">
          <el-radio v-for="item in selectObj.lc_bt.option" :key="item.v" :label="item.v">{{item.n}}</el-radio>
        </el-radio-group>
        <el-input v-model="dialogForm.lc_bt_name" placeholder="请填写必填项名称"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { xmgl_xmlcAdd, xmgl_xmlcEdit, xmgl_xmlcDetail } from "../api";
export default {
  data() {
    return {
      dialogForm: {
        lc_jd: "",
        lc_sx: "",
        lc_sxqz: "",
        lc_fj_bz: "",
        lc_bt: "2",
        lc_bt_name: "",
      },
      rules_check: {
        lc_jd: [{ required: true, message: '请选择项目阶段', trigger: 'change' }],
        lc_sx: [{ required: true, message: '请输入项目事项', trigger: 'blur' }],
        lc_sxqz: [{ required: true, message: '请输入事项权值', trigger: 'blur' }],
        lc_fj_bz: [{ required: true, message: '请输入上传附件标注', trigger: 'blur' }],
        lc_bt: [{ required: true, message: '请选择必填项', trigger: 'change' }],
      },
    }
  },
  props: ["currentId", "selectObj"],
  watch: {
    currentId: {
      immediate: true,
      handler(val) {
        if (val == "") {
          this.resetForm();
        } else {
          this.initDetailInfo(val);
        }
      }
    }
  },
  methods: {
    resetForm() {
      this.$refs.addForm && this.$refs.addForm.resetFields();

      this.dialogForm.lc_bt_name = "";
    },
    async initDetailInfo(id) {
      let res = await xmgl_xmlcDetail({ lc_id: id });
      for (let key in this.dialogForm) {
        this.$set(this.dialogForm, key, res.one_info[key]);
      }
    },
    commitData() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.dialogForm.lc_bt == 1 && this.dialogForm.lc_bt_name == "") {
            this.$message.error("请填写必填项名称");
            return
          }
          let data = {
            issubmit: '1',
            jdbg_xmgl_xmlc: this.dialogForm,
          };
          let obj = {};
          if (this.currentId == "") {
            obj = await xmgl_xmlcAdd(data);
          } else {
            data = Object.assign({ lc_id: this.currentId }, data);
            obj = await xmgl_xmlcEdit(data);
          }
          if (obj) {
            this.$message.success("保存成功！");
            this.$emit("close", 1);
          }
        }
      })
    },
  }
}
</script>

<style>
</style>