<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      width="40%"
    >
      <el-form
        ref="formName"
        :rules="rules"
        label-width="150px"
        :model="form"
        :disabled="disabled"
      >
        <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="voltageId">
          <el-select
            clearable
            v-model.trim="form.voltageId"
            placeholder="请选择所属母线"
          >
            <el-option
              v-for="item in voltageIdList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电压等级:">
          <el-select
            clearable
            v-model.trim="form.scale"
            placeholder="请选择电压等级"
          >
            <el-option
              v-for="item in scaleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </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="code">
          <el-input
            clearable
            v-model.trim="form.code"
            placeholder="请输入监测点编号"
          ></el-input>
        </el-form-item>

        <el-form-item label="监测对象编号/名称:" prop="objName">
          <el-input
            clearable
            v-model.trim="form.objName"
            placeholder="请输入监测对象编号/名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="监测对象大类:" prop="smallName">
          <el-input
            clearable
            v-model.trim="form.smallName"
            placeholder="请输入监测对象大类"
          ></el-input>
        </el-form-item>
        <el-form-item label="监测对象小类:" prop="bigName">
          <el-input
            clearable
            v-model.trim="form.bigName"
            placeholder="请输入监测对象小类"
          ></el-input>
        </el-form-item>

        <el-form-item label="所属变压器编号:" prop="tfCode">
          <el-input
            clearable
            v-model.trim="form.tfCode"
            placeholder="请输入所属变压器编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="最小短路容量(MVA):" prop="shortCapacity">
          <el-input
            clearable
            v-model.trim="form.shortCapacity"
            placeholder="请输入最小短路容量"
          >
          </el-input>
        </el-form-item>

        <el-form-item label="电压互感器类型:" prop="tfType">
          <el-select
            clearable
            v-model.trim="form.tfType"
            placeholder="请选择电压互感器类型"
          >
            <el-option
              v-for="item in tfTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="中性点接地方式:" prop="groundType">
          <el-select
            clearable
            v-model.trim="form.groundType"
            placeholder="请选择中性点接地方式"
          >
            <el-option
              v-for="item in groundTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="监测终端接线方式:" prop="ptType">
          <el-select
            clearable
            v-model.trim="form.ptType"
            placeholder="请选择监测终端接线方式"
          >
            <el-option
              v-for="item in ptTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供电设备容量(MVA):" prop="devCapacity">
          <el-input
            clearable
            v-model.trim="form.devCapacity"
            placeholder="请输入供电设备容量"
          >
          </el-input>
        </el-form-item>

        <el-form-item label="用户协议容量(MVA):" prop="dealCapacity">
          <el-input
            clearable
            v-model.trim="form.dealCapacity"
            placeholder="请输入用户协议容量"
          >
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="是否应设点:" prop="devId">
          <el-select
            clearable
            v-model.trim="form.whether"
            placeholder="请选择是否应设点"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </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, //是否必须传入
    },
    title: { type: String, required: true },
    treeList: { type: [Array, Object], required: true },

    formInline: { type: [Array, Object], required: true },
  },
  data() {
    return {
      form: {},
      scaleList: [], //电压等级
      devIdList: [], //1所属终端
      voltageIdList: [], //1所属母线
      ptTypeList: [], //监测终端接线方式
      tfTypeList: [], //互感器类型
      groundTypeList: [], //接地方式
      // 表格检验
      rules: {
        devId: [{ required: true, message: "请选择内容", trigger: "change" }],
        voltageId: [
          { required: true, message: "请选择内容", trigger: "change" },
        ],
        name: [{ required: true, message: "请输入内容", trigger: "blur" }],
        code: [{ required: true, message: "请输入内容", trigger: "blur" }],
        objName: [{ required: true, message: "请输入内容", trigger: "blur" }],
        userCode: [{ required: true, message: "请输入内容", trigger: "blur" }],
        tfType: [{ required: true, message: "请选择内容", trigger: "change" }],
        smallName: [{ required: true, message: "请输入内容", trigger: "blur" }],
        bigName: [{ required: true, message: "请输入内容", trigger: "blur" }],
        tfCode: [{ required: true, message: "请输入内容", trigger: "blur" }],
        shortCapacity: [
          { required: true, message: "请输入内容", trigger: "blur" },
        ],
        groundType: [
          { required: true, message: "请选择内容", trigger: "change" },
        ],
        ptType: [{ required: true, message: "请选择内容", trigger: "change" }],
        devCapacity: [
          { required: true, message: "请输入内容", trigger: "blur" },
        ],
        dealCapacity: [
          { required: true, message: "请输入内容", trigger: "blur" },
        ],
      },
      disabled: false,
    };
  },
  created() {
    this.form = JSON.parse(JSON.stringify(this.formInline));
    this.dictypeData();
  },

  methods: {
    // 字典表
    dictypeData() {
      //所属母线
      getAllPvVoltageList().then((res) => {
        this.voltageIdList = res.data;
      });
      //所属终端
      getAllPvDeviceList().then((res) => {
        this.devIdList = res.data;
        this.devIdList.forEach((item) => {
          if (item.typeCode != "Dev_Quality") {
            item.disabled = true;
          }
        });

        //  console.log( this.devIdList);
      });
      // 接线方式
      this.ptTypeList = dicData("Dev_Connect", []);
      // 互感器
      this.tfTypeList = dicData("Voltage_Transformer", []);
      // 中性点接地方式
      this.groundTypeList = dicData("Neutral_Point", []);
      this.scaleList = 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.userCodeType = 3;
            this.form.type = "1a189003c912046f178576615713300e";
            this.form.substationId = 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 {
          // 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>
