<template>
  <div>
    <BaseDialog
      width="580px"
      :isShow.sync="isShow"
      :title="formData.categoryId ? '编辑分类' : '添加分类'"
      @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
            :disabled="doneEdit"
            @change="changeMerId"
            clearable
            v-model="formData.merId"
            placeholder="请选择合作商户"
          >
            <el-option
              v-for="(item, index) in merList"
              :key="index"
              :label="item.merName"
              :value="item.merId"
            >
            </el-option></el-select></el-form-item
        ><el-form-item label="商户店铺：" prop="storeId" v-if="formData.merId">
          <el-select
            :disabled="doneEdit"
            @change="changeStoreId"
            clearable
            v-model="formData.storeId"
            placeholder="请选择商户店铺"
          >
            <el-option
              v-for="(item, index) in computerStore(storeList)"
              :key="index"
              :label="item.storeName"
              :value="item.storeId"
            >
            </el-option>
          </el-select> </el-form-item
        ><el-form-item
          label="上级分类："
          prop="parentId"
          v-if="formData.storeId"
        >
          <el-cascader
            :disabled="doneEdit"
            clearable
            placeholder="请选择上级分类"
            v-model="formData.parentId"
            :options="computerTree(categorytreeList)"
            :show-all-levels="false"
            :props="{
              label: 'categoryName',
              value: 'categoryId',
              checkStrictly: true,
              emitPath: false,
            }"
          ></el-cascader> </el-form-item
        ><el-form-item label="商品分类：" prop="categoryName">
          <el-input
            placeholder="请输入商品分类"
            v-model="formData.categoryName"
          /> </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,
    },
    treeData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      doneEdit: false,
      formData: {
        merId: "",
        storeId: "",
        parentId: "",
        categoryName: "",
        status: 1,
      },
      rules: {
        merId: [
          { required: true, message: "请选择合作商户", trigger: "change" },
        ],
        storeId: [
          { required: true, message: "请选择商户店铺", trigger: "change" },
        ],
        parentId: [
          { required: true, message: "请选择上级分类", trigger: "change" },
        ],
        categoryName: [
          { required: true, message: "请输入商品分类", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
    };
  },
  methods: {
    changeMerId(e) {
      this.$set(this.formData, "storeId", null);
    },
    changeStoreId(e) {
      this.$set(this.formData, "parentId", null);
    },
    init() {
      this.formData = JSON.parse(JSON.stringify(this.treeData));
      if (this.formData.createTime) {
        this.doneEdit = true;
      } else {
        this.doneEdit = false;
      }
    },
    close() {
      this.$refs["formData"].resetFields();
    },
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.$api[
            this.formData.categoryId
              ? "editmallgoodscategorysave"
              : "addmallgoodscategorysave"
          ](this.formData).then((res) => {
            this.$message.success("成功");
            this.$store.commit("CATEGORYTREELIST");
            this.isShow = false;
            this.$emit("search");
          });
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    ...mapGetters(["merList", "storeList", "categorytreeList"]),
    computerStore: function () {
      return function (rows) {
        var array = [];
        if (rows?.length > 0) {
          array = rows.filter((i) => i.merId == this.formData.merId) || [];
        }
        return array;
      };
    },
    computerTree: function () {
      return function (rows) {
        var array = [
          {
            categoryId: 0,
            categoryName: "---无---",
          },
        ];
        if (rows?.length > 0) {
          array.push(
            ...rows.filter((i) => i.storeId === this.formData.storeId)
          );
        }
        return array;
      };
    },
    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>
