<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-form
        ref="formName"
        :rules="rules"
        label-width="134px"
        :model="form"
        :disabled="disabled"
      >
        <!-- <el-form-item label="所属10kV线路:" prop="line">
          <el-input
            clearable
            v-model.trim="form.line"
            placeholder="请输入所属10kV线路"
          ></el-input>
        </el-form-item> -->
        <!-- <el-form-item label="所属台区:" prop="line">
          <el-select
            clearable
            v-model.trim="form.region"
            placeholder="请选择所属台区"
          >
            <el-option
              v-for="item in options"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="用户所属类型:" prop="userCode">
          <el-select
            clearable
            v-model.trim="form.userCodeType"
            placeholder="请选择用户所属类型"
          >
            <el-option
              v-for="item in userCodeTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户编号:" prop="userCode">
          <el-input
            clearable
            v-model.trim="form.userCode"
            placeholder="请输入用户编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属终端:" prop="devId">
          <el-select
            clearable
            v-model.trim="form.devId"
            placeholder="请选择所属终端"
          >
            <el-option
              v-for="item in devIdList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="监测点编号:" prop="code">
          <el-input
            clearable
            v-model.trim="form.code"
            placeholder="请输入监测点编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="监测点名称:" prop="name">
          <el-input
            clearable
            v-model.trim="form.name"
            placeholder="请输入监测点名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="电压等级:" prop="scale">
          <el-select
            clearable
            v-model.trim="form.scale"
            placeholder="请选择电压等级"
          >
            <el-option
              v-for="item in VoltageList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="监测点状态:" prop="lineStatus">
          <el-select
            clearable
            v-model.trim="form.lineStatus"
            placeholder="请选择监测点状态"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="接入日期:" prop="access">
          <el-date-picker
            clearable
            v-model.trim="form.access"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择接入日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="投运日期:" prop="putIn">
          <el-date-picker
            clearable
            v-model.trim="form.putIn"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="投运日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="统计间隔(分钟):" prop="timeInterval">
          <el-input
            clearable
            v-model.trim="form.timeInterval"
            placeholder="请输入统计间隔(分钟)"
          ></el-input>
        </el-form-item>

        <!-- <el-form-item label="限值:" prop="LimitValue">
          <el-input
            clearable
            v-model.trim="form.LimitValue"
            placeholder="限值"
          ></el-input>
        </el-form-item>
        <el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAllPvVoltageList,
  getAllPvDeviceList,
  addLineDetail,
  updateLineDetail,
} from "@/api/accountmanagement";
import { dicData } from "@/assets/commjs/dictypeData";

export default {
  components: {},
  props: {
    dialogVisible: {
      type: Boolean, //类型
      required: true, //是否必须传入
    },
    treeList: { type: [Array, Object], required: true },
    // subAreaId: { type: [Array, Object], required: true },

    title: { type: String, required: true },
    formInline: { type: [Array, Object], required: true },
  },
  data() {
    return {
      form: {},
      VoltageList: [], //电压等级数组
      userCodeTypeList: [
        {
          id: "0",
          name: "分布式光伏用户",
        },
        {
          id: "1",
          name: "低压用户用户",
        },
      ],
      options: [
        {
          id: "1",
          name: "正常",
        },
        {
          id: "0",
          name: "中断",
        },
      ],

      devIdList: [], //1所属终端
      // 表格检验
      rules: {
        userCode: [{ required: true, message: "请输入内容", trigger: "blur" }],
        devId: [{ required: true, message: "请选择内容", trigger: "change" }],
        userCodeType: [
          { required: true, message: "请选择内容", trigger: "change" },
        ],

        line: [{ required: true, message: "请输入内容", trigger: "blur" }],
        code: [{ required: true, message: "请输入内容", trigger: "blur" }],
        name: [{ required: true, message: "请输入内容", trigger: "blur" }],
        scale: [{ required: true, message: "请选择内容", trigger: "change" }],
        lineStatus: [
          { required: true, message: "请选择内容", trigger: "change" },
        ],
        access: [{ required: true, message: "请输入内容", trigger: "blur" }],
        putIn: [{ required: true, message: "请输入内容", trigger: "blur" }],
        timeInterval: [
          { required: true, message: "请输入内容", trigger: "blur" },
        ],
        LimitValue: [
          { required: true, message: "请输入内容", trigger: "blur" },
        ],
      },
      disabled: false,
    };
  },
  created() {
    this.dictypeData();
    this.form = JSON.parse(JSON.stringify(this.formInline));
  },

  methods: {
    // 字典表
    dictypeData() {
      //所属终端
      getAllPvDeviceList().then((res) => {
        this.devIdList = res.data;
        this.devIdList.forEach((item) => {
          if (item.typeCode == "Dev_Quality") {
            item.disabled = true;
          }
        });
      });
      this.VoltageList = dicData("Dev_Voltage", []);
    },
    // 确认按钮
    confirm() {
      if (this.title.substr(0, 2) == "详情") {
        this.handleClose();
        return;
      }
      this.$refs["formName"].validate((valid) => {
        if (valid) {
          if (this.title.substr(0, 2) == "新增") {
            this.form.type = "ca389ad654ec4d95a4089aedf47124fd";

            this.form.subAreaId = this.treeList.id;

            addLineDetail(this.form).then((res) => {
              if (res.code == `A0000`) {
                this.$message({
                  message: res.message,
                  type: "success",
                });
                this.handleClose();
              }
            });
          } else if (this.title.substr(0, 2) == "修改") {
            updateLineDetail(this.form).then((res) => {
              if (res.code == `A0000`) {
                this.$message({
                  message: res.message,
                  type: "success",
                });
                this.handleClose();
              }
            });
          }
          // } else if (this.title.substr(0, 2) == "修改") {
          // }
        } else {
          // this.$message({
          //   message: "警告哦，这是一条警告消息",
          //   type: "warning"
          // });
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.$emit("monitorClose");
    },
  },

  computed: {},

  watch: {},
};
</script>
<style lang="less" scoped>
::v-deep .el-dialog {
  margin-top: 0px !important;
}
</style>
