<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item :label="__('消费者保障名称')" prop="contract_type_name">
        <el-input
          v-model="form.contract_type_name"
          :placeholder="__('消费者保障名称')"
        />
      </el-form-item>

      <el-form-item :label="__('保障描述')" prop="contract_type_text">
        <el-input
          v-model="form.contract_type_text"
          :placeholder="__('保障描述')"
        />
      </el-form-item>
      <el-form-item :label="__('保证金')" prop="contract_type_deposit">
        <el-input
          v-model="form.contract_type_deposit"
          :placeholder="__('保证金')"
        />
      </el-form-item>
      <el-form-item :label="__('项目图标')" prop="contract_type_icon">
        <Upload
          height="100px"
          :image="form.contract_type_icon"
          width="100px"
          @upImage="
            (image) => {
              form.contract_type_icon = image
            }
          "
        />
      </el-form-item>
      <el-form-item v-if="false" :label="__('链接地址')" prop="contract_type_url">
        <el-input
          v-model="form.contract_type_url"
          :placeholder="__('链接地址')"
        />
      </el-form-item>
      <el-form-item :label="__('是否开启')" prop="contract_type_enable">
        <el-switch
          v-model="form.contract_type_enable"

          :active-value="1"

          :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 Upload from '@/components/upload'

  import { doEdit } from '@/api/base/contract/type'
  import { URL } from '@/config'
  export default {
    name: 'BaseContractTypeEdit',
    components: { Upload },
    data() {
      return {
        action: URL.action,
        form: {
          contract_type_icon: '',
          contract_type_enable: 1,
          unit_name: '',
        },
        rules: {
          contract_type_id: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('消费者保障编号'),
            },
          ],
          contract_type_name: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('消费者保障名称'),
            },
          ],
          contract_type_desc: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('消费者保障描述简写'),
            },
          ],
          contract_type_text: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('消费者保障描述'),
            },
          ],
          contract_type_deposit: [
            { required: true, trigger: 'blur', message: this.__('保证金') },
          ],
          // contract_type_icon: [
          //   { required: true, trigger: 'blur', message: this.__('项目图标') },
          // ],

          contract_type_order: [
            { required: true, trigger: 'blur', message: this.__('排序') },
          ],
          contract_type_enable: [
            { required: true, trigger: 'blur', message: this.__('是否开启') },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('新增')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          contract_type_icon: '',
          contract_type_enable: 1,
          unit_name: '',
        }
        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>
