<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef"
      v-model:search="search"
      v-model="form"
      :data="data"
      :option="option"
      v-model:page="page"
      @row-save="createRow"
      @row-update="updateRow"
      @row-del="deleteRow"
      @search-change="searchChange"
      @search-reset="resetChange"
      @size-change="dataList"
      @current-change="dataList"
      @selection-change="selectionChange"
    >
      <template #menu="{ size, row, index }">
        <el-button
          icon="el-icon-edit"
          text
          v-if="row.state === '-1' || row.state === '0'"
          @click="handleEdit(row, index)"
          type="primary"
          :size="size"
          >派工调整</el-button
        >
        <el-button
            icon="el-icon-edit"
            text
            v-if="row.productLineCode === '1'"
            @click="handleDevice(row, index)"
            type="primary"
            :size="size"
        >资源调整</el-button
        >
      </template>
      <template #menu-left>
        <div id="charts"></div>
      </template>
    </avue-crud>
    <el-dialog
        v-model="dialog4.visible"
        :title="dialog4.title"
        width="950px"
        @close="dialog4.visible = false"
    >
      <plan_device_task :workOrderInfo="form" />
    </el-dialog>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { getUserList } from "@/api/system/user";
import { useCrud } from "@/hooks/userCrud";
import dictDataUtil from "@/common/configs/dictDataUtil";
import { queryStationByLineId } from "@/api/station";
import { useDictionaryStore } from "@/store";
import { getStatistics } from "@/api/order/index";
import * as echarts from "echarts";

// 数据字典相关
const { dicts } = useDictionaryStore();

// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/plan/task",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具

const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const stationList = ref([]);
const userList = ref([])
const charts = shallowRef(null);
const handleEdit = (row, index) => {
  queryStationByLineId(row.productLineId).then((data) => {
    stationList.value = data.data;
  });
  crudRef.value && crudRef.value.rowEdit(row, index);
};
const handleDevice =(row,index) =>{
  form.value = row
  dialog4.visible = true
}
const dialog4 = reactive({
  title: "资源调整",
  visible: false,
});
const queryUserList = () => {
  getUserList({}).then((data) => {
    userList.value = data.data;
  });
};
onMounted(() => {
  charts.value = echarts.init(document.getElementById("charts"));
  queryUserList();
  dataList();

  getStatistics(search.value).then((res) => {
    const { data } = res;
    const { value, key } = data;
    charts.value.setOption({
      title: {
        text: "任务状态统计图",
        left: "center",
      },
      tooltip: {
        trigger: "item",
      },
      xAxis: {
        data: key,
      },
      yAxis: {},
      series: [
        {
          type: "bar",
          data: value,
          itemStyle: {
            barBorderRadius: 5,
            borderWidth: 1,
            borderType: "solid",
            borderColor: "#73c0de",
            shadowColor: "#5470c6",
            shadowBlur: 3,
          },
        },
      ],
    });
  });
});
const validatePass1 = (rule, value, callback) => {
  if (form.value.planStartWhen != "" && form.value.planStartWhen != null) {
    if (getTime(form.value.planStartWhen) > getTime(form.value.planStartEnd)) {
      callback(new Error("开始时间不能大于结束时间"));
    } else {
      callback();
    }
  } else {
    callback();
  }
};
const getTime = (str) => {
  const isoDateString = str.replace(" ", "T");
  const date = new Date(isoDateString);
  const timestamp = Math.floor(date.getTime());
  return timestamp;
};
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  selection: true,
  menu: true,
  menuWidth: 100,
  addBtn: false,
  filterBtn: false,
  searchShowBtn: false,
  columnBtn: false,
  gridBtn: false,
  editBtn: false,
  viewBtn: false,
  delBtn: false,
  column: [
    {
      label: "流转卡号",
      prop: "seqNo",
      editDisabled: true,
      search: true,
    },
    {
      label: "工单编码",
      prop: "workOrderCode",
      editDisabled: true,
      search: true,
    },
    {
      label: "工位名称",
      prop: "stationName",
      search: true,
      display: false,
    },
    {
      label: "工位名称",
      hide: true,
      editDisabled: false,
      type: "select",
      dicData: stationList,
      prop: "stationId",
      props: { label: "name", value: "id" },
      rules: [
        {
          required: true,
          message: "请选择工位名称",
          trigger: "blur",
        },
      ],
    },
    {
      label: "分配人员",
      prop: "allocationUser",
      width: 100,
      overHidden: true,
      type: "select",
      dicData: userList,
      props: { label: "userName", value: "userName" },
    },
    {
      label: "产线名称",
      prop: "productLineName",
      editDisabled: true,
    },
    {
      label: "工艺路线",
      prop: "routeName",
      editDisabled: true,
    },
    {
      label: "物料编码",
      prop: "materialCode",
      search: true,
      editDisabled: true,
    },

    {
      label: "工序编码",
      prop: "operationCode",
      editDisabled: true,
      search: true,
    },
    {
      label: "工序名称",
      prop: "operationName",
      editDisabled: true,
    },
    {
      label: "排序",
      prop: "operationSort",
      formatter: (val) => {
        return val.operationSort + 1;
      },
      editDisabled: true,
      width:55,
    },
    {
      label: "状态",
      prop: "state",
      type: "select",
      search: true,
      width: 80,
      editDisabled: true,
      dicUrl:
        dictDataUtil.request_url + dictDataUtil.TYPE_CODE.station_task_state,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
    },
    {
      label: "开始时间",
      prop: "planStartWhen",
      type: "datetime",
      valueFormat: "YYYY-MM-DD HH:mm:ss",
      rules: [
        {
          required: true,
          message: "开始时间不能大于结束时间",
          trigger: "blur",
          validator: validatePass1,
        },
      ],
    },
    {
      label: "结束时间",
      prop: "planStartEnd",
      type: "datetime",
      valueFormat: "YYYY-MM-DD HH:mm:ss",
    },
    {
      label: "来源",
      prop: "source",
      editDisabled: true
    },
  ],
});
</script>

<style lang="scss" scoped>
:deep(.avue-crud__left) {
  width: 100%;
}

#charts {
  width: 100%;
  height: 300px;
  border: 1px solid #ccc;
}
</style>
