<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item
        :label="__('是否启用')"
        prop="store_business_category_enable"
      >
        <el-input
          v-model="form.store_business_category_enable"
          :placeholder="__('是否启用')"
        />
      </el-form-item>

      <el-form-item
        :label="__('选择分类')"
        prop="category_id"
        :style="{ width: '100%' }"
      >
        <tree-select
          ref="treeSelect"
          v-model="form.category_id"
          :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">
      <baidu-map class="map" :center="{lng: 116.404, lat: 39.915}" :zoom="15">
          <bm-circle :center="circlePath.center" :radius="circlePath.radius" stroke-color="blue" :stroke-opacity="0.5" :stroke-weight="2" @lineupdate="updateCirclePath" :editing="true"></bm-circle>
      </baidu-map>
      <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 TreeSelect from '@/components/treeSelect'
  import { doEdit } from '@/api/store/business/category'
  import { getTree } from '@/api/base/product/category'
  import { getSpecMap } from '@/api/base/product/spec'
  import { getBrandMap } from '@/api/base/product/brand'

  export default {
    name: 'StoreBusinessCategoryEdit',
    components: { TreeSelect },
    data() {
      return {
        circlePath: {
                center: {
                lng: 116.404,
                lat: 39.915
                },
        radius: 500
      },
        form: {
          store_id: '',
          type_name: undefined,
          category_id: undefined,
        },
        specForm: [],
        brandForm: [],
        rules: {},
        title: '',
        selectSpecs: [], // 预选
        data: [], // 产品规格数据
        nodeKey: 'id',
        defaultProps: {
          children: 'children',
          label: 'label',
        },
        defaultCheckedKeys: [],
        dialogFormVisible: false,
      }
    },
    created() {
      this.getTree()
      this.getSpecMap()
      this.getBrandMap()
    },
    mounted() {
      this.rules = {
        store_business_category_id: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('店铺经营分类编号'),
          },
        ],
        store_id: [
          { required: true, trigger: 'blur', message: this.__('店铺编号') },
        ],

        store_business_category_commission_rate: [
          { required: true, trigger: 'blur', message: this.__('佣金比率') },
        ],
        store_business_category_enable: [
          { required: true, trigger: 'blur', message: this.__('是否启用') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      async getTree() {
        const { data } = await getTree()
        this.data = data
      },
      async getSpecMap() {
        const { data } = await getSpecMap()
        this.specForm = data
      },
      async getBrandMap() {
        const { data } = await getBrandMap()
        this.brandForm = data
      },

      change(id) {
        this.form.category_id = id
      },
      showEdit(row) {
        if (
          !row.store_id &&
          !row.category_id &&
          !row.store_business_category_id
        ) {
          this.form.store_id = row
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
          console.log(this.form.category_id)
        }

        this.dialogFormVisible = true
      },

      close() {
        this.$refs['treeSelect'].isShowSelect = false
        this.$refs['treeSelect'].selectedData = []
        this.form = this.$options.data().form
        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>
