<template>
  <el-form
    v-loading="loading"
    ref="ruleForm"
    :model="ruleForm"
    status-icon
    :rules="rulesThis"
    :label-width="labelWidth || '100px'"
    class="demo-ruleForm"
    :size="size || 'medium'"
  >
    <el-row>
      <el-col :span="12"
        ><el-form-item label="审核状态" prop="state">
          <el-radio-group
            v-model="ruleForm.state"
            placeholder="审核状态"
            style="width: 100%"
            :disabled="disabled"
            @change="stateChange"
          >
            <el-radio
              v-for="item in stateList"
              :key="item.value"
              :label="item.value"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-button
          v-if="!disabled"
          :size="'mini'"
          type="primary"
          class="fr"
          @click="submitForm"
          >保 存
        </el-button>
      </el-col>
      <el-col :span="24">
        <el-form-item label="审核备注" prop="remark">
          <el-input
            type="textarea"
            placeholder="审核备注"
            v-model="ruleForm.remark"
            :disabled="disabled"
            maxlength="250"
            :autosize="{ minRows: 2, maxRows: 2 }"
            show-word-limit
          />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
export default {
  name: "exam-form",
  props: ["size", "disabled", "labelWidth", "sitem"],
  /**
   * 属性集合
   * @param {String}        size             : 组件大小             非必填
   * @param {Array}         statusList       : 驳回至备选项          必填
   * @param {Boolean}       disabled         : 是否禁用              必填
   * @param {Boolean}       isMust           : 是否需要展示驳回节点   必填
   *
   *
   */
  /**
   * 事件集合
   * @searchChange             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      loading: false,
      stateList: [
        {
          value: "1",
          label: "通过",
        },
        {
          value: "0",
          label: "驳回",
        },
      ],
      showModelThis: this.showModel,
      ruleForm: {
        state: "1", // 通过or驳回
        rebut: "", //驳回至
        remark: "",
      },
      rulesThis: this.rules,
      rules: {
        state: [
          {
            required: true,
            message: "请选择审核状态",
            trigger: "change",
          },
        ],

        remark: [
          { required: true, message: "请输入审核备注", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    newTime: function () {
      this.initForm();
    },
  },
  mounted() {
    this.initForm();
  },
  methods: {
    async initForm() {
      this.loading = true;
      this.rulesThis = this.rules;
      await this.resetForm();
      this.stateChange();
      this.loading = false;
    },
    stateChange() {
      this.rulesThis.remark[0].required = this.ruleForm.state !== "1";
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          console.log(this.sitem);
          const { state, remark } = this.sitem;
          this.ruleForm = {
            state: state || "1", // 通过or驳回
            remark: remark || "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.$emit("searchChange", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.exa {
  margin: 20px 0 0 30px;
  font-size: 14px;
  font-weight: bold;
}
</style>