<template>
  <div class="chain-category-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button
          icon="el-icon-upload2"
          type="primary"
          @click="toggleRowExpansion(true)"
        >
          {{ __('全部展开') }}
        </el-button>
        <el-button
          icon="el-icon-download"
          type="primary"
          @click="toggleRowExpansion(false)"
        >
          {{ __('全部折叠') }}
        </el-button>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(null, null)"
        >
          {{ __('添加') }}
        </el-button>
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="dataTreeList"
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      row-key="chain_category_id"
    >
      <el-table-column align="center" :label="__('操作')" width="180">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row, null)">
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="!row.chain_category_id"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
          <el-button
            type="text"
            @click="handleEdit(null, row.chain_category_id)"
          >
            {{ __('新增') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('分类编号')"
        prop="chain_category_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('门店分类名称')"
        prop="chain_category_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('父编号')"
        prop="chain_category_pid"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否启用')"
        prop="chain_category_enable"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.chain_category_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="chain_category_sort"
        show-overflow-tooltip
      />
    </el-table>
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, editState, doDelete } from '@/api/chain/category/base'
  import Edit from './components/chainCategoryEdit'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ChainCategory',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    computed: {
      ...mapGetters({
        rid: 'user/rid',
      }),
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      setSelectRows(val) {
        this.selectRows = val
      },
      async sChange(row) {
        const param = {
          chain_category_id: row.chain_category_id,
          chain_category_enable: row.chain_category_enable,
        }
        const { msg, status } = await editState(param)
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        await this.fetchData()
      },
      toggleRowExpansion(isExpansion) {
        this.listLoading = true
        this.toggleRowExpansion_forAll(this.list, isExpansion)
        this.listLoading = false
      },
      toggleRowExpansion_forAll(data, isExpansion) {
        data.forEach((item) => {
          this.$refs.dataTreeList.toggleRowExpansion(item, isExpansion)
          if (item.children !== undefined && item.children != null) {
            this.toggleRowExpansion_forAll(item.children, isExpansion)
          }
        })
      },
      handleEdit(row, chain_category_id) {
        if (!row || chain_category_id) {
          this.$refs['edit'].showEdit(null, chain_category_id)
        } else {
          this.$refs['edit'].showEdit(row)
        }
      },
      handleDelete(row) {
        if (row.chain_category_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            if (row.children) {
              this.$baseMessage('不能选择根目录', 'error')
              return
            }
            const { msg, status } = await doDelete({
              chain_category_id: row.chain_category_id,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows
              .map((item) => item.chain_category_id)
              .join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ ids })
                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }
                await this.fetchData()
              }
            )
          } else {
            this.$baseMessage(this.__('未选中任何行'), 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items !== null ? data.items : []
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
