<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 = [],handleSysTypeChange(form.type)"/>
                      </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>
              <ai-card title="扩展设置">
                <template #content>
                  <template v-if="form.type=='mp'">
                    <el-form-item label="小程序AppId">
                      <el-input v-model="form.appId" clearable placeholder="小程序appId"/>
                    </el-form-item>
                    <ai-title title="底部导航栏"/>
                    <ai-table :tableData="tabBar.list" :colConfigs="colConfigs" tableSize="mini" :isShowPagination="false" border>
                      <el-table-column slot="options" label="操作" width="80" align="center">
                        <template slot-scope="{row}">
                          <ai-dialog-btn text="更换" dialogTitle="选择应用">
                            <ai-lib-table :meta="appList" v-model="row.id" @select="v=>handleTabbarChange(row,v)" :isShowPagination="false" v-bind="$props"
                                          :border="false"/>
                          </ai-dialog-btn>
                        </template>
                      </el-table-column>
                    </ai-table>
                  </template>
                  <template v-else-if="form.type=='wxwork'">
                    <el-row type="flex">
                      <div class="fill">
                        <el-form-item label="接口是否单服务">
                          <el-checkbox v-model="form.isSingleService"/>
                        </el-form-item>
                        <el-form-item label="是否启用水印">
                          <el-checkbox v-model="form.waterMarker"/>
                        </el-form-item>
                      </div>
                      <div class="fill">
                        <el-form-item label="默认首页">
                          <el-input v-model="form.homePage" clearable placeholder="填写应用的文件名"/>
                        </el-form-item>
                        <el-form-item label="开启百度流量">
                          <el-checkbox v-model="form.hmt"/>
                        </el-form-item>
                      </div>
                    </el-row>
                  </template>
                </template>
              </ai-card>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="方案应用" lazy>
            <ai-lib-table :meta="appList" :isShowPagination="false" v-bind="$props" disabled :colConfigs="appListConfigs"/>
          </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 AiLibTable from "./AiLibTable";

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
      }) || []
    },
  },
  data() {
    return {
      form: {apps: [], type: null},
      rules: {
        name: {required: true, message: "请输入"},
        type: {required: true, message: "请选择"},
        customPath: {required: true, message: "请输入"},
      },
      colConfigs: [
        {prop: 'text', label: "名称"},
        {prop: 'pagePath', label: "应用路径"},
        {prop: 'iconPath', label: "默认图标"},
        {prop: 'selectedIconPath', label: "选中图标"},
      ],
      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: "模块名"}
      ],
      tabBar: {
        color: "#666666",
        selectedColor: "#197DF0",
        backgroundColor: "#ffffff",
        list: [
          {pagePath: "pages/AppHome/AppHome", text: "首页", iconPath: "static/TabBar/home.png", selectedIconPath: "static/TabBar/home_selected.png"},
          {pagePath: "pages/AppModules/AppModules", text: "应用", iconPath: "static/TabBar/service.png", selectedIconPath: "static/TabBar/service_selected.png"},
          {
            pagePath: "pages/AppEnteringVillage/AppEnteringVillage", text: "进村",
            iconPath: "static/TabBar/custom.png", selectedIconPath: "static/TabBar/custom_selected.png"
          },
          {pagePath: "pages/AppMine/AppMine", text: "我的", iconPath: "static/TabBar/me.png", selectedIconPath: "static/TabBar/me_selected.png"}
        ]
      }
    }
  },
  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
          this.handleSysTypeChange(this.form.type, this.form.extra)
        }
      })
    },
    back() {
      this.$router.push({})
    },
    submit() {
      this.$refs.AddForm.validate(v => {
        if (v) {
          const {tabBar, form: {type, appId, isSingleService, homePage}} = this
          if (type == 'mp') {
            this.form.extra = {tabBar, appId}
          } else if (type == 'wxwork') {
            this.form.extra = {isSingleService, homePage}
          }
          this.instance.post("/node/custom/addOrUpdate", this.form).then(res => {
            if (res?.code == 0) {
              this.$message.success("提交成功!")
              this.back()
            }
          })
        }
      })
    },
    handleSysTypeChange(v, data = {}) {
      let values = this.$copy(data)
      if (v == 'mp') {
        if (values?.tabBar) {
          this.tabBar = values.tabBar || this.tabBar
          delete values.tabBar
        }
      }
      Object.keys(values).map(e => this.$set(this.form, e, values[e]))
    },
    handleTabbarChange(row, {name, label}) {
      row.text = label
      row.pagePath = `pages/${name}/${name}`
    }
  },
  created() {
    this.getDetail()
  }
}
</script>

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

  .mar-l16 {
    margin-left: 16px;
  }
}
</style>
