<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <div>
        <h3>一级菜单</h3>
        <el-form-item label="菜单名字" prop="first">
          <el-input
            v-model="form.name"
            size="small"
            :rows="2"
            placeholder="菜单名字"
          ></el-input>
        </el-form-item>
      </div>
      <div>
        <h3>二级菜单</h3>
        <el-button type="primary" size="small" @click="addField">
          添加二级菜单
        </el-button>
        <div v-for="(items, index) in form.sub_button" :key="index">
          <el-form-item label="菜单类型" prop="type">
            <el-select
              v-model="items.type"
              size="small"
              style="width: 100%"
              filterable
              placeholder="菜单类型"
            >
              <el-option
                v-for="item in typeData"
                :key="item.dataCode"
                style="width: 100%"
                :label="item.dataValue"
                :value="item.dataCode"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="菜单名字" prop="name">
            <el-input
              v-model="items.name"
              size="small"
              :rows="2"
              placeholder="二级菜单名字"
            ></el-input>
          </el-form-item>
          <el-form-item label="菜单url" prop="url">
            <el-input
              v-model="items.url"
              size="small"
              type="textarea"
              :rows="2"
              placeholder="菜单url"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="danger" plain @click="deleteItem(items, index)">
              删除
            </el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>
    <div class="dialog-footer">
      <el-button size="small" @click="close">取 消</el-button>
      <el-button type="primary" size="small" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addMenuPub, getMenuType, getMenuList } from '@/api/wechat';
export default {
  data() {
    return {
      form: {
        name: '',
        sub_button: [],
      },
      isFirst: false,
      typeData: [],
      menuData: [],
      title: '',
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    addField() {
      this.form.sub_button.push({
        name: '',
        type: '',
        url: '',
      });
    },
    async getMenuList() {
      const { data } = await getMenuList();
      this.menuData = data;
    },
    deleteItem(item, index) {
      this.form.sub_button.splice(index, 1);
    },
    //菜单类型
    async getMenuTypeData() {
      const { data } = await getMenuType();
      this.typeData = data;
    },
    showEdit() {
      this.title = '添加菜单';
      this.dialogFormVisible = true;
      this.getMenuTypeData();
      this.getMenuList();
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          this.menuData.push(this.form);
          const { msg } = await addMenuPub(this.menuData);
          this.$notify({
            title: msg,
            type: 'success',
          });
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          this.$parent.fetchData();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
