<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="900px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="editFormRules">
      <el-form-item :label="__('选择商品')" label-width="110px" prop="item_id">
        <el-input
          v-model="form.item_id"
          clearable
          :placeholder="__('请选择商品')"
          readonly
          :style="{ width: '100%' }"
          @focus="showProduct"
        />
      </el-form-item>
      <el-form-item :label="__('商品名称')" label-width="110px" prop="name">
        <el-input
          v-model="form.name"
          :placeholder="__('商品名称，最长14个汉字')"
        />
      </el-form-item>
      <el-form-item
        :label="__('商品图片')"
        label-width="110px"
        prop="coverImgUrl"
      >
        <picUpload
          :file-list="[form.coverImgUrl]"
          :height="175"
          :show-file-list="false"
          style="width: 50%"
          :width="175"
          @get-file="
            (value) => {
              form.coverImgUrl = value
            }
          "
          @remove-file="(value) => (form.coverImgUrl = value)"
        />
        <span>300 * 300px</span>
      </el-form-item>
      <el-form-item
        :label="__('价格显示类型')"
        label-width="110px"
        prop="priceType"
      >
        <el-radio-group v-model="form.priceType">
          <el-radio :label="1">{{ __('一口价') }}</el-radio>
          <el-radio :label="2">{{ __('价格区间') }}</el-radio>
          <el-radio :label="3">{{ __('限时折扣') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="__('价格')" label-width="110px" prop="price">
        <el-input v-model="form.price" :placeholder="__('价格')" />
      </el-form-item>
      <el-form-item
        v-if="form.priceType != 1"
        :label="__('价格2')"
        label-width="110px"
        prop="price2"
      >
        <el-input v-model="form.price2" :placeholder="__('价格2')" />
      </el-form-item>
      <el-form-item :label="__('小程序路径')" label-width="110px" prop="url">
        <el-input v-model="form.url" disabled :placeholder="__('小程序路径')" />
      </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>
    <productItem ref="productItem" @checkItem="getItem" />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import picUpload from '@/components/material/PicUpload'
  import { addGood, updateGood } from '@/api/base/product/live'
  import productItem from '@/components/activity/ProductItemDialogTable'

  export default {
    name: 'LiveBaseEdit',
    components: { picUpload, productItem },
    data() {
      return {
        editFormRules: {
          coverImgUrl: [{ required: true, trigger: 'blur', message: '背景图' }],
          name: [{ required: true, trigger: 'blur', message: '商品名称' }],
          priceType: [{ required: true, trigger: 'blur', message: '价格类型' }],
          url: [{ required: true, trigger: 'blur', message: '小程序路径' }],
        },
        activeName: 'first',
        form: {
          coverImgUrl: '',
        },
        formData: {
          pageNum: 1,
          pageSize: 9999,
        },
        isAdd: true,
        value: '',
        title: '',
        dialogFormVisible: false,
        rules: {},
        loading: false,
      }
    },
    created() {},
    mounted() {
      this.$forceUpdate()
    },
    methods: {
      __,
      showProduct() {
        this.$refs['productItem'].showTable()
      },
      getItem(item) {
        this.form.item_id = item.item_id
        this.form.name = item.item_name
        this.form.url = 'pages/product/detail?pid=' + item.item_id
        this.$forceUpdate()
      },
      showEdit(row) {
        if (!row) {
          this.isAdd = true
          this.title = this.__('添加')
        } else {
          this.isAdd = false
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = this.isAdd
              ? await addGood(this.form)
              : await updateGood(this.form)

            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }

            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      close() {
        this.checkedData = []
        this.$refs['form'].resetFields()
        this.form.coverImgUrl = ''
        this.dialogFormVisible = false
      },
    },
  }
</script>
