<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('页面地址')" prop="ucc_code">
        <el-input
          v-model="form.ucc_code"
          clearable
          :placeholder="__('请输入页面地址')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('渠道名称')" prop="user_channel_id">
        <el-select
          v-model="form.user_channel_id"
          clearable
          :placeholder="__('请选择渠道名称')"
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="item in channels"
            :key="item.user_channel_id"
            :label="item.user_channel_name"
            :value="item.user_channel_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="ucc_enable">
        <el-switch
          v-model="form.ucc_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 {
    getListChannel,
    doEditChannelCode,
    doAddChannelCode,
  } from '@/api/user/channel'

  export default {
    name: 'UserResourceEdit',
    data() {
      return {
        form: {
          ucc_enable: 1,
        },
        channels: [],
        rules: {
          ucc_code: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输入页面地址'),
            },
          ],
          user_channel_id: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请选择渠道名称'),
            },
          ],
          ucc_enable: [
            {
              required: true,
              trigger: 'change',
              message: this.__('请选择是否启用'),
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.getChannelList()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          ucc_enable: 1,
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.ucc_id && this.form.ucc_id != undefined) {
              const { msg, status } = await doEditChannelCode(this.form)
              if (200 == status) {
                this.$baseMessage(msg, 'success')
              } else {
                this.$baseMessage(msg, 'error')
              }
              this.$emit('fetch-data')
              this.close()
            } else {
              const { msg, status } = await doAddChannelCode(this.form)
              if (200 == status) {
                this.$baseMessage(msg, 'success')
              } else {
                this.$baseMessage(msg, 'error')
              }
              this.$emit('fetch-data')
              this.close()
            }
          } else {
            return false
          }
        })
      },
      async getChannelList() {
        const params = {}
        params.user_channel_enable = 1
        await getListChannel(params)
          .then((res) => {
            this.channels = res.data.items
          })
          .catch((err) => {
            console.log(err)
          })
      },
    },
  }
</script>
