<template>
  <div class="my-alert">
    <div class="breadcrumb"></div>
    <div class="content">
      <bg-table-list>
        <template #filter>
          <bg-filter-form
            :keyword="search"
            placeholder="请输入任务名称"
            :options="filter_data"
            @keyword="keywordAction"
            @filters="get_filter_list"
          >
            <template #left_action>
              <div class="left_action">
                <el-button type="primary" @click="AddWorkOrder">
                  新增
                </el-button>
                <el-button type="danger" @click="delBatch">
                  批量删除
                </el-button>
                <span class="select-num">
                  已选择
                  <span>{{ selectedItems.length }}</span>
                  项
                </span>
                <el-button type="primary" link @click="clearSelected"
                  >清空</el-button
                >
              </div>
            </template>
          </bg-filter-form>
        </template>

        <template #main>
          <bg-table
            ref="listtable"
            :headers="headers"
            :rows="rows"
            height="100%"
            :isIndex="true"
            :isSelect="true"
            :stripe="true"
            @select="selectRows"
          >
            <template #work_order_name="{ row, header }">
              <BgTableHref :row="row" :header="header"></BgTableHref>
            </template>
            <template #status="{ row }">
              <span>
                <span>{{ status[row.status] }}</span>
                <span v-if="row.status != 3">
                  （{{ row.finish }}/{{ row.all }}）
                </span>
              </span>
            </template>
            <template #operation="{ row }">
              <div class="table-operation">
                <span class="primary-edit" @click="operation(row)">
                  手动下发
                </span>
                <span class="line"></span>
                <span class="primary-edit" @click="Edit(row)"> 编辑 </span>
                <span class="line"></span>
                <span class="primary-edit" @click="Delete(row)"> 删除 </span>
              </div>
            </template>
          </bg-table>
        </template>
        <template #footer>
          <bg-pagination
            :pagination="pagination"
            @sizechange="sizechange"
            @current="current"
          >
          </bg-pagination>
        </template>
      </bg-table-list>
      <ManualDistribution v-model:visible="visible" :active_row="active_row" />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick, computed } from "vue";
import bgTableList from "@/components/table/bg-table-list.vue";
import BgTableHref from "@/components/table/bg-table-href.vue";
import bgFilterForm from "@/components/table/bg-filter-form.vue";
import bgPagination from "@/components/table/bg-pagination.vue";
import bgTable from "@/components/table/bg-table.vue";
import ManualDistribution from "@/components/manual-distribution/index.vue";
const status = {
  1: "已完成",
  2: "待处置",
  3: "已关闭",
};
let search = ref("");
let filter_data = reactive([
  {
    label: "工单等级",
    prop: "work_order_level",
    type: "select",
    options: [
      {
        value: "",
        label: "全部",
      },
    ],
  },
  {
    label: "状态",
    prop: "status",
    type: "select",
    options: [
      {
        value: "",
        label: "全部",
      },
    ],
  },
  {
    label: "创建时间",
    prop: "create_time",
    type: "datetimerange",
  },
  {
    label: "完成时间",
    prop: "finish_time",
    type: "datetimerange",
  },
]);
let headers = reactive([
  {
    label: "工单名称",
    prop: "work_order_name",
    align: "left",
    href: {
      baseurl: "/work-order/business-manage-detail",
      params: { id: "id", name: "work_order_name" },
    },
  },
  {
    label: "工单等级",
    prop: "work_order_level",
    align: "left",
  },
  {
    label: "实例工单数",
    prop: "example_work_order_num",
    align: "left",
  },
  {
    label: "下发对象",
    prop: "distribute_target",
    align: "left",
  },
  {
    label: "定时规则",
    prop: "timing_rules",
    align: "left",
  },
  {
    label: "定时是否启用",
    prop: "status",
    align: "left",
    width: 150,
  },
  {
    label: "创建人",
    prop: "create_by",
    align: "left",
  },
  {
    label: "创建时间",
    prop: "create_time",
    align: "left",
  },
  {
    label: "操作",
    prop: "operation",
    align: "left",
    width: "200px",
  },
]);
let rows = ref([
  { work_order_name: 11, id: 111, status: 1, finish: 10, all: 10 },
  { work_order_name: 22, id: 222, status: 2, finish: 5, all: 10 },
  { work_order_name: 33, id: 333, status: 3, finish: 0, all: 10 },
]);
const pagination = reactive({
  rowsPerPage: 15,
  page: 1,
  total: 0,
});
let searchData = reactive({
  create_time: [],
  finish_time: [],
  work_order_level: "",
  status: "",
});
// *********分页start
const sizechange = (val) => {
  pagination.page = 1;
  pagination.rowsPerPage = val;
  init();
};
const current = (val) => {
  pagination.page = val;
  init();
};
//搜索内容
const get_filter_list = (
  { create_time, finish_time, work_order_level, status },
  val
) => {
  pagination.rowsPerPage = 15;
  pagination.page = 1;
  searchData.create_time = create_time || [];
  searchData.finish_time = finish_time || [];
  searchData.work_order_level = work_order_level;
  searchData.status = status;
  search.value = val.value;
  init();
};

const init = async () => {
  let [createStartTime, createEndTime] = searchData.create_time;
  let [finishStartTime, finishEndTime] = searchData.finish_time;
  let urlParams = {
    keyword: search.value || "",
    createStartTime,
    createEndTime,
    finishStartTime,
    finishEndTime,
    work_order_level: searchData.work_order_level,
    status: searchData.status,
    page: pagination.page,
    limit: pagination.rowsPerPage,
  };
  //   const { data, total } = await instanceList(urlParams);
  //   rows.value = data.list || [];
  //   pagination.total = data.total_count;
};
const keywordAction = async (val) => {
  search.value = val;
  init();
};
// 手动下发
const visible = ref(false);
const active_row = ref(null);
const operation = (row) => {
  active_row.value = row;
  visible.value = true;
};
// 编辑调用
const Edit = (row) => {
  active_row.value = row;
};
// 删除调用
const selectedItems = ref([]);
const Delete = (row) => {
  selectedItems.value = [row];
  console.log(del_ids.value);
};
const del_ids = computed(() => {
  return selectedItems.value.map((e) => e.id) || [];
});
// 表格选中
const selectRows = (items) => {
  selectedItems.value = items;
};
// 新增
const AddWorkOrder = () => {};
// 批量删除
const delBatch = () => {
  console.log(del_ids.value);
};
// 清空选中
const listtable = ref(null);
const clearSelected = () => {
  listtable.value.clearSelection();
};
onMounted(() => {
  init();
});
</script>

<style lang="scss" scoped>
.my-alert {
  width: 100%;
  height: 100%;
  padding: 0 24px 16px;
  .breadcrumb {
    width: 100%;
    height: 46px;
  }
  .content {
    width: 100%;
    height: calc(100% - 46px);
    background-color: #ffffff;
    box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
    border-radius: 6px;
    .select-num {
      margin: 0 40px 0 24px;
      font-size: 14px;
      color: #404a62;
      span {
        font-weight: bold;
      }
    }
    .table-operation {
      display: flex;
      align-items: center;
      .line {
        width: 1px;
        height: 14px;
        background-color: #c1c7d7;
        margin: 0 16px;
        display: inline-block;
      }
    }
  }
}
</style>
