<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="18vh"
    width="900px"
    @close="closeModel"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-card>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            v-if="isApproval"
            :model="cexForm"
            status-icon
            :rules="cexRules"
            ref="cexForm"
            label-width="80px"
            class="cexForm-ruleForm"
            ><el-row>
              <el-col :span="12">
                <el-row class="cexForm-div">
                  <el-col :span="8" class="tr cexForm-label">LOGO：</el-col>
                  <el-col :span="16" class="cexForm-main">{{
                    ruleForm.logo
                  }}</el-col>
                </el-row>
                <el-row class="cexForm-div">
                  <el-col :span="8" class="tr cexForm-label">品牌名称：</el-col>
                  <el-col :span="16" class="cexForm-main">{{
                    ruleForm.name
                  }}</el-col>
                </el-row>
                <el-row class="cexForm-div">
                  <el-col :span="8" class="tr cexForm-label">企业名称：</el-col>
                  <el-col :span="16" class="cexForm-main">{{
                    ruleForm.company
                  }}</el-col>
                </el-row>
                <el-row class="cexForm-div">
                  <el-col :span="8" class="tr cexForm-label">品牌排序：</el-col>
                  <el-col :span="16" class="cexForm-main">{{
                    ruleForm.order
                  }}</el-col>
                </el-row>
                <el-row class="cexForm-div">
                  <el-col :span="8" class="tr cexForm-label">品牌备注：</el-col>
                  <el-col :span="16" class="cexForm-main">{{
                    ruleForm.fremark
                  }}</el-col>
                </el-row>
              </el-col>
              <el-col :span="12">
                <el-form-item label="审核状态" prop="status">
                  <el-select
                    v-model="cexForm.status"
                    placeholder="请选择审核状态"
                    @change="selectChange"
                  >
                    <el-option
                      v-for="item in statusList"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="审核说明" prop="remark">
                  <el-input
                    v-model="cexForm.remark"
                    type="textarea"
                    placeholder="请输入审核说明"
                    :rows="5"
                    maxlength="300"
                    show-word-limit
                  ></el-input>
                </el-form-item>
                <el-col :span="24" style="text-align: right">
                  <el-button type="primary" @click="submitCexForm"
                    >保 存
                  </el-button>
                  <el-button @click="showModelThis = false">取 消</el-button>
                </el-col>
              </el-col>
            </el-row></el-form
          >
          <el-form
            v-else
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            ref="ruleForm"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="品牌名称" prop="name">
                  <el-input
                    v-model="ruleForm.name"
                    :disabled="isDetail"
                  ></el-input>
                </el-form-item>
                <el-form-item label="企业名称" prop="company">
                  <el-input
                    v-model="ruleForm.company"
                    :disabled="isDetail"
                  ></el-input>
                </el-form-item>
                <el-form-item label="品牌排序" prop="order">
                  <el-input-number
                    :disabled="isDetail"
                    id="tl-number"
                    style="width: 100%; text-align: left"
                    v-model="ruleForm.order"
                    :step="1"
                    :min="1"
                    :max="1000"
                    :precision="0"
                    step-strictly
                    :controls="false"
                  ></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="LOGO"
                  prop="image"
                  :disabled="isDetail"
                  class="activity-upload"
                >
                  <div class="btnupload" style="position: relative">
                    <img v-if="img" :src="img" class="avatar" />
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    <Upload
                      class="Upload"
                      :accept="'.jpg,.png,.bmp,.jpeg,.gif'"
                      :multiple="true"
                      :uploadcondition="beforeAvatarUpload"
                      @UploadErrorEvent="UploadErrorEvent"
                      @UploadSuccessEvent="UploadSuccessEvent"
                    ></Upload>
                  </div>
                  <div class="txt-tips fl">
                    <p>大小：小于1M</p>
                    <p>尺寸：100*100</p>
                    <p>类型：jpg.png.bmp.jpeg.gif</p>
                    <p></p>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="规格备注" prop="remark">
              <el-input
                v-model="ruleForm.remark"
                type="textarea"
                placeholder="请输入备注"
                :rows="5"
                maxlength="300"
                show-word-limit
                :disabled="isDetail"
              ></el-input>
            </el-form-item>
            <el-col :span="24" style="text-align: right">
              <el-tag
                type="warning"
                class="fl"
                style="max-width: 500px"
                v-if="dataInfo.ex_remark && dataInfo.ex_status === 2"
                >审核备注：{{ dataInfo.ex_remark }}</el-tag
              >
              <el-button type="primary" @click="submitForm" v-if="!isDetail"
                >保 存
              </el-button>
              <el-button @click="showModelThis = false">{{
                isDetail ? "关 闭" : "取 消"
              }}</el-button>
            </el-col>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
   <script>
import asyncRequest from "@/apis/service/goodStore/brand";
import Upload from "@/components/Upload";
import {
  isnumber,
  isMobile,
  validEmail,
  isAlphanumeric,
  isChinese,
  isEmoticon,
  validAlphabets,
} from "@/utils/validate";

