<template>
  <section class="add">
    <ai-detail>
      <ai-title slot="title" :title="pageTitle" isShowBottomBorder/>
      <template #content>
        <el-form ref="AddForm" :model="form" size="small" label-width="120px" :rules="rules">
          <ai-card title="基本信息">
            <template #content>
              <el-form-item label="项目/系统名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入" clearable/>
              </el-form-item>
              <el-row type="flex">
                <el-form-item label="系统类型" prop="type" class="half">
                  <ai-select v-model="form.type" :selectList="dict.getDict('systemType')"/>
                </el-form-item>
                <el-form-item label="定制项目根目录" prop="customPath" class="half">
                  <el-input v-model="form.customPath" placeholder="请输入" clearable/>
                </el-form-item>
              </el-row>
            </template>
          </ai-card>
          <ai-card title="主库应用">
            <template #content>
              <el-form-item label-width="0" prop="apps">
                <ai-table-select v-if="form.type" v-model="form.apps" :instance="instance" :action="`/node/wechatapps/list?type=${form.type}`"
                                 nodeName="label" extra="libPath" multiple/>
                <ai-empty v-else>请先选择系统类型</ai-empty>
              </el-form-item>
            </template>
          </ai-card>
        </el-form>
      </template>
      <template #footer>
        <el-button @click="back">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </template>
    </ai-detail>
  </section>
</template>

<script>
export default {
  name: "add",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    isEdit: v => !!v.$route.query.id,
    pageTitle: v => v.isEdit ? "编辑定制项目" : "新增定制项目",
  },
  data() {
    return {
      form: {apps: []},
      rules: {
        name: {required: true, message: "请输入"},
        type: {required: true, message: "请选择"},
        customPath: {required: true, message: "请输入"},
        apps: {required: true, message: "请选择"},
      },
      mainLibApps: []
    }
  },
  methods: {
    getDetail() {
      let {id} = this.$route.query
      id && this.instance.post("/node/custom/detail", null, {
        params: {id}
      }).then(res => {
        if (res?.data) {
          this.form = res.data
        }
      })
    },
    back() {
      this.$router.push({})
    },
    submit() {
      this.$refs.AddForm.validate(v => {
        if (v) {
          this.instance.post("/node/custom/addOrUpdate", this.form).then(res => {
            if (res?.code == 0) {
              this.$message.success("提交成功!")
              this.back()
            }
          })
        }
      })
    },
  },
  created() {
    this.getDetail()
  }
}
</script>

<style lang="scss" scoped>
.add {
  height: 100%;

  .half {
    width: 50%;

    & + .half {
      margin-left: 16px;
    }
  }
}
</style>
