<template>
  <div :class="$style.weight">
    <a-card>
      <h2>搜索字段 权重(取值范围[1,10])</h2>
      <a-divider />
      <a-form
        :form="form"
        :label-col="{ span: 5 }"
        :wrapper-col="{ span: 16 }"
        @submit="handleSubmit"
      >
        <a-form-item v-for="item in data" :key="item.id" :label="item.fieldName">
          <a-input-number v-model="item.weight" :min="1" :max="10" />
        </a-form-item>
        <a-form-item :wrapper-col="{ offset: 9 }">
          <a-button @click="handleReset">
            重置
          </a-button>
          <a-button type="primary" html-type="submit">
            确定
          </a-button>
        </a-form-item>
      </a-form>
    </a-card>
  </div>
</template>

<script>
import { Message } from 'ant-design-vue'
import components from './_import-components/sd-search-weight-import'
import WeightService from './weight-service'

export default {
  name: 'SdSearchWeight',
  metaInfo: {
    title: '权重配置',
  },
  components,
  data() {
    return {
      data: [],
      form: this.$form.createForm(this),
      initData: [],
    }
  },
  created() {
    this.getInitData()
  },
  methods: {
    getInitData() {
      const params = {
        columns: 'id,fieldName,fieldCode,weight,status,moduleCode',
        expressions: [
          {
            dataType: 'str',
            name: 'deleted',
            op: 'eq',
            stringValue: '0',
          },
        ],
        formId: 'oaFtsSearchConfig',
        maxResults: 10,
        orderBy: 'id asc',
        startPosition: 0,
      }
      WeightService.getWetigheInfo(params).then((res) => {
        this.data = res?.data
        this.initData = JSON.parse(JSON.stringify(this.data))
      })
    },
    handleReset() {
      this.data = JSON.parse(JSON.stringify(this.initData))
    },
    handleSubmit(e) {
      e.preventDefault()
      WeightService.saveWeightSettings(this.data).then((res) => {
        if (res.data) {
          Message.success('提交成功')
        } else {
          Message.error('提交失败')
        }
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.weight {
  h2 {
    width: 100%;
    margin-left: 1rem;
    text-align: center;
  }
}
:global(.ant-btn-primary) {
  margin-left: 5rem;
}
</style>
