<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-right"
          text
          v-if="row.state === '-1' || row.state === '0' || row.state === '1'"
          @click="doEdit(row, index)"
          type="primary"
          :size="size"
          >派工</el-button
        >
      </template>
      <template #menu-left>
        <div id="charts" style="margin-bottom: 10px"></div>
        <el-button
          v-hasPerm="[buttonPermission.PLAN.BTNS.plan_task_batchUpdate]"
          :disabled="toDeleteIds.length < 1"
          type="primary"
          icon="el-icon-edit"
          :size="size"
          @click="updateStation"
          >工位修改</el-button
        >
      </template>
    </avue-crud>
    <el-dialog
      v-model="centerDialogVisible"
      title="批量修改工位类型"
      width="500"
      align-center
    >
      <el-form
        :model="tmpForm"
        ref="tempForm"
        label-width="auto"
        style="max-width: 800px"
        :rules="rules"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="工位类型" prop="stationType">
              <el-select
                v-model="tmpForm.stationType"
                filterable
                placeholder="请选择工位类型"
                style="width: 240px"
              >
                <el-option
                  v-for="item in dicts.station_type"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitStation()"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { useCrud } from "@/hooks/userCrud";
import dictDataUtil from "@/common/configs/dictDataUtil";
import { queryStationByLineId } from "@/api/station";
import { getUserList } from "@/api/system/user";
import { useDictionaryStore } from "@/store";
import { getStatistics } from "@/api/order/index";
import * as echarts from "echarts";
import { updateStationTask } from "@/api/plan";
import buttonPermission from "@/common/configs/buttonPermission";

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

const submitStation = () => {
  tempForm.value.validate((isValid, invalidFields) => {
    if (isValid) {
      tmpForm.value.ids = toDeleteIds.value;
      updateStationTask(tmpForm.value).then((data) => {
        if (data.code === "200") {
          centerDialogVisible.value = false;
          ElMessage.success(data.msg);
          dataList();
        } else {
          ElMessage.error(data.msg);
        }
      });
    }
  });
};
// 传入一个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 centerDialogVisible = ref(false);
const updateStation = () => {
  //console.log(toDeleteIds.value);

  centerDialogVisible.value = true;
};
const handleEdit = (row, index) => {
  queryStationByLineId(row.productLineId).then((data) => {
    stationList.value = data.data;
  });
  option.value.column.forEach((item) => {
    if (item.prop === "allocationUser") {
      item.display = false;
    } else {
      item.display = true;
    }
  });
  /*option.value.column[0].display = true;
  option.value.column[2].display = true;
  option.value.column[3].display = false;
  option.value.column[9].display = true;
  option.value.column[4].display = true;
  option.value.column[5].display = true;
  option.value.column[6].display = true;
  option.value.column[7].display = true;
  option.value.column[8].display = true;
  option.value.column[10].display = true;
  option.value.column[11].display = true;
  option.value.column[12].display = true;
  option.value.column[13].display = true;*/
  crudRef.value && crudRef.value.rowEdit(row, index);
};

const doEdit = (row, index) => {
  getUserList({}).then((data) => {
    userList.value = data.data;
  });
  option.value.column.forEach((item) => {
    if (item.prop === "allocationUser" || item.prop === "operationName") {
      item.display = true;
    } else {
      item.display = false;
    }
  });
  /*option.value.column[0].display = false;
  option.value.column[1].display = false;
  option.value.column[2].display = false;
  option.value.column[3].display = true;
  option.value.column[9].display = true;
  option.value.column[4].display = false;
  option.value.column[5].display = false;
  option.value.column[6].display = false;
  option.value.column[7].display = false;
  option.value.column[8].display = false;
  option.value.column[10].display = false;
  option.value.column[11].display = false;
  option.value.column[12].display = false;
  option.value.column[13].display = false;*/
  crudRef.value && crudRef.value.rowEdit(row, index);
};
onMounted(() => {
  charts.value = echarts.init(document.getElementById("charts"));
  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 rules = reactive({
  stationType: [
    // required是否必填,message不符合此规则时的提示信息,
    // trigger触发此条规则校验的时机，有两个值, blur 或 change,默认就是blur和change都会进行校验
    // min此字段的最小长度，max此字段的最大长度
    // pattern 正则表达式
    { required: true, message: "工位类型不能为空", trigger: "blur" },
  ],
});
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  searchEnter: true,
  selection: true,
  reserveSelection: true,
  menu: true,
  menuWidth: 160,
  addBtn: false,
  filterBtn: false,
  searchShowBtn: false,
  columnBtn: false,
  gridBtn: false,
  editBtn: false,
  viewBtn: false,
  delBtn: false,
  selectable: function (row, index) {
    if (row.state === "0" || row.state === "-1") {
      return true;
    }
  },
  column: [
    {
      label: "管号",
      prop: "seqNo",
      editDisabled: true,
      search: true,
    },
    /* {
      label: "工位名称",
      prop: "stationName",
      //search: true,
      hide: true,
      display: false,
    },*/
    /*{
      label: "工位名称",
      hide: true,
      editDisabled: false,
      type: "select",
      dicData: stationList,
      filterable: true,
      prop: "stationId",
      props: { label: "name", value: "id" },
      rules: [
        {
          required: true,
          message: "请选择工位名称",
          trigger: "blur",
        },
      ],
    },*/
    {
      label: "工位类型",
      prop: "stationType",
      search: true,
      rules: [
        {
          required: true,
          message: "请选择工位类型",
          trigger: "blur",
        },
      ],
      type: "select",
      dicData: dicts.station_type,
      filterable: true, //添加filterable属性即可启用搜索功能
      props: {
        label: "dictLabel", // 下拉菜单显示的字段
        value: "dictValue", // 下拉菜单值的字段
      },
    },
    {
      label: "派工人员",
      editDisabled: false,
      type: "select",
      display: false,
      dicData: userList,
      filterable: true,
      prop: "allocationUser",
      props: { label: "userName", value: "userName" },
    },
    {
      label: "工单编码",
      prop: "workOrderCode",
      search: true,
      width: 125,
      editDisabled: true,
    },
    {
      label: "产线名称",
      prop: "productLineName",
      search: true,
      editDisabled: true,
    },
    {
      label: "工艺路线",
      prop: "routeName",
      editDisabled: true,
    },
    {
      label: "物料编号",
      prop: "materialCode",
      search: true,
      editDisabled: true,
    },

    {
      label: "工序编码",
      prop: "operationCode",
      editDisabled: true,
      search: true,
    },
    {
      label: "工序名称",
      prop: "operationName",
      search: true,
      editDisabled: true,
    },
    {
      label: "排序",
      prop: "operationSort",
      editDisabled: true,
      width: 60,
    },
    {
      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",
    },
    {
      label: "结束时间",
      prop: "planStartEnd",
      type: "datetime",
      valueFormat: "YYYY-MM-DD HH:mm:ss",
    },
  ],
});
</script>

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

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