import statusList from "@/assets/js/cexStatusList";
export default {
  name: "brand",
  props: ["showModel", "id", "isDetail", "isApproval"],
  components: { Upload },
  data() {
    return {
      img:"",
      statusList: statusList,
      loading: false,
      title: "添加商品规格",
      showModelThis: this.showModel,
      dataInfo: {},
      cexForm: {
        id: this.id,
        remark: "",
        status: "1",
      },
      ruleForm: {
        img: "",
        id: this.id,
        company: "",
        name: "",
        order: "1",
        remark: "",
      },
      cexRules: {
        status: {
          required: true,
          message: "请选择审核状态",
          trigger: "change",
        },
        remark: [
          {
            required: false,
            message: "请输入审核说明！",
            trigger: "blur",
          },
        ],
      },
      rulesThis: this.rules,
      rules: {
        order: [
          {
            required: true,
            message: "请输入排序",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入品牌名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 30,
            message: "长度在 2 到 30 个字符",
            trigger: "blur",
          },
        ],
        company: [
          {
            required: true,
            message: "请输入品牌企业",
            trigger: "blur",
          },
          {
            min: 2,
            max: 30,
            message: "长度在 2 到 30 个字符",
            trigger: "blur",
          },
        ],
        image: [{ required: false, message: "请上传图片", trigger: "blur" }],
        remark: [
          {
            required: false,
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    selectChange() {
      if (this.cexForm.status === "1") {
        this.cexRules.remark[0].required = false;
      } else {
        this.cexRules.remark[0].required = true;
      }
    },
    async initForm() {
      if (this.isApproval) {
        this.loading = true;
        this.title = "商品品牌审核";
        await this.initData();
        await this.resetcexForm();
        this.selectChange()
        this.loading = false;
      } else {
        if (this.id === "add") {
          this.title = "添加商品品牌";
          // this.ruleForm.isAdmin = 0;
          this.loading = false;
          this.rulesThis = this.rules;
          await this.resetForm();
        } else {
          if (this.isDetail) {
            this.title = "商品品牌详情";
            this.rulesThis = {};
          } else {
            this.title = "修改商品品牌";
            this.rulesThis = this.rules;
          }
          await this.resetForm();
          await this.initData();
        }
      }
    },
    async initData() {
      this.loading = true;
      let res = await asyncRequest.detail({ id: this.id });
      this.loading = false;
      if (res.code === 0) {
        let data = res.data;
        this.dataInfo = data;
        console.log(this.dataInfo);
        this.ruleForm = {
          img: data.brand_logo,
          id: this.id,
          company: data.brand_company,
          name: data.brand_name,
          order: data.order,
          remark: data.brand_desc,
        };
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            img: "",
            id: this.id,
            company: "",
            name: "",
            order: "1",
            remark: "",
          };
        }
      });
    },
    async resetcexForm() {
      if (this.$refs.cexForm) {
        this.$refs.cexForm.resetFields();
        this.$refs.cexForm.clearValidate();
        this.cexForm = {
          id: this.id,
          remark: "",
          status: "1",
        };
      }
    },
    async submitCexForm() {
      await this.$refs.cexForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let obj = JSON.parse(JSON.stringify(this.cexForm));
          obj.id = this.id;
          let res = await asyncRequest.exam(obj);
          this.loading = false;
          if (res.code === 0) {
            let title = "审核审批成功！";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let obj = JSON.parse(JSON.stringify(this.ruleForm));
          let res = {};
          if (this.id === "add") {
            delete obj["id"];
            res = await asyncRequest.add(obj);
          } else {
            res = await asyncRequest.update(obj);
          }
          this.loading = false;
          if (res.code === 0) {
            let title = this.id === "add" ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //图片上传失败
    UploadErrorEvent() {
      this.$message.error("图片上传失败!");
      this.ruleForm.image = "";
      this.img = "";
      this.$refs.ruleForm.validateField("image");
    },

    //图片上传成功
    UploadSuccessEvent(data) {
      this.img = data.url;
      this.ruleForm.image = data.url;
      this.$message.success("图片上传成功!");
      this.$refs.ruleForm.validateField("image");
    },

    //判断图片规格
    beforeAvatarUpload(file) {
      let isJPG = false;
      if (
        file.type === "image/jpg" ||
        file.type === "image/png" ||
        file.type === "image/bmp" ||
        file.type === "image/jpeg" ||
        file.type === "image/gif"
      ) {
        isJPG = true;
      }
      const isLt2M = file.size / 1024 / 1024 < 1;
      if (!isJPG) {
        this.$message.error("图片格式不正确！");
      }
      if (!isLt2M) {
        this.$message.error("图片大小不能超过 1MB!");
      }
      return isJPG && isLt2M;
    },
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
};
</script>

   <style lang="scss" scoped>
.brand {
  .cexForm-ruleForm {
    .cexForm-div {
      margin-bottom: 0px;
      .cexForm-label {
        padding-right: 12px;
        color: #606266;
        line-height: 40px;
        font-size: 14px;
      }
      .cexForm-main {
        line-height: 40px;
        color: rgb(151, 168, 190);
        line-height: 40px;
        font-size: 14px;
      }
    }
  }
  .activity-upload {
    .btnupload {
      float: left;
      border: 1px solid #cccccc;
      box-sizing: border-box;
      width: 135px;
      height: 135px;
      line-height: 135px;
      text-align: center;
    }
    .Upload {
      width: 135px;
      height: 135px;
      line-height: 135px;
      text-align: center;
      position: absolute;
      line-height: 0px;
      top: 0;
      left: 0;
      line-height: 135px;
    }
    .fileUp {
      vertical-align: top;
    }
    .avatar {
      width: 135px;
      height: 135px;
      line-height: 135px;
      text-align: center;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 50px;
      height: 50px;
      line-height: 50px;
      text-align: center;
    }
    .avatar {
      width: 100%;
      height: 100%;
      display: block;
    }
    .txt-tips {
      display: inline-block;
      font-size: 13px;
      color: #606266;
      padding: 18px 0 0 18px;
      p {
        margin: 0;
        line-height: 30px;
      }
    }
    .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
  }
}
</style>
   
   