<template>
  <section class="add">
    <ai-detail>
      <ai-title slot="title" :title="pageTitle" isShowBottomBorder />
      <template #content>
        <el-tabs tab-position="left">
          <el-tab-pane label="方案设置">
            <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">
                    <div class="fill">
                      <el-form-item label="系统类型" prop="type">
                        <ai-select v-model="form.type" :selectList="dict.getDict('systemType')"
                          @change="form.apps = []" />
                      </el-form-item>
                      <el-form-item label="更新项目路径" prop="dist">
                        <el-input v-model="form.dist" placeholder="常填写nginx路径,下载包从这里取" clearable />
                      </el-form-item>
                    </div>
                    <div class="fill mar-l16">
                      <el-form-item label="库项目根路径" prop="customPath">
                        <el-input v-model="form.customPath" placeholder="请输入" clearable />
                      </el-form-item>
                      <el-form-item label="版本号" prop="version">
                        <el-input v-model="form.version" placeholder="请输入" clearable />
                      </el-form-item>
                    </div>
                  </el-row>
                </template>
              </ai-card>
              <ai-card title="主库应用">
                <template #content>
                  <ai-lib-table v-if="form.type" v-model="form.apps" v-bind="$props" multiple searchKey="name"
                    :action="`/node/wechatapps/list?type=${form.type}&isMain=1`" border />
                  <ai-empty v-else>请先选择系统类型</ai-empty>
                </template>
              </ai-card>
              <component class="extraConfig" title="扩展设置" :is="extraConfig" v-model="form.extra" :appList="appList" />
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="方案应用" lazy>
            <ai-lib-table :meta="appList" customData :isShowPagination="false" v-bind="$props" disabled
              :colConfigs="appListConfigs">
              <template slot="options" slot-scope="{row}">
                <ai-dialog-btn text="编辑" :customFooter="false" dialogTitle="应用配置" width="500px"
                  @onConfirm="handleAppEdit(row)">
                  <el-form size="small" label-width="80px">
                    <el-form-item label="应用名称">
                      <el-input v-model="row.label" clearable placeholder="请输入应用名称" />
                    </el-form-item>
                  </el-form>
                </ai-dialog-btn>
              </template>
            </ai-lib-table>
          </el-tab-pane>
        </el-tabs>
      </template>
      <template #footer>
        <el-button @click="back">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </template>
    </ai-detail>
  </section>
</template>

<script>
import { mapActions } from "vuex"
import AiLibTable from "./AiLibTable";
import webConfig from "./config/webConfig.vue";
import wxcpConfig from "./config/wxcpConfig.vue";
import wxmpConfig from "./config/wxmpConfig.vue";

export default {
  name: "add",
  components: { AiLibTable },
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    isEdit: v => !!v.$route.query.id,
    pageTitle: v => v.isEdit ? "编辑定制方案" : "新增定制方案",
    appList() {
      return this.form.appList?.map(e => {
        e.category = e.libPath.replace(/^\/[^\/]+\/([^\/]+)\/.+/, '$1')
        if (/\/project\//.test(e.libPath)) {
          e.project = e.libPath.replace(/.*project\/([^\/]+)\/.+/, '$1')
        } else if (/\/core\//.test(e.libPath)) {
          e.project = "core"
        } else e.project = "standard"
        return e
      }).filter(e => e.project != "core") || []
    },
    extraConfig: v => ({
      web: webConfig,
      wxwork: wxcpConfig,
      mp: wxmpConfig
    }[v.form.type])
  },
  data() {
    return {
      form: { apps: [], type: null, customPath: "", extra: { sysInfo: {} } },
      rules: {
        name: { required: true, message: "请输入" },
        type: { required: true, message: "请选择" },
        // customPath: {required: true, message: "请输入"},
      },
      appListConfigs: [
        { prop: 'label', label: "应用名称", render: (h, { row }) => h(row.tabbar ? 'b' : 'p', row.label + `  ${row.tabbar ? '(底部导航栏)' : ''}`) },
        { prop: 'project', label: "项目/框架" },
        { prop: 'category', label: "分类", dict: "appsCategory" },
        { prop: 'name', label: "模块名" }
      ],
    }
  },
  methods: {
    ...mapActions(['closePage']),
    getDetail() {
      let { id } = this.$route.query
      id && this.instance.post("/node/custom/detail", null, {
        params: { id }
      }).then(res => {
        if (res?.data) {
          this.form = { ...this.form, ...res.data }
        }
      })
    },
    back() {
      this.closePage()
      this.$router.push({})
    },
    submit() {
      this.$refs.AddForm.validate(v => {
        if (v) {
          if (this.form.type == 'web') {
            this.form.extra.sysInfo.desc = JSON.parse(this.form.extra.sysInfo.desc || null)
            this.form.extra.sysInfo.ssl = this.form.extra.sysInfo.ssl?.replace(/"/g, "'")
          }
          this.instance.post("/node/custom/addOrUpdate", this.form).then(res => {
            if (res?.code == 0) {
              this.$message.success("提交成功!")
              this.getDetail()
            }
          })
        }
      })
    },
    handleAppEdit(row) {
      const i = this.form.appList.findIndex(e => e.id == row.id)
      this.form.appList.splice(i, 1, row)
    },

  },
  created() {
    this.getDetail()
  }
}
</script>

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

  .mar-l16 {
    margin-left: 16px;
  }

  :deep(.tabBarOptions) {
    flex-wrap: wrap;

    .el-button--text+.el-button--text {
      margin-left: 0;
    }
  }

  :deep(.extraConfig) {
    .ai-card__body {
      display: grid;
      grid-template-columns: 1fr 1fr;

      .row {
        grid-column: span 2;
      }
    }
  }
}
</style>
