<script>
export default {
  name: "ccList",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  data() {
    return {
      search: {name: ""},
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      colConfigs: [
        {prop: "appId", label: "应用编号"},
        {prop: "appName", label: "应用名称", width: 200},
        {prop: "status", label: "状态", width: 100, dict: "assessmentStartStatus"},
      ],
      dialog: false,
      form: {}
    }
  },
  computed: {
    dialogTitle: v => v.form?.id ? "编辑应用" : "新增应用",
    rules: v => ({
      appId: {required: true, message: "请输入AB_appid", trigger: "blur"},
      appName: {required: true, message: "请输入应用名称", trigger: "blur"},
      appIconUrl: {required: true, message: "请输入应用图标", trigger: "blur"},
    })
  },
  methods: {
    getTableData() {
      this.instance.post("/app/appaiconfiginfo/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data.records.map(e => ({...e, count: 0}))
          this.page.total = res.data.total
        }
      })
    },
    handleAdd(row = {}) {
      const {appIconUrl: url, fileId} = row
      this.form = this.$copy({...row, appIconUrl: [{url, fileId}]})
      this.dialog = true
      // this.$router.push({hash: "#add", query: {id}})
    },
    handleDelete(ids) {
      this.$confirm("是否要删除?").then(() => {
        this.instance.post("/app/appaiconfiginfo/delete", null, {
          params: {ids}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("删除成功")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    },
    handlePublic(id, status = 0) {
      const dict = {0: '发布', 1: '撤销'}
      status = Number(status)
      this.$confirm(`确认${dict[status]}当前应用信息吗？${dict[status]}后可通过${dict[(status + 1) % 2]}按钮${dict[(status + 1) % 2]}应用`).then(() => {
        this.instance.post("/app/appaiconfiginfo/updatePublishStatus", null, {
          params: {id}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success(`${dict[status]}成功`)
            this.getTableData()
          }
        })
      }).catch(() => 0)
    },
    submit() {
      this.$refs.form.validate().then(() => {
        const {appIconUrl} = this.form, {url, fileId} = appIconUrl.at(0) || {}
        this.instance.post("/app/appaiconfiginfo/addOrUpdate", {...this.form, appIconUrl: url, fileId}).then(res => {
          if (res?.code == '0') {
            this.$message.success("提交成功!")
            this.getTableData()
            this.dialog = false
          }
        })
      })
    }
  },
  created() {
    this.$dict.load("copilotAbility", "assessmentStartStatus")
    this.getTableData()
  },
}
</script>

<template>
  <section class="ccList">
    <ai-list>
      <ai-title slot="title" title="定制方案" isShowBottomBorder/>
      <template #content>
        <ai-search-bar>
          <template #left>
            <el-button type="primary" icon="iconfont iconAdd" @click="handleAdd()">添加</el-button>
          </template>
          <template #right>
            <el-input size="small" placeholder="搜索" v-model="search.appName" clearable
                      @change="page.current=1,getTableData()"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                  @getList="getTableData" :col-configs="colConfigs" :dict="dict">
          <el-table-column slot="options" label="操作" fixed="right" width="520" header-align="center">
            <template slot-scope="{row}">
              <div class="flex center">
                <el-button type="text" @click="handleAdd(row)">编辑</el-button>
                <el-button v-if="row.status!=1" type="text" @click="handlePublic(row.id,row.status)">发布</el-button>
                <el-button v-else type="text" @click="handlePublic(row.id,row.status)">撤销</el-button>
                <el-button type="text" @click="handleDelete(row.id)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
    <ai-dialog v-model="dialog" :title="dialogTitle" @close="form={}" width="600px" @confirm="submit">
      <el-form ref="form" :model="form" size="small" label-width="120px" :rules="rules">
        <el-form-item label="应用名称" prop="appName">
          <el-input v-model="form.appName" clearable :maxlength="6" show-word-limit/>
        </el-form-item>
        <el-form-item label="AB_appid" prop="appId">
          <el-input v-model="form.appId" clearable/>
        </el-form-item>
        <el-form-item label="独立能力" prop="ability">
          <ai-select v-model="form.ability" dict="copilotAbility"/>
        </el-form-item>
        <el-form-item label="应用图标" prop="appIconUrl">
          <ai-uploader v-model="form.appIconUrl" :instance="instance" :limit="1"/>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </section>
</template>

<style scoped lang="scss">
.ccList {
  height: 100%;
}
</style>
