<template>
  <CustomCard>
    <div>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-tree style="max-width: 600px" :data="typeTree" :props="{ children: 'children', label: 'label' }" @node-click="handleNodeClick" />
        </el-col>
        <el-col :span="18">
          <avue-crud
            ref="crudRef"
            v-model="state.form"
            v-model:search="state.query"
            v-model:page="state.pageData"
            :table-loading="state.loading"
            :data="state.data"
            :option="state.options"
            @refresh-change="refreshChange"
            @search-reset="searchChange"
            @search-change="searchChange"
            @selection-change="selectionChange"
            @current-change="currentChange"
            @size-change="sizeChange"
            @row-del="rowDel"
            @row-save="rowSave"
            @row-update="rowUpdate"
          >
            <template #menu-left>
              <el-button type="primary" icon="Plus" @click="onAdd">新增</el-button>
              <el-button type="success" icon="download" @click="onExport">导出</el-button>
            </template>

            <template #menu="scope">
              <custom-table-operate :actions="state.options.actions" :data="scope" />
            </template>
          </avue-crud>
        </el-col>
      </el-row>
    </div>
  </CustomCard>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useApp } from '@/hooks';
import { CRUD_OPTIONS } from '@/config';
import CustomCard from '@/components/CustomCard.vue';

const { VITE_APP_BASE_API } = import.meta.env;
const app = useApp();

/* --------------- data --------------- */
// #region
const crudRef = ref(null);
const landUsedOptions = reactive([
  { label: '农用地', value: '0' },
  { label: '建设用地', value: '1' },
  { label: '住宅用地', value: '2' },
]);
const state = reactive({
  loading: false,
  query: {
    current: 1,
    size: 10,
  },
  form: {},
  selection: [],
  options: {
    ...CRUD_OPTIONS,
    addBtnText: '',
    addBtn: false,
    column: [
      { label: '编号', prop: 'gridManager', addDisplay: false, editDisplay: false },
      {
        label: '土地分类',
        prop: 'gridManager',
        dicData: landUsedOptions,
        multiple: true,
        rules: {
          required: true,
          message: '请选择',
          trigger: 'blur',
        },
      },
      { label: '土地类别', prop: 'gridManager' },
      { label: '状态', prop: 'gridManager', addDisplay: false, editDisplay: false },
    ],
    actions: [
      {
        name: (row) => getStatusButtonText(row.status),
        icon: 'edit',
        event: ({ row }) => rowStatus(row),
      },
      {
        name: '编辑',
        icon: 'edit',
        event: ({ row }) => rowEdit(row),
      },
      {
        type: 'danger',
        name: '删除',
        icon: 'delete',
        event: ({ row }) => rowDel(row),
      },
    ],
  },
  pageData: {
    total: 0,
    currentPage: 1,
    pageSize: 10,
  },
  data: [],
  currentRow: {},
});

let typeTree = reactive([
  {
    label: '农用地',
    id: '0',
    children: [
      { label: '耕地', id: '01', children: [], pId: '0' },
      { label: '园地', children: [], id: '02', pId: '0' },
    ],
  },
  {
    label: '建设用地',
    id: '1',
    children: [{ label: '城乡建设用地', children: [], id: '11', pId: '10' }],
  },
  {
    label: '住宅用地',
    id: '2',
    children: [],
  },
]);

// #endregion

/* --------------- methods --------------- */
const loadData = () => {
  //state.loading = true;
  // getAnnualList(state.query)
  //   .then((res) => {
  //     if (res.code === 200) {
  //       const { current, size, total, records } = res.data;
  //       state.data = records;
  //       state.pageData = {
  //         currentPage: current || 1,
  //         pageSize: size || 10,
  //         total: total,
  //       };
  //     }
  //   })
  //   .catch((err) => {
  //     app.$message.error(err.msg);
  //     state.data = [];
  //   })
  //   .finally(() => {
  //     state.loading = false;
  //   });
};

onMounted(() => {
  loadData();
});

// 页数
const currentChange = (current) => {
  state.query.current = current;
  loadData();
};

// 条数
const sizeChange = (size) => {
  state.query.size = size;
  loadData();
};

// 搜索
const searchChange = (params, done) => {
  if (done) done();
  state.query = params;
  state.query.current = 1;
  loadData();
};

// 刷新
const refreshChange = () => {
  loadData();
  app.$message.success('刷新成功');
};

// 选择
const selectionChange = (rows) => {
  state.selection = rows;
};

const handleNodeClick = (data) => {
  console.log('handleNodeClick', data);
};
// 编辑
const rowStatus = (row) => {
  console.info('操作状态');
};

const rowDel = (row) => {};

const getStatusButtonText = (status) => {
  switch (status) {
    case 'active':
      return '启用';
    case 'inactive':
      return '禁用';
  }
};

const rowEdit = (row) => {
  crudRef.value && crudRef.value.rowEdit(row);
};

const onAdd = () => {
  crudRef.value && crudRef.value.rowAdd();
};
const onExport = () => {};
const rowSave = (row, done, loading) => {
  // console.info('新增', row);
  // saveOperationRecord(row)
  //   .then((res) => {
  //     if (res.code === 200) {
  //       app.$message.success('添加成功！');
  //       done();
  //       loadData();
  //     }
  //   })
  //   .catch((err) => {
  //     app.$message.error(err.msg);
  //   })
  //   .finally(() => {
  //     loading();
  //   });
};

const rowUpdate = (row, index, done, loading) => {
  console.info('更新');
  // editOperationRecord(row)
  //   .then((res) => {
  //     if (res.code === 200) {
  //       app.$message.success('更新成功！');
  //       done();
  //       loadData();
  //     }
  //   })
  //   .catch((err) => {
  //     app.$message.error(err.msg);
  //   })
  //   .finally(() => {
  //     loading();
  //   });
};

// #region

// #endregion
</script>

<style lang="scss" scoped></style>
