<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('分类名称')" prop="feedback_category_name">
        <el-input
          v-model="form.feedback_category_name"
          :placeholder="__('分类名称')"
        />
      </el-form-item>

      <el-form-item :label="__('反馈类型')" prop="payment_type_id">
        <el-select
          v-model="form.feedback_type_id"
          :placeholder="__('反馈类型')"
        >
          <el-option
            v-for="item in optionsId"
            :key="item.feedback_type_id"
            :label="item.feedback_type_name"
            :value="item.feedback_type_id"
          />
        </el-select>
      </el-form-item>

      <el-form-item prop="feedback_category_enable">
        <el-switch
          v-model="form.feedback_category_enable"

          active-text="开启"
          :active-value="1"

          inactive-text="关闭"
          :inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit, ListType } from '@/api/plantform/feedback/category'

  export default {
    name: 'PlantformFeedbackCategoryEdit',
    data() {
      return {
        optionsId: [],
        form: {
          feedback_category_enable: 1,
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.functionlistType()
    },
    mounted() {
      this.rules = {
        feedback_category_id: [
          { required: true, trigger: 'blur', message: this.__('反馈分类编号') },
        ],
        feedback_category_name: [
          { required: true, trigger: 'blur', message: this.__('分类名称') },
        ],
        feedback_type_id: [
          { required: true, trigger: 'blur', message: this.__('反馈类型') },
        ],
        feedback_category_enable: [
          { required: true, trigger: 'blur', message: this.__('是否启用') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      async functionlistType() {
        // 发请求获取渠道下拉框的值
        await ListType()
          .then((res) => {
            this.optionsId = res.data // 把获取到的数据赋给this.dataList
          })

          .catch((err) => {
            console.log(err)
          })
      },
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
