<template>
  <el-dialog
    append-to-body
    :title="__('店铺经营类目')"
    :visible.sync="dialogTableVisibleCategory"
    width="600px"
  >
    <el-form ref="form" label-width="80px" :model="form" size="medium">
      <el-form-item v-if="false" :label="__('是否启用')">
        <el-radio-group v-model="form.store_business_category_enable" size="medium">
          <el-radio
            v-for="(item, index) in enableOptions"
            :key="index"
            :disabled="item.disabled"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="__('选择分类')"
        label-width="90px"
        prop="category_id"
      >
        <tree-select
          ref="treeSelect"
          :checked-keys="defaultCheckedKeys"
          clearable
          :data="data"
          :default-props="defaultProps"
          :height="300"
          :node-key="nodeKey"
          size="small"
          :width="220"
          @change="change"
        />
      </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, getListCategory } from '@/api/store/business/category'
  import { getTree } from '@/api/base/product/category'
  import TreeSelect from '@/components/treeSelect'

  export default {
    name: 'SetAward',
    components: { TreeSelect },
    data() {
      return {
        list: [],
        defaultCheckedKeys: [],
        data: [],
        defaultProps: {
          children: 'children',
          label: 'label',
        },
        listLoading: true,
        title: '',
        form: {
          store_business_category_enable: 1,
          store_business_category_id: undefined,
        },
        nodeKey: 'id',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        dialogTableVisibleCategory: false,
        enableOptions: [
          {
            label: this.__('启用'),
            value: 1,
          },
          {
            label: this.__('禁用'),
            value: 0,
          },
        ],
      }
    },
    created() {
      this.getTree()
    },
    mounted() {
      this.$forceUpdate()
    },
    methods: {
      __,
      async getTree() {
        const { data } = await getTree()
        this.data = data
      },
      // 获取规格数据
      showEdit(row, store_id) {
        if (row == null) {
          this.form.store_id = store_id
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.defaultCheckedKeys.push(row.category_id)
          this.form = Object.assign({}, row)
        }
        this.dialogTableVisibleCategory = true
      },
      close() {
        this.$refs['treeSelect'].isShowSelect = false
        this.$refs['treeSelect'].selectedData = []
        this.form = this.$options.data().form
        this.dialogTableVisibleCategory = false
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getListCategory(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      change(id) {
        this.form.category_id = id
      },
      async save() {
        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()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
    },
  }
</script>
