<template>
  <el-dialog
    class="iccidDialog"
    title="导入ICCID"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    width="420px"
  >
    <el-form
      label-position="left"
      label-width="92px"
      ref="formLineInfo"
      :model="formdata"
    >
      <el-form-item label="网络类型：" prop="netType">
        <el-select v-model="formdata.netType" placeholder="请选择">
          <el-option
            v-for="item in networkOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="ICCID：" prop="iccid">
        <el-input
          type="textarea"
          :rows="10"
          placeholder="请输入iccid"
          v-model="formdata.iccid"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { importiccidApi } from "@/utils/api/index";

export default {
  props: {},

  data() {
    return {
      isShow: false,
      formdata: {},
      networkOptions: [
        {
          id: -1,
          name: "未绑定",
        },
        {
          id: 1,
          name: "大汉移动",
        },
        {
          id: 2,
          name: "公田移动rabchase",
        },
        {
          id: 3,
          name: "公田m2m10086",
        },
        {
          id: 4,
          name: "安徽",
        },
      ], //网络类型
    };
  },
  computed: {
    // 计算属性，将 cmdidArr 字符串转换为数组
    iccidArrList() {
      // 使用 split 方法按换行符分割字符串，并返回数组
      return this.formdata.iccid.split(/\r\n|\n|\r/).filter(Boolean);
    },
  },
  methods: {
    // 保存确定操作
    submitForm() {
      console.log(this.formdata);
      console.log(this.iccidArrList);
      this.$refs.formLineInfo.validate((valid) => {
        if (valid) {
          console.log(this.formdata);
          importiccidApi({
            iccidList: this.iccidArrList,
            type: this.formdata.netType,
          })
            .then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.isShow = false;
                this.$message({
                  duration: 1500,
                  showClose: true,
                  message: "导入成功",
                  type: "success",
                });
              } else {
                this.$message({
                  duration: 1500,
                  showClose: true,
                  message: res.msg,
                  type: "error",
                });
              }
            })
            .catch((err) => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    display() {
      this.isShow = true;
    },
    hide() {
      this.isShow = false;
    },
  },
};
</script>
<style lang="less">
.iccidDialog {
  .el-select {
    width: 100%;
  }
}
</style>
