<template>
  <div>
    <BaseDialog
      width="580px"
      :isShow.sync="isShow"
      :title="storeId ? '编辑店铺' : '添加店铺'"
      @submit="submitForm"
      @close="close"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formData"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="合作商户：" prop="merId">
          <el-select
            v-model="formData.merId"
            placeholder="请选择合作商户"
            clearable
            filterable
          >
            <el-option
              v-for="(items, indexs) in merList"
              :key="indexs"
              :label="items.merName"
              :value="items.merId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺简称：" prop="storeName">
          <el-input
            clearable
            v-model="formData.storeName"
            placeholder="请输入店铺简称"
          ></el-input>
        </el-form-item>
        <el-form-item label="店铺地址：" prop="address">
          <el-input
            clearable
            v-model="formData.address"
            placeholder="请输入店铺地址"
          ></el-input> </el-form-item
        ><el-form-item label="状态：" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="1">启用</el-radio
            ><el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </BaseDialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    storeId: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      formData: {
        merId: "",
        storeName: "",
        address: "",
        status: 1,
      },
      rules: {
        merId: [
          { required: true, message: "请选择合作商户", trigger: "change" },
        ],
        storeName: [
          { required: true, message: "请输入店铺简称", trigger: "blur" },
        ],
        address: [
          { required: false, message: "请输入店铺地址", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
    };
  },

  methods: {
    init() {
      if (this.storeId) {
        this.$api.mallstoredata(this.storeId).then((res) => {
          this.formData = res.data;
        });
      } else {
        this.formData = {
          merId: "",
          storeName: "",
          address: "",
          status: 1,
        };
      }
    },
    close() {
      this.$refs["formData"].resetFields();
    },
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.$api[this.storeId ? "editmallstore" : "addmallstore"](
            this.formData
          ).then((res) => {
            this.$message.success("成功");
            this.$store.commit("STORELIST");
            this.isShow = false;
            this.$emit("search");
          });
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    ...mapGetters(["merList"]),
    isShow: {
      get() {
        if (this.dialogVisible) {
          this.init();
        }
        return this.dialogVisible;
      },
      set(val) {
        this.$emit("update:dialogVisible", false);
      },
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 220px;
  margin-right: 10px;
}
.el-select {
  width: 220px;
  margin-right: 10px;
}
</style>
