<template>
  <div class="page-view bg-white px-11 pb-11 border-radius-5">
    <app-filter ref="app-filter" :model="filters" @query="onQuery" @reset="onReset">
      <app-filter-item :filter-config="{ operation: 'EQUAL' }" el-type="input" label="资源名称" prop="pointName">
        <el-input v-model="filters.pointName" clearable placeholder="请输入" @keyup.enter.native="$refs['app-filter'].query" />
      </app-filter-item>
      <app-filter-item label="资源类型" prop="pointCode">
        <el-input v-model="filters.pointCode" clearable placeholder="请输入" @keyup.enter.native="$refs['app-filter'].query" />
      </app-filter-item>
    </app-filter>

    <div class="mb-15">
      <el-button icon="el-icon-plus" size="mini" type="primary" @click="addCompany"> 新增 </el-button>

      <el-button icon="el-icon-upload2" size="mini" type="primary"> 导入 </el-button>

      <el-button icon="el-icon-download" size="mini" type="primary"> 导出 </el-button>
    </div>

    <div class="flex-1 bg-white flex-col">
      <app-table
        :column="columns"
        :pagination="pagination"
        :table-data="tableData"
        :table-loading="tableLoading"
        :total="total"
        @pagination-change="onPaginationChange"
      />
    </div>

    <detail-dialog v-model="dialogVisible" :data="currentRow" :title="dialogTitle" @confirm="onCompanyConfirm" />

    <!--    <el-drawer :visible.sync="drawerVisible" :with-header="false" size="89%">-->
    <!--      <app-title-bar class="">-->
    <!--        数据源配置-->
    <!--        <template slot="right-area" />-->
    <!--      </app-title-bar>-->
    <!--    </el-drawer>-->
  </div>
</template>

<script>
import tableMixin from '@/mixins/table.js';
import DetailDialog from './DetailDialog';

export default {
  components: { DetailDialog },
  mixins: [tableMixin],
  props: {
    providerRow: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    const TABLE_CONFIG = {
      columns: [
        { label: '资源名称', prop: 'name' },
        {
          label: '资源类型',
          prop: 'is_active',
          formatData: (item) => {
            const str = item == true ? '数据库' : '上传';
            return str;
          },
        },
        {
          label: '来源格式',
          prop: 'type',
        },
        {
          label: '是否发布',
          prop: 'is_publish',
          formatData: (item) => {
            const str = item == true ? '是' : '否';
            return str;
          },
        },
        {
          label: '创建时间',
          prop: 'createdTime',
        },
        {
          dataType: 'option',
          label: '操作',
          width: '240px',
          operation: [
            {
              name: '表管理',
              type: 'text',
              size: 'mini',
              // icon: 'el-icon-edit-outline',
              plain: true,
              // showHide: (row) => {},
              clickFun: this.editCompany,
            },
            {
              name: '查看表',
              type: 'text',
              size: 'mini',
              // icon: 'el-icon-document',
              plain: true,
              // showHide: (row) => {},
              clickFun: this.editCompany,
            },
            {
              name: '查看触发器',
              type: 'text',
              size: 'mini',
              // icon: 'el-icon-mobile',
              plain: true,
              // showHide: (row) => {},
              clickFun: this.editCompany,
            },
          ],
        },
      ],
    };

    const DIALOG_CONFIG = {
      dialogVisible: false,
      dialogTitle: '新增',
    };

    const DRAWER_CONFIG = {
      drawerVisible: false,
    };
    return {
      ...TABLE_CONFIG, // table相关
      ...DIALOG_CONFIG, // 主列表dialog相关
      ...DRAWER_CONFIG, // 抽屉相关
    };
  },
  mounted() {},
  methods: {
    handleBack() {
      this.$router.push({
        name: 'dataCollectCompany',
      });
    },
    getData() {
      this.tableLoading = true;
      setTimeout(() => {
        this.tableData = [
          {
            id: 1,
            name: '测试资源1',
            is_active: true,
            is_publish: false,
            createdTime: '2024-11-22 08:54:33',
            type: '格式1',
          },
          {
            id: 2,
            name: '测试资源2',
            is_active: false,
            is_publish: true,
            createdTime: '2024-11-22 08:54:33',
            type: '格式2',
          },
        ];
        this.tableLoading = false;
      }, 1000);
    },
    addCompany() {
      this.dialogTitle = '新增';
      this.currentRow = null;
      this.dialogVisible = true;
    },
    editCompany(row) {
      this.dialogTitle = `编辑 - ${row.name}`;
      this.currentRow = row;
      this.dialogVisible = true;
    },
    delCompany(row) {
      this.$confirm(`此操作将永久删除该汇聚方（${row.name}）, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        const ind = this.tableData.findIndex((o) => o.id == row.id);
        ind >= 0 && this.tableData.splice(ind, 1);
      });
    },
    onCompanyConfirm(data) {
      const id = this.tableData.length++;
      if (data?.id) {
        const ind = this.tableData.findIndex((o) => o.id == data.id);
        ind >= 0 && this.tableData.splice(ind, 1, data);
      } else {
        this.tableData.push({ ...data, id });
      }
      this.dialogVisible = false;
    },
  },
};
</script>

<style></style>
