<template>
  <div class="midPopUp" v-if="checkPop" @click="checkPop = false">
    <div class="container" @click.stop>
      <div class="title titleText" style="font-size: 38px">消息指派</div>
      <el-scrollbar class="listBody">
        <el-form ref="formRef" :model="form" label-width="aotu" :key="formKey">
          <el-form-item
            :rules="{
              required: true,
              message: '该选项为必选',
              trigger: 'change',
            }"
            label="标题："
            prop="title"
            ><el-input v-model="form.title" />
          </el-form-item>
          <el-form-item
            :rules="{
              required: true,
              message: '该选项为必选',
              trigger: 'change',
            }"
            label="批号："
            prop="workOrderCode"
          >
            <el-select
              v-model="form.workOrderCode"
              :disabled="route.name != 'ProcessMain'"
              placeholder="Select"
              filterable
              @change="getType"
            >
              <el-option
                v-for="item in oderList"
                :key="item.value"
                :label="item.workOrderCode"
                :value="item.workOrderCode"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            :rules="{
              required: true,
              message: '该选项为必选',
              trigger: 'change',
            }"
            label="工位："
            prop="stationType"
          >
            <el-select
              multiple
              clearable
              v-model="form.stationType"
              placeholder="Select"
            >
              <el-option
                v-for="item in stationList"
                :key="item"
                :label="dictS.getLableByValue('station_type', item)"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            :rules="{
              required: true,
              message: '该选项为必选',
              trigger: 'change',
            }"
            label="备注："
            prop="content"
            ><el-input v-model="form.content" type="textarea" />
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <div class="btns">
        <el-button
          type="primary"
          @click="validate"
          class="titleText"
          style="width: 260px; height: 50px; border-radius: 16px"
          >提 交
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { sendMsg, getStationType } from "@/api/process";
import { emitter, EventsNames } from "@/utils/common";
import { useDictionaryStore } from "@/store";
import { useProcessStore } from "@/store/modules/processView";
import { getTaskWorkOrder } from "@/api/process";
const dictS = useDictionaryStore();
const store = useProcessStore();
const route = useRoute();
//点检项数组
const form = ref({ stationType: [] });
const resetForm = () => {
  form.value = reactive({ stationType: [] });
};
const formRef = ref(null);
const checkPop = ref(false);
const stationList = ref([]);
const formKey = ref(false);
const getType = async () => {
  const { data } = await getStationType(form.value.workOrderCode);
  stationList.value = data;
  form.value.stationType = [];
};
const open = () => {
  formKey.value = !formKey.value;
  resetForm();
  if (route.name != "ProcessMain") {
    form.value.workOrderCode = store.odersData.workOrderCode;
    getType();
  }
  checkPop.value = true;
};
const validate = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      submit();
    } else {
      ElMessage.error("请检查是否有未填项");
    }
  });
};
const submit = async () => {
  const { data, code } = await sendMsg(form.value);
  if (code == "200") {
    ElMessage.success("提交成功！");
    resetForm();
    formKey.value = !formKey.value;
    checkPop.value = false;
  }
};
const getOrdersData = async () => {
  const { code, data } = await getTaskWorkOrder({
    pageNo: 1,
    pageSize: 999999999,
    queryComplete: 0,
  });
  if (code == "200") {
    oderList.value = data.records;
  }
};
const oderList = ref([]);
// emitter.on(EventsNames.ORDER_LIST, (val) => {
//   oderList.value = val.value;
// });
defineExpose({ open });
onMounted(() => {
  getOrdersData();
});
</script>

<style lang="scss" scoped>
.title {
  width: 100%;
  height: 50px;
  text-align: center;
  margin-bottom: 20px;
}

:deep(label) {
  display: inline-block;
}

.listBody {
  height: calc(100% - 160px);
  padding: 0 $p20;

  .item {
    width: 100%;
    background-color: white;
    border-radius: 16px;
    margin: 10px 0;
    padding: $p10 $p20;

    .showInfo {
      .remark {
        font-size: $f20;
        color: red;
        margin-right: 5px;
      }
    }
  }
}

.btns {
  width: 100%;
  height: 50px;
  @include flex;
}

//表单字体样式
:deep(.el-form-item__label) {
  font-size: $f20;
  max-width: 600px;
  display: inline-block;
  height: auto;
  padding-right: 0px;
}

:deep(.el-radio__label) {
  font-size: $f20;
}

:deep(.el-radio) {
  display: flex;
  align-items: center;
}

:deep(.el-textarea__inner) {
  font-size: $f20;
}
</style>
