<template>
  <div>
    <x-card>
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.name" allow-clear placeholder="请输入名称" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 6) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table ref="table" :columns="columns" :data="loadData" :alert="true" :rowKey="record => record.id">
        <template slot="operator" v-if="hasPerm('industry:add')">
          <a-button @click="AddorEdit({}, 0)" icon="plus" type="primary" v-if="hasPerm('industry:add')"
            >新增行业</a-button
          >
        </template>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('industry:edit')" @click="AddorEdit(record, 1)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('industry:delete')" />
          <a-popconfirm
            v-if="hasPerm('industry:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => Delete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <editForm ref="editForm" :title="title" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable, XCard } from '@/components'
import { List, Delete } from '@/api/modular/main/industry'
import { GetClientDataItem, GetClientDataList } from '@/utils/clientdata'
import editForm from './editForm'
export default {
  components: {
    XCard,
    STable,
    editForm
  },
  data() {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      title: '',
      // 表头
      columns: [
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '备注',
          dataIndex: 'remark'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return List(Object.assign(parameter, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  created() {
    if (this.hasPerm('industry:status') || this.hasPerm('industry:edit')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    GetClientDataList,
    handleOk() {
      this.$refs.table.refresh()
    },
    // 获取枚举信息
    getenumval(type, item) {
      return GetClientDataItem(type, item)
    },
    // 新增或者编辑
    AddorEdit(record, type) {
      this.title = type === 1 ? '编辑行业' : '新增行业'
      this.$refs.editForm.init(record)
    },
    // 删除
    Delete(record) {
      Delete({
        id: record.id
      })
        .then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$myerror(res.message)
          }
        })
        .catch(err => {
          this.$myerror(err.message)
        })
    }
  }
}
</script>
<style scoped lang="less">
/deep/ .ant-table td {
  white-space: nowrap;
}
/deep/ .ant-table-body {
  overflow-x: auto !important;
}
/deep/ .ant-table-thead > tr > th,
.ant-table-tbody > tr > td {
  white-space: nowrap;
}
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
