<template>
  <el-form
    v-loading="loading"
    ref="ruleForm"
    :model="ruleForm"
    status-icon
    :rules="rulesThis"
    :label-width="labelWidth || '110px'"
    class="demo-ruleForm-goodsOnline"
    :size="'mini'"
  >
    <el-row>
      <el-col :span="12"
        ><el-form-item label="审核状态" prop="state">
          <el-radio-group
            v-model="ruleForm.state"
            placeholder="请选择审核状态"
            style="width: 100%"
            :size="size || 'medium'"
            @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" class="tr">
        <el-form-item label="审核备注" v-if="ruleForm.state === '0'" prop="remark">
          <el-input
            type="textarea"
            placeholder="审核备注"
            v-model="ruleForm.remark"
            :disabled="disabled"
            maxlength="250"
            show-word-limit
            :autosize="{ minRows: 3, maxRows: 3 }"
          />
        </el-form-item>
        <el-form-item class="fr" v-if="ruleForm.state === '1'">
          <el-button :size="'mini'" type="primary" @click="submitForm('6')"
            >保 存
          </el-button>
        </el-form-item>

        <el-form-item class="fr" v-if="ruleForm.state === '0'">
          <el-button :size="'mini'" type="primary" @click="submitForm('')"
            >驳回重新审核
          </el-button>
        </el-form-item>
        <!-- <el-form-item class="fr" v-if="ruleForm.state === '0'" >
          <el-button
            :size="'mini'"
            type="danger"
            @click="submitForm('8')"
            >驳回并下线
          </el-button>
        </el-form-item> -->
      </el-col>
      <el-col :span="24" v-if="ruleForm.state === '1'"
        ><el-form-item label="平台商品编码" prop="plat_code">
          <el-input
            v-model="ruleForm.plat_code"
            placeholder="平台商品编码"
            maxlength="100"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
import costFormAddEdit from "./costFormAddEdit";
import asyncRequest from "@/apis/service/goodStore/goodsOnline";
import { isnumber, isAlphanumeric, validAlphabets } from "@/utils/validate";
export default {
  name: "exam-form",
  props: ["size", "statusList", "disabled", "isMust", "labelWidth", "id", "code"],
  components: {
    costFormAddEdit,
  },
  /**
   * 属性集合
   * @param {String}        size             : 组件大小             非必填
   * @param {Array}         statusList       : 驳回至备选项          必填
   * @param {Boolean}       disabled         : 是否禁用              必填
   * @param {Boolean}       isMust           : 是否需要展示驳回节点   必填
   *
   *
   */
  /**
   * 事件集合
   * @searchChange             : 选中值变化调用   抛出选中数据
   */
  data() {
    const validate_plat_code = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("平台商品编号不能为空！"));
      } else {
        if (isnumber(value)) {
          callback();
        } else if (validAlphabets(value)) {
          callback();
        } else if (isAlphanumeric(value)) {
          callback();
        } else {
          callback("平台商品编号仅支持数字和字母！");
        }
      }
    };
    return {
      stateList: [
        {
          value: "1",
          label: "通过",
        },
        {
          value: "0",
          label: "驳回",
        },
      ],
      loading: false,

      showModelThis: this.showModel,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 60 * 60 * 24 * 1000;
        },
      },

      ruleForm: {
        state: "1", // 通过or驳回
        remark: "",
        plat_code: "",
      },
      rulesThis: this.rules,
      rules: {
        plat_code: [
          {
            required: true,
            validator: validate_plat_code,
            trigger: "blur",
          },
        ],
        state: [
          {
            required: true,
            message: "请选择审核状态",
            trigger: "change",
          },
        ],
        remark: [
          { required: true, message: "请输入审核备注", trigger: "blur" },
          {
            min: 1,
            max: 250,
            message: "长度在 1 到 250 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  newTime: function (val) {
    if (val) {
      this.initForm();
    }
  },
  mounted() {
    this.initForm();
  },
  methods: {
    async initForm() {
      this.loading = true;
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();

          this.ruleForm = {
            plat_code: this.code || "",
            state: "1", // 通过or驳回
            remark: "",
          };
        }
      });
    },
    async submitForm(numStatus) {
      this.ruleForm.numStatus = numStatus;
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          // 刷新
          this.$emit("resSuccess", model);
          // this.routeReGoto("goodsOnline", {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.demo-ruleForm-goodsOnline {
  .shangchuan-ul {
    li {
      position: relative;
      width: 100%;
      &.tupian {
      }
    }
  }
}
</style>
