<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper" v-if="hasPerm('classifyDistIndex:page')">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="16" :sm="20">
            <a-radio-group default-value="1" v-model="queryParam.systemDictType" button-style="solid" @change="$refs.table.refresh(true)">
              <a-radio-button v-for="(item,index) in typeList" :key="index" :value="item.code">
                {{ item.name }}
              </a-radio-button>
            </a-radio-group>
          </a-col>
          <a-col :md="8" :sm="4">
            <a-button type="primary" style="margin-left: 8px" v-if="hasPerm('classifyDistIndex:add')" icon="plus" @click="$refs.addForm.add({'systemDictType': queryParam.systemDictType})">新增</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.systemDictId"
    >
      <span slot="systemDictType" slot-scope="text">
        {{ typeFilter(text) }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a-popconfirm placement="top" :title="record.systemDictIsLine===1? '是否确认下线该条目？':'是否确认上线该条目？'" @confirm="() => editjobStatusStatus(record.systemDictIsLine,record)">
          <a>{{ statusFilter(record.systemDictIsLine) }}</a>
        </a-popconfirm>
        <a-divider type="vertical"/>
        <a v-if="hasPerm('classifyDistIndex:edit')" @click="$refs.editForm.edit(record)">编辑</a>
        <a-divider type="vertical" v-if="hasPerm('classifyDistIndex:edit') && hasPerm('classifyDistIndex:delete')"/>
        <a-popconfirm v-if="hasPerm('classifyDistIndex:delete')" placement="topRight" title="是否确认删除该条目？" @confirm="() => classifyDistIndexDelete(record)">
          <a>删除</a>
        </a-popconfirm>
      </span>
    </s-table>
    <add-form ref="addForm" @ok="handleOk" />
    <edit-form ref="editForm" @ok="handleOk" />
  </a-card>
</template>
<script>
  import { STable, Ellipsis } from '@/components'
  import homeApi from '@/api/homeApi'
  import addForm from './addClassifyDictForm'
  import editForm from './editClassifyDictForm'

  export default {
    name: 'ClassifyDistIndex',
    components: {
      STable,
      Ellipsis,
      addForm,
      editForm
    },
    data () {
      return {
        typeList: homeApi.systemTypeList,
        // 查询参数
        queryParam: { id: '', systemDictType: 1 },
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: 'dataIndex',
            customRender: (text, row, index) => {
              return index + 1
            },
            align: 'center',
            width: '90px'
          },
          {
            title: '所属类别',
            dataIndex: 'systemDictType',
            scopedSlots: { customRender: 'systemDictType' }
          },
          {
            title: '分类',
            dataIndex: 'systemDictClassify',
            scopedSlots: { customRender: 'systemDictClassify' }
          },
          {
            title: '子分类',
            dataIndex: 'systemDictSubclassify',
            scopedSlots: { customRender: 'systemDictSubclassify' }
          },
          {
            title: '排序',
            dataIndex: 'systemDictSort',
            align: 'center'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return homeApi.findSystemDictById(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        statusDictTypeDropDown: [{ code: 0, name: '下线' }, { code: 1, name: '上线' }]
      }
    },
    created () {
      if (this.hasPerm('classifyDistIndex:edit') || this.hasPerm('classifyDistIndex:delete') || hasPerm('classifyDistIndex:start') || hasPerm('classifyDistIndex:stop')) {
        this.columns.push({
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        })
      }
    },
    methods: {
      statusFilter (status) {
        const values = this.statusDictTypeDropDown.filter(item => item.code === status)
        if (values.length > 0) {
          return values[0].name
        } else {
          return '上线'
        }
      },
      typeFilter (status) {
        const values = this.typeList.filter(item => item.code === status)
        if (values.length > 0) {
          return values[0].name
        } else {
          return ''
        }
      },
      /**
       * 启动停止
       */
      editjobStatusStatus (code, record) {
        if (code === 1) {
          homeApi.updateIsLineSystemDict({ id: record.systemDictId, isLine: 0 }).then(res => {
            if (res.status === 200) {
              if (res.data) {
                this.$message.success('下线成功')
                this.$refs.table.refresh()
              } else {
                this.$message.error('下线失败')
              }
            } else {
              this.$message.error('下线失败：' + res.msg)
            }
          })
        } else {
          homeApi.updateIsLineSystemDict({ id: record.systemDictId, isLine: 1 }).then(res => {
            if (res.status === 200) {
              if (res.data) {
                this.$message.success('上线成功')
                this.$refs.table.refresh()
              } else {
                this.$message.error('上线失败')
              }
            } else {
              this.$message.error('上线失败：' + res.msg)
            }
          })
        }
      },
      classifyDistIndexDelete (record) {
        homeApi.deleteSystemDict({ id: record.systemDictId }).then((res) => {
          if (res.status === 200) {
            if (res.data) {
              this.$message.success('删除成功')
              this.$refs.table.refresh()
            } else {
              this.$message.error('删除失败')
            }
          } else {
            this.$message.error('删除失败：' + res.msg)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
