<template>
  <div>
    <el-form
      ref="monitorForm"
      :model="monitorForm"
      :rules="rules"
      label-width="200px"
      class="form"
    >
      <el-form-item label="所在铁路线：" prop="wayId">
        <el-select
          v-model="monitorForm.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="monitorForm.siteId"
          placeholder="请选择站点"
          @change="changesite()"
        >
          <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="fsuId">
        <el-select v-model="monitorForm.fsuId" placeholder="请选择FSU">
          <el-option
            v-for="item in fsuSelect2"
            :key="item.id"
            :label="item.equipName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备身份编号：" prop="equipCode">
        <el-input
          v-model="monitorForm.equipCode"
          placeholder="请输入设备身份编号"
        />
      </el-form-item>
      <el-form-item label="设备名称：" prop="equipName">
        <el-input
          v-model="monitorForm.equipName"
          placeholder="请输入设备名称"
        />
      </el-form-item>
      <el-form-item label="设备厂商：" prop="equipFactory">
        <el-input
          v-model="monitorForm.equipFactory"
          placeholder="请输入设备厂商"
        />
      </el-form-item>
      <el-form-item label="IP：" prop="ip">
        <el-input
          v-model="monitorForm.ip"
          placeholder="请输入设备厂商"
        />
      </el-form-item>
      <el-form-item label="设备类别：" prop="equipType">
        <el-select v-model="monitorForm.equipType" placeholder="请输入设备类别">
          <el-option
            v-for="item in equipTypeSelect"
            :key="item.id"
            :label="item.dictValue"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备型号：" prop="equipMode">
        <el-select v-model="monitorForm.equipMode" placeholder="请输入设备型号">
          <el-option
            v-for="item in equipModeSelect"
            :key="item.id"
            :label="item.dictValue"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="设备生产序列号：" prop="equipSerialNumber">
        <el-input
          v-model="monitorForm.equipSerialNumber"
          placeholder="请输入设备生产序列号"
        />
      </el-form-item>
      <el-form-item label="设备所在铁路公里标：" prop="kmSign">
        <el-input
          v-model="monitorForm.kmSign"
          placeholder="请输入设备所在铁路公里标"
        />
      </el-form-item>
      <el-form-item label="安装日期：" prop="installDate">
       
         <el-date-picker
      v-model="monitorForm.installDate"
      type="datetime"
      placeholder="请选择安装日期"
      format="yyyy-MM-dd HH:mm:ss"
      style="width:400px"
      value-format="yyyy-MM-dd HH:mm:ss">
    </el-date-picker>
      </el-form-item>

      <el-form-item label="	维护日期：" prop="repairDate">
        <el-date-picker
      v-model="monitorForm.repairDate"
      type="datetime"
      placeholder="请选择维护日期"
      format="yyyy-MM-dd HH:mm:ss"
      style="width:400px"
      value-format="yyyy-MM-dd HH:mm:ss">
    </el-date-picker>
      </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 { monitorEquipsave, selectMonitorItem } from "../../api";
import { mapGetters, mapActions } from "vuex";
import { successAlert, warningAlert } from "@/utils/alert";
export default {
  props: [],
  components: {},
  data() {
    return {
      railWaySelect: [],
      stationSelect: [],
      stationSelect2: [],
      fsuSelect: [],
      fsuSelect2: [],
      FSUrailway: [
        {
          key: 6,
          label: "张呼客运专线",
        },
        {
          key: 12,
          label: "南昆客运专线",
        },
      ],
      equipTypeSelect: [],
      equipModeSelect: [],
      monitorForm: {
        wayId: "",
        parentId: "",
        siteId: "",
        fsuId: "",
        ip:"",
        equipCode: "",
        equipFactory: "",
        equipMode: "",
        equipName: "",
        equipSerialNumber: "",
        equipType: "",
        installDate: "",
        kmSign: "",

        repairDate: "",
      },
      rules: {
        parentId: [
          { required: true, message: "请选择铁路线", trigger: "blur" },
        ],
        equipCode: [
          { required: true, message: "请输入设备身份编号", trigger: "blur" },
        ],
        equipFactory: [
          { required: true, message: "请输入设备厂商", trigger: "blur" },
        ],
          ip: [
          { required: true, message: "请输入IP", trigger: "blur" },
        ],
        equipMode: [
          { required: true, message: "请选择设备型号", trigger: "blur" },
        ],
        equipName: [
          { required: true, message: "请输入设备名", trigger: "blur" },
        ],
        equipSerialNumber: [
          { required: true, message: "请输入设备生产序列号", trigger: "blur" },
        ],
        equipType: [
          { required: true, message: "请输入设备类别", trigger: "blur" },
        ],
        installDate: [
          { required: true, message: "请输入安装日期", trigger: "blur" },
        ],
        kmSign: [
          {
            required: true,
            message: "请输入设备所在铁路公里标",
            trigger: "blur",
          },
        ],
        repairDate: [
          { required: true, message: "请输入维护日期", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      railWaylist: "railWay/list",
      stationlist: "station/list",
      fsulist: "FSU/list",
    }),
  },
  methods: {
    ...mapActions({
      asyncrailWayList: "railWay/asyncList",
      asyncstationList: "station/asyncList",
      asyncfsuList: "FSU/asyncList",
    }),
    changerailWay() {
      
      this.stationSelect2 = this.stationlist.filter(
        (item) => item.parentId === this.monitorForm.wayId
      );
    },
    changesite() {
      this.fsuSelect2 = this.fsulist.filter(
        (item) => item.parentId === this.monitorForm.siteId
      );
    },
    reset() {
      
      this.$refs.monitorForm.resetFields();
    },
    submit() {
      this.$refs.monitorForm.validate((valid) => {
        if (valid) {
          this.monitorForm.parentId = this.monitorForm.fsuId;
        
          monitorEquipsave(this.monitorForm).then((res) => {
            if (res.code == 200) {
              successAlert("添加成功");
            } else {
              warning("添加失败");
            }
          });
        }
        this.monitorForm = {
          wayId: "",
          parentId: "",
          siteId: "",
          fsuId: "",
          equipCode: "",
          ip:"",
          equipFactory: "",
          equipMode: "",
          equipName: "",
          equipSerialNumber: "",
          equipType: "",
          installDate: "",
          kmSign: "",

          repairDate: "",
        };
      });
    },
  
     
  },
  created() {
    selectMonitorItem().then((res) => {
      this.equipTypeSelect = res["03"];
      this.equipModeSelect = res["09"];
    });
  },
  mounted() {
    this.asyncrailWayList();
    this.asyncstationList();
    this.asyncfsuList();
    this.railWaySelect = this.railWaylist;
    this.stationSelect = this.stationlist;
    this.fsuSelect = this.fsulist;
  },
};
</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>