<template>
  <el-form
    ref="ruleForm"
    v-loading="loading"
    :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="status">
          <el-radio-group
            v-model="ruleForm.status"
            placeholder="请选择审核状态"
            style="width: 100%"
            :size="size || 'medium'"
            @change="statusChange"
          >
            <el-radio v-for="item in statusList" :key="item.value" :label="item.value">{{
              item.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item
          v-if="ruleForm.status + '' === '0'"
          label="费用类型"
          prop="fee_type"
        >
          <el-radio-group v-model="ruleForm.fee_type">
            <el-radio
              :label="item.value"
              v-for="item in cost_type"
              :key="item.value"
              :value="item.value"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="审核备注" prop="remark">
          <el-input
            v-model="ruleForm.remark"
            type="textarea"
            placeholder="请输入审核备注"
            :disabled="disabled"
            maxlength="250"
            :autosize="{ minRows: 3, maxRows: 3 }"
            show-word-limit
          />
        </el-form-item>
        <el-form-item class="fr">
          <el-button v-if="!isDetail" :size="'mini'" type="primary" @click="submitForm"
            >保 存
          </el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
import { replaceTextWrapAndSpace } from "@/utils";
import { cost_type } from "../columns";
export default {
  name: "ExamForm",
  props: ["size", "disabled", "isMust", "labelWidth"],
  /**
   * 属性集合
   * @param {String}        size             : 组件大小             非必填
   * @param {Array}         statusList       : 驳回至备选项          必填
   * @param {Boolean}       disabled         : 是否禁用              必填
   * @param {Boolean}       isMust           : 是否需要展示费用类型   必填
   *
   *
   */
  /**
   * 事件集合
   * @searchChange             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      cost_type,
      loading: false,
      statusList: [
        {
          value: "0",
          label: "通过",
        },
        {
          value: "3",
          label: "驳回",
        },
      ],
      showModelThis: this.showModel,
      ruleForm: {
        status: "1", // 通过or驳回
        fee_type: "", // 驳回至
        remark: "",
      },
      rulesThis: this.rules,
      rules: {
        status: [
          {
            required: true,
            message: "请选择审核状态",
            trigger: "change",
          },
        ],
        fee_type: [
          {
            required: true,
            message: "请选择费用类型",
            trigger: "change",
          },
        ],
        remark: [
          { required: true, message: "请输入审核备注", trigger: "blur" },
          {
            min: 1,
            max: 250,
            message: "长度在 1 到 250 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    isDetail: function (val, old) {
      if (this.isDetail !== val) {
        this.options = [];
        this.selectLoading = false;
      }
    },
  },
  mounted() {
    this.initForm();
  },
  methods: {
    async initForm() {
      this.loading = true;
      this.rulesThis = this.rules;
      await this.resetForm();
      this.statusChange();
      this.loading = false;
    },
    statusChange() {
      if (this.ruleForm.status + "" === "0") {
        this.rulesThis.remark[0].required = false;
      } else {
        this.rulesThis.remark[0].required = true;
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            status: "0", // 通过or驳回
            fee_type: "", // 驳回至
            remark: "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.ruleForm.remark = replaceTextWrapAndSpace(this.ruleForm.remark);
          this.$emit("searchChange", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style></style>
