<!--
 * @Author: SunTao 328867980@qq.com
 * @Date: 2024-11-20 11:58:33
 * @LastEditors: SunTao 328867980@qq.com
 * @LastEditTime: 2025-01-03 10:37:57
 * @FilePath: \znxjxt-ui\src\views\xj\document\maintenance-notice\components\edit-dialog.vue
 * @Description: 养护通知单编辑弹窗
-->
<template>
  <div class="edit-dialog">
    <el-form
      class="editForm"
      ref="editForm"
      :model="editForm"
      :rules="rules"
      label-width="7rem"
    >
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="道路名称:" prop="segmentId">
            <el-select
              v-model="editForm.segmentId"
              placeholder="请选择路段"
              clearable
            >
              <el-option
                v-for="item in segmentList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="开始桩号:" prop="stakeStart">
            <el-input v-model="editForm.stakeStart" placeholder="K0000+000" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束桩号:" prop="stakeEnd">
            <el-input v-model="editForm.stakeEnd" placeholder="K0000+000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="养护负责人:" prop="receiver">
            <el-input
              clearable
              v-model="editForm.receiver"
              placeholder="请输入负责人名称"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-form-item label="养护内容:" prop="taskDescription">
          <fssm-scroll style="max-height: 10rem">
            <div
              class="content-box"
              v-for="(item, index) in editForm.taskDescription"
              :key="`content-box-${index}`"
            >
              <el-col :span="21">
                <el-input
                  type="textarea"
                  resize="none"
                  placeholder="请输入养护内容"
                  v-model="editForm.taskDescription[index]"
                />
              </el-col>
              <el-col :span="2">
                <span
                  v-if="index < 1"
                  class="el-icon-circle-plus-outline"
                  @click="handleAddContent"
                ></span>
                <span
                  v-else
                  class="el-icon-delete"
                  @click="handleDeleteContent(index)"
                ></span>
              </el-col>
            </div>
          </fssm-scroll>
        </el-form-item>
      </el-row> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import FssmScroll from "@/components/scroll/fssm-scroll.vue";
import { addMaintenanceNotice } from "@/api/xj/document";
export default {
  name: "EditDialog",
  components: { FssmScroll },
  props: {
    dialogItem: {
      type: Object,
      default: () => {},
    },
    segmentList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      // 编辑表单
      editForm: {
        // 病害id
        defectId: "",
        // 道路名称
        segmentId: "",
        // 开始桩号
        stakeStart: "",
        // 结束桩号
        stakeEnd: "",
        // 养护单位项目负责人
        receiver: "",
        // 养护内容
        // taskDescription: [""],
      },
      // 表单验证规则
      rules: {
        segmentId: [
          { required: true, message: "请选择路段", trigger: "change" },
        ],
        stakeStart: [
          {
            required: true,
            message: "请输入开始桩号",
            trigger: "blur",
          },
          {
            pattern: /^K\d{4}\+\d{3}$/,
            message: "请输入正确的桩号格式",
            trigger: "blur",
          },
        ],
        stakeEnd: [
          {
            required: true,
            message: "请输入结束桩号",
            trigger: "blur",
          },
          {
            pattern: /^K\d{4}\+\d{3}$/,
            message: "请输入正确的桩号格式",
            trigger: "blur",
          },
        ],
        receiver: [
          { required: true, message: "请输入负责人名称", trigger: "blur" },
        ],
        // taskDescription: [
        //   {
        //     type: "array",
        //     required: true,
        //     defaultField: {
        //       type: "string",
        //       required: true,
        //       message: "请完整填写内容",
        //     },
        //   },
        // ],
      },
    };
  },
  watch: {
    dialogItem: {
      handler(val) {
        this.editForm = {
          ...val,
          segmentId: val.segmentId === "未知路段" ? "" : val.segmentId,
          id: null,
          defectId: val.id,
          createdTime: null,
          // taskDescription: [
          //   `${val.createdTimeLocal}  ${val.roadName || ""}-${
          //     val.segmentName || ""
          //   } 主线 ${val.stakeStart || ""}-${val.stakeEnd || ""} 有${
          //     val.defectTypeName
          //   }`,
          // ],
        };
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    // /**
    //  * @description: 添加养护内容
    //  */
    // handleAddContent() {
    //   this.editForm.taskDescription.push("");
    // },

    // /**
    //  * @description: 删除养护内容
    //  */
    // handleDeleteContent(index) {
    //   this.editForm.taskDescription.splice(index, 1);
    // },

    /**
     * @description: 取消
     */
    cancel() {
      this.$emit("cancel");
    },

    /**
     * @description: 提交表单
     */
    submitForm() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          addMaintenanceNotice(this.editForm).then(({ code, data }) => {
            if (code === 200) {
              this.$message.success("生成成功");
              this.$emit("cancel");
            } else {
              this.$message.error(data);
            }
          });
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.edit-dialog {
  width: 100%;
  height: 100%;
}

.editForm {
  .el-select {
    width: 100%;
  }

  .content-box {
    display: flex;

    span {
      font-size: 1.5rem;
      line-height: 3rem;
    }

    .el-icon-circle-plus-outline {
      color: #08ce29;
    }

    .el-icon-delete {
      color: #f01515;
    }

    > div {
      margin: 0.5rem 0;
    }
  }
}

/* 页脚 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
