<template>
  <div>
    <el-form
      ref="FSUForm"
      :rules="rules"
      :model="FSUForm"
      label-width="200px"
      class="form"
    >
      <el-form-item label="所在铁路线：" prop="wayId">
        <el-select
          v-model="FSUForm.wayId"
          placeholder="请选择铁路线"
          @change="changerailWay()"
        >
          <el-option
            v-for="item in railWaySelect"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在站点：" prop="siteId">
        <el-select v-model="FSUForm.siteId" placeholder="请选择站点">
          <el-option
            v-for="item in stationSelect2"
            :key="item.id"
            :label="item.siteName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="FSU身份编号：" prop="fsuCode">
        <el-input v-model="FSUForm.fsuCode" placeholder="请输入FSU身份编号" />
      </el-form-item>
      <el-form-item label="FSU端口数：" prop="fsuPort">
        <el-input v-model="FSUForm.fsuPort" placeholder="请输入FSU端口数" />
      </el-form-item>
      <el-form-item label="设备名称：" prop="equipName">
        <el-input v-model="FSUForm.equipName" placeholder="请输入设备名称" />
      </el-form-item>
      <el-form-item label="IP地址：" prop="ip">
        <el-input v-model="FSUForm.ip" placeholder="请输入IP地址" />
      </el-form-item>
      <el-form-item label="设备厂商：" prop="equipFactory">
        <el-input v-model="FSUForm.equipFactory" placeholder="请输入设备厂商" />
      </el-form-item>

      <el-form-item label="设备备用方式：" prop="backupMode">
        <el-select
          v-model="FSUForm.backupMode"
          placeholder="请选择设备备用方式"
        >
          <el-option
            v-for="item in backupModeSelect"
            :key="item.id"
            :label="item.dictValue"
            :value="item.dictValue"
          ></el-option
        ></el-select>
      </el-form-item>
      <el-form-item label="通信方式：" prop="connectMode">
        <el-select v-model="FSUForm.connectMode" placeholder="请选择通信方式">
          <el-option
            v-for="item in connectModeSelect"
            :key="item.id"
            :label="item.dictValue"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型：" prop="equipType">
        <el-select v-model="FSUForm.equipType" placeholder="请选择设备类型">
          <el-option
            v-for="item in equipTypeSelect"
            :key="item.id"
            :label="item.dictValue"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备子类型：" prop="equipSubType">
        <el-select
          v-model="FSUForm.equipSubType"
          placeholder="请选择设备子类型"
        >
          <el-option
            v-for="item in equipSubTypeSelect"
            :key="item.id"
            :label="item.dictValue"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="软件版本号：" prop="softVersion">
        <el-input
          v-model="FSUForm.softVersion"
          placeholder="请输入软件版本号"
        />
      </el-form-item>
      <el-form-item label="设备生产序列号：" prop="equipSerialNumber">
        <el-input
          v-model="FSUForm.equipSerialNumber"
          placeholder="请输入设备生产序列号"
        />
      </el-form-item>
      <el-form-item label="设备所在铁路公里标：" prop="kmSign">
        <el-input
          v-model="FSUForm.kmSign"
          placeholder="请输入设备所在铁路公里标"
        />
      </el-form-item>
    </el-form>
    <div class="btn">
      <el-button type="primary" @click="reset">重置</el-button>
      <el-button type="primary" @click="submit">确认提交</el-button>
    </div>
  </div>
