<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="750px"
    @close="close"
  >
    <el-form ref="form" label-width="90px" :model="form" :rules="rules">
      <el-form-item
        :label="__('服务名称')"
        label-width="110px"
        prop="chain_project_name"
      >
        <el-input
          v-model="form.chain_project_name"
          :placeholder="__('服务名称')"
        />
      </el-form-item>
      <el-form-item
        :label="__('服务图片')"
        label-width="110px"
        prop="chain_project_image"
      >
        <Upload
          height="100px"
          :image="form.chain_project_image"
          width="170px"
          @upImage="
            (image) => {
              form.chain_project_image = image
            }
          "
        />
      </el-form-item>
      <el-form-item
        :label="__('可配置状态')"
        label-width="110px"
        prop="chain_project_state"
      >
        <el-select
          v-model="form.chain_project_state"
          :placeholder="__('请选择')"
          style="width: 190px"
        >
          <el-option
            v-for="item in projectStateList"
            :key="item.project_id"
            :label="item.project_name"
            :value="item.project_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        :label="__('激活状态')"
        label-width="110px"
        prop="chain_project_enable"
      >
        <el-radio-group v-model="form.chain_project_enable">
          <el-radio :label="1">{{ __('启用') }}</el-radio>
          <el-radio :label="0">{{ __('关闭') }}</el-radio>
        </el-radio-group>
      </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 } from '@/api/chain/project/base'
  import Upload from '@/components/upload'

  export default {
    name: 'ChainProjectEdit',
    components: { Upload },
    data() {
      return {
        form: {
          chain_project_name: '',
          chain_project_image: '',
          chain_project_state: '',
          chain_project_enable: 1,
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
        projectStateList: [
          {
            project_id: 401,
            project_name: '忙碌',
          },
          {
            project_id: 402,
            project_name: '适中',
          },
          {
            project_id: 403,
            project_name: '空闲',
          },
        ],
      }
    },
    created() {},
    mounted() {
      this.rules = {
        chain_project_id: [
          { required: true, trigger: 'blur', message: '服务编号' },
        ],
        chain_project_name: [
          { required: true, trigger: 'blur', message: '服务名称' },
        ],
        chain_project_image: [
          { required: true, trigger: 'blur', message: '配置图片' },
        ],
        chain_project_state: [
          { required: true, trigger: 'blur', message: '可配置状态' },
        ],
        project_enable: [
          { required: true, trigger: 'blur', message: '是否启用' },
        ],
      }

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