<template>
  <basic-container>
    <avue-form :option="option"
               v-model="form"
               ref="form"
               :before-open="beforeOpen"
               :class="frame_class">
    </avue-form>
    <div class="container">
       <span >
          <el-button type="primary"
                     size="small"
                     @click="handleFormSubmit"
                     v-if="addUpdateShowButton && permission.inspectionroute_add">
                     确定</el-button>
         <el-button type="primary"
                    size="small"
                    @click="seachToUpdate"
                    v-if="shouldShowButton && permission.inspectionroute_edit">
                      修改</el-button>
       </span>
    </div>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove, takeEffect, lapse} from "@/api/inspection/inspectionroute";
  import {mapGetters} from "vuex";
  import {getToken} from "@/util/auth";

  export default {
    name: "inspectionrouteDetail",
    data() {
      return {
        addUpdateShowButton:{},
        shouldShowButton:{},
        readonlyForm:{},
        frame_class: '',
        form: {},
        query: {},
        option: {
          submitBtn: false,
          emptyBtn: false,
          column: [
            {
              label: "路线编号",
              prop: "routeNo",
              disabled: true,
              span: 8,
            },
            {
              label: "路线名称",
              prop: "routeName",
              span: 8,
              rules: [{
                required: true,
                message: "请输入路线名称",
                trigger: "blur"
              }]
            },
            {
              label: "类型",
              prop: "type",
              span: 8,
              rules: [{
                required: true,
                message: "请输入类型",
                trigger: "blur"
              }]
            },
            {
              label: "状态",
              prop: "istatus",
              span: 8,
              type: "select",
              dicUrl: "/api/daf-system/dict/dictionary?code=istatus",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入状态",
                trigger: "blur"
              }]
            },
            {
              label: "维护工厂",
              prop: "factory",
              span: 8,
              rules: [{
                required: true,
                message: "请输入维护工厂",
                trigger: "blur"
              }]
            },
            {
              label: "取消原因",
              prop: "cancelReason",
              span: 8,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
          // this.option.column = this.option.column.filter(v => {
          //     return this.permission['inspectionroute_col_' + v.prop]
          // })
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn: false
        };
      },
    },
    created () {
      if(["view"].includes(this.$route.query.frameMode)){
        this.readonlyForm = true;
        this.addUpdateShowButton = false;
        this.shouldShowButton=true;
        this.frame_class = 'frame_class';
      }else{
        const routeNo = this.findObject(this.option.column, "routeNo");
        routeNo.value = ' ';
        routeNo.disabled = true;
        const iStatus = this.findObject(this.option.column, "istatus");
        iStatus.display = false;
        const cancelReason = this.findObject(this.option.column, "cancelReason");
        cancelReason.display = false;
        this.readonlyForm = false;
        this.addUpdateShowButton = true;
        this.shouldShowButton=false;
        this.frame_class = null;
      }
      this.SearchDetail();
    },
    methods: {
      handleFormSubmit() {
        this.$refs.form.validate((valid, done, msg) => {
          if (valid) {
            done()
            this.Submit();
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },
      // 处理表单提交
      Submit() {
        if(this.$route.query.frameMode=="add"){
          this.$confirm("是否新增所填数据?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            add(this.form).then(()=>{
              this.$router.push({
                path: "/inspection/inspectionroute",
                query: {
                },
              });
              this.$message({
                type: "success",
                message: "操作成功!"
              });
            }).catch((error) => {
              this.$message({
                type: "error",
                message: "操作失败!"
              });
            });
          });
        }else {
          this.$confirm("是否修改所填数据?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            update(this.form).then(() => {
              this.$router.push({
                path: "/inspection/inspectionroute",
                query: {},
              });
              this.$message({
                type: "success",
                message: "操作成功!"
              });
            }).catch((error) => {
              this.$message({
                type: "error",
                message: "操作失败!"
              });
            });
          });
        }
      },
      seachToUpdate () {
        this.shouldShowButton=false;
        this.addUpdateShowButton=true;
        this.readonlyForm=false;
        this.frame_class = null;
      },
      SearchDetail() {
        if (["view"].includes(this.$route.query.frameMode)) {
          getDetail(this.$route.query.id).then(res => {
            this.form = res.data.data;
          });
        }
      },
    }
  };
</script>

<style>
.frame_class input.el-input__inner {
  border: none;
  box-shadow: none;
  outline: none;
  pointer-events: none;
}
.frame_class .el-input__icon {
  display: none;
}
.frame_class .el-input--prefix .el-input__inner {
  padding-left: 15px;
}
.frame_class input::placeholder {
  color: transparent;
  display: none; /* 默认隐藏 */
}
.container {
  display: flex;
  justify-content: flex-end; /* 将子元素推到容器的末端 */
}
</style>