</template>
<script>
import { fsusave, selectFsuItem, selectForSite } from "../../api";
import { mapGetters, mapActions } from "vuex";
import { successAlert, warningAlert } from "@/utils/alert";
export default {
  props: [],
  components: {},
  data() {
    return {
      asd: {
        "01": [{ ss: "dasda", fsa: "dasd" }],
        "02": [{ ss2: "dasda", fsasdasd: "dasd" }],
      },
      type: 1,
      railWaySelect: [],
      stationSelect: [],
      stationSelect2: [],
      backupModeSelect: [],
      connectModeSelect: [],
      equipTypeSelect: [],
      equipSubTypeSelect: [],
      FSUForm: {
        backupMode: '',
        connectMode: '',
        equipFactory: "设备厂商1",
        equipName: "设备名称1",
        equipSerialNumber: "4651",
        equipSubType: '',
        equipType: '',
        fsuCode: "212",
        fsuPort: 10,
        ip: "192.168.2.1",
        kmSign: "46511",
        parentId: '',
        siteId: '',
        wayId: '',
        siteName: "站点1",
        softVersion: "6.0.2",
        
      },
      rules: {
        wayId: [{ required: true, message: "请选择铁路线", trigger: "blur" }],
        siteId: [{ required: true, message: "请选择站点", trigger: "blur" }],
        fsuCode: [
          { required: true, message: "请输入FSU身份编号", trigger: "blur" },
        ],
        fsuPort: [
          { required: true, message: "请输入FSU端口数", trigger: "blur" },
        ],
        equipName: [
          { required: true, message: "请输入设备名称", trigger: "blur" },
        ],
        ip: [{ required: true, message: "请输入IP地址", trigger: "blur" }],
        equipFactory: [
          { required: true, message: "请输入设备厂商", trigger: "blur" },
        ],
        backupMode: [
          { required: true, message: "请选择设备备用方式", trigger: "blur" },
        ],
        connectMode: [
          { required: true, message: "请选择通信方式", trigger: "blur" },
        ],
        equipType: [
          { required: true, message: "请选择设备类型", trigger: "blur" },
        ],
        equipSubType: [
          { required: true, message: "请选择设备子类型", trigger: "blur" },
        ],
        softVersion: [
          { required: true, message: "请输入软件版本号", trigger: "blur" },
        ],
        equipSerialNumber: [
          { required: true, message: "请输入设备生产序列号", trigger: "blur" },
        ],
        kmSign: [
          {
            required: true,
            message: "请输入设备所在铁路公里标",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      railWaylist: "railWay/list",
      stationlist: "station/list",
    }),
  },
  methods: {
    ...mapActions({
      asyncrailWayList: "railWay/asyncList",
      asyncstationList: "station/asyncList",
    }),
    changerailWay() {
      console.log(this.FSUForm.parentId);
      this.stationSelect2 = this.stationlist.filter(
        (item) => item.wayId === this.FSUForm.wayId
      );
      console.log(this.stationSelect2);
    },
    readNodes(aaa = [], arrarea = []) {
      for (let item of aaa) {
        arrarea.push({ id: item.id, siteName: item.name });
        if (item.children) {
          this.readNodes(item.children, arrarea);
        }
      }
      return arrarea;
    },
    reset() {
      this.$refs.FSUForm.resetFields();
    },
    submit() {
      this.$refs.FSUForm.validate((valid) => {
        if (valid) {
          this.FSUForm.parentId = this.FSUForm.siteId;
          this.FSUForm.fsuPort = Number(this.FSUForm.fsuPort);
          console.log(this.FSUForm);
          fsusave(this.FSUform).then((res) => {
            if (res.code == 200) {
              successAlert("添加成功");
              console.log(res);
            } else {
              warning("添加失败");
            }
          });
        }
        this.FSUForm = {
          parentId: "",
          wayId: "",
          fsuCode: "",
          fsuPort: "",
          equipName: "",
          ip: "",
          equipFactory: "",
          backupMode: "",
          connectMode: "",
          equipType: "",
          equipSubType: "",
          softVersion: "",
          equipSerialNumber: "",
          kmSign: "",
        };
      });
    },
  },
  created() {
    selectFsuItem().then((res) => {
      this.backupModeSelect = res["01"];
      this.connectModeSelect = res["02"];
      this.equipTypeSelect = res["03"];
      this.equipSubTypeSelect = res["04"];
     
      
    });
  },
  mounted() {
    this.asyncrailWayList();
    this.asyncstationList();
    this.railWaySelect = this.railWaylist;
    this.stationSelect = this.stationlist;
    console.log(this.asd);
  },
};
</script>
<style lang="scss" scoped>
.form {
  padding: 20px 0;
  width: 600px;
  margin: 0 auto;
}
.btn {
  padding-top: 50px;
  text-align: center;
  button {
    width: 120px;
  }
}
</style>