<template>
  <div class="my-business-ticket">
    <div class="breadcrumb">
      <bg-breadcrumb />
    </div>
    <div class="content">
      <bg-filter-group @search="changeSearch" v-model="filter.search" placeholder="请输入角色名称">
        <template v-slot:filter_group>
          <div class="left-filter filter_list">
            <div class="filter_item">
              <span class="filter_title">工单等级</span>
              <el-select v-model="filter.order_level" placeholder="请选择" style="width: 300px">
                <el-option
                  v-for="(item, index) in ticketLevelOptions"
                  :key="'order_level_options' + index"
                  :label="item"
                  :value="index">
                </el-option>
              </el-select>
            </div>
            <div class="filter_item">
              <span class="filter_title">状态</span>
              <el-select v-model="filter.order_state" placeholder="请选择" style="width: 300px">
                <el-option
                  v-for="(item, index) in statusOptions"
                  :key="'order_state_options' + index"
                  :label="item"
                  :value="index">
                </el-option>
              </el-select>
            </div>
            <div class="filter_item">
              <span class="filter_title">创建时间</span>
              <el-date-picker
                v-model="filter.create_time"
                type="datetimerange"
                value-format="YYYY-MM-DD HH:mm:ss"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间">
              </el-date-picker>
            </div>
            <div class="filter_item">
              <span class="filter_title">完成时间</span>
              <el-date-picker
                v-model="filter.finish_time"
                type="datetimerange"
                value-format="YYYY-MM-DD HH:mm:ss"
                :disabled-date="disabledDate"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间">
              </el-date-picker>
            </div>
          </div>
          <div class="right-action apaas_button">
            <el-button type="primary" @click="filterAction"> 查询 </el-button>
            <el-button type="default" @click="filterClear"> 重置 </el-button>
          </div>
        </template>
      </bg-filter-group>
      <div class="table_container">
        <div class="table">
          <bg-table ref="listtable" :headers="headers" :rows="rows" height="100%" :isIndex="true" :stripe="true">
            <template #order_name="{ row }">
              <div class="href" @click="goTo(row)">{{ row.order_name || "-" }}</div>
            </template>
            <template #order_level="{ row }">
              <span>{{ ticketLevelOptions[row.order_level] }}</span>
            </template>
            <template #order_state="{ row }">
              <span class="status-body">
                <span class="status" :class="`status-${row.order_state}`"></span>
                <span>{{ statusOptions[row.order_state] }}</span>
              </span>
            </template>
            <template #create_time="{ row }">
              {{ dateStringToDate(row.create_time) }}
            </template>
            <template #complete_time="{ row }">
              {{ dateStringToDate(row.complete_time) }}
            </template>
            <template #operation="{ row }">
              <el-button type="primary" :disabled="row.order_state != 1" link size="small" @click="operation(row)">
                处置反馈
              </el-button>
            </template>
          </bg-table>
          <div class="pagination_box">
            <bg-pagination
              :page="filter.page"
              :size="filter.page_size"
              :total="tableTotal"
              @change-page="changePage"
              @change-size="changeSize">
            </bg-pagination>
          </div>
        </div>
      </div>
      <FeedBack v-model:visible="visible" :active_row="active_row" />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from "vue";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
import FeedBack from "../modules/feedback.vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import axios from "@/request/http.js";
import { dateStringToDate } from "@/components/env";
const router = useRouter();
const filter = reactive({
  order_level: 0,
  search: "",
  create_time: [],
  finish_time: [],
  order_state: 0,
  page: 1,
  page_size: 10,
});
const filterClear = () => {
  filter.order_level = 0;
  filter.search = "";
  filter.order_state = 0;
  filter.create_time = [];
  filter.finish_time = [];
  filter.page_size = 10;
  filter.page = 1;
  changePage(1);
}; // 重置筛选项
const tableTotal = ref(0);
const ticketLevelOptions = ["全部", "紧急任务", "重要任务", "一般任务"];
const statusOptions = ["全部", "待处置", "已完成", "已关闭"];
const disabledDate = (time) => {
  return time.getTime() > Date.now();
};
let headers = reactive([
  {
    label: "工单名称",
    prop: "order_name",
    align: "left",
  },
  {
    label: "工单等级",
    prop: "order_level",
    align: "left",
  },
  {
    label: "工单状态",
    prop: "order_state",
    align: "left",
  },
  {
    label: "下发对象",
    prop: "push_obj",
    align: "left",
  },
  {
    label: "创建人",
    prop: "create_user",
    align: "left",
  },
  {
    label: "创建时间",
    prop: "create_time",
    align: "left",
    width: 160,
  },
  {
    label: "完成时间",
    prop: "complete_time",
    align: "left",
    width: 160,
  },
  {
    label: "操作",
    prop: "operation",
    align: "left",
    width: "80px",
    fixed: "right",
  },
]);
let rows = ref([]);
const changeSize = (size) => {
  filter.page_size = size;
  changePage(1);
};

const changePage = (page) => {
  filter.page = page;
  getTableRows();
};
const changeSearch = (val) => {
  filter.search = val;
  changePage(1);
}; // 表格关键字筛选

const filterAction = () => {
  changePage(1);
}; // 查询按钮
onMounted(() => {
  getTableRows();
});

const getTableRows = async () => {
  let [create_date_from = "", create_date_to = ""] = filter.create_time || [];
  let [complete_time_from = "", complete_time_to = ""] = filter.finish_time || [];
  let urlParams = {
    ...filter,
    create_date_from,
    create_date_to,
    complete_time_from,
    complete_time_to,
  };
  Reflect.deleteProperty(urlParams, "create_time");
  Reflect.deleteProperty(urlParams, "finish_time");
  axios.get("/v1/api/work_order/work_order_me/list", { params: urlParams }).then((res) => {
    if (res.data.code == 200) {
      rows.value = res.data.data || [];
      tableTotal.value = res.data.total;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
// 处置反馈
const visible = ref(false);
const active_row = ref(null);
const operation = (row) => {
  active_row.value = { ...row, url: "/v1/api/work_order/work_order_me/feedback" };
  visible.value = true;
};
const goTo = ({ order_issuance_id }) => {
  router.push({
    path: "/ticket/my-business-ticket/detail",
    query: {
      id: order_issuance_id,
    },
  });
};
</script>

<style lang="scss" scoped>
.my-business-ticket {
  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;
    display: flex;
    flex-direction: column;
    padding: 16px;
    :deep(.bg-filter-group) {
      padding: 0;
    }
    .filter-group {
      .left-filter {
        display: flex;
        justify-content: start;
        flex-wrap: wrap;
      }
      .right-action {
        white-space: nowrap;
      }
    }
    .table_container {
      flex: 1;
      display: flex;
      flex-direction: column;
      position: relative;
      .table {
        position: absolute;
        flex: 1;
        width: 100%;
        height: calc(100% - 48px);
        .href {
          font-size: 14px;
          color: #3759be;
          cursor: pointer;
        }
      }
    }
    :deep(.el-button.is-link) {
      line-height: 1;
    }
    .bg-pagination {
      bottom: unset;
    }
    .status-body {
      display: flex;
      align-items: center;
      .status {
        display: inline-block;
        width: 6px;
        height: 6px;
        border-radius: 50%;
        margin-right: 8px;
        $statusObj: (
          2: #48ad97,
          3: #9e9e9e,
          1: #3759be,
        );
        @each $status, $color in $statusObj {
          &-#{$status} {
            background-color: $color;
          }
        }
      }
    }
  }
}
</style>
