<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="12vh"
    width="800px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="closeModel"
  >
    <el-card style="margin-top: -25px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="110px"
            class="demo-ruleForm"
          >
            <el-form-item label="企业名称" prop="testName">
              <exSelect :type="isDetail" :dataList="test" :seleValue="ruleForm.testName" @selectFn = "selectFn" />
              
            </el-form-item>
            <!-- <el-form-item label="企业名称" prop="testName">
              <el-input
                v-model="ruleForm.testName"
                placeholder="请输入企业名称"
                :disabled="isDetail === '007'"
              />
            </el-form-item> -->
            <el-form-item label="联系人" prop="nickname">
              <el-input
                v-model="ruleForm.nickname"
                placeholder="请输入联系人"
                :disabled="isDetail === '007'"
              />
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
              <el-input
                v-model="ruleForm.mobile"
                placeholder="请输入手机号手机号"
                :disabled="isDetail === '007'"
              />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="ruleForm.remark"
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4 }"
                placeholder="请输入内容"
                :disabled="isDetail === '007'"
                maxlength="250"
                show-word-limit
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button
            v-if="isDetail !== '007'"
            type="primary"
            @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            isDetail !== "007" ? "取 消" : "关 闭"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/superTube/batchAccount";
import resToken from "@/mixins/resToken";
import PeriodDateTimePicker from "@/components/PeriodDateTimePicker";
import exSelect from "@/components/global/exSelect";
import {
  isnumber,
  isMobile,
  isChinese,
  isEmoticon,
  validAlphabets,
} from "@/utils/validate";
export default {
  name: "batchAccount",
  props: ["showModel", "id", "isDetail", "sitem"],
  mixins: [resToken],
  components: {
    PeriodDateTimePicker,
    exSelect,
  },
  data() {
    const validatename = (rule, value, callback) => {
      if ((value ?? "") != "") {
        callback();
      } else {
        callback(new Error("联系人不能为空！"));
      }
    };
    const validatetestName = (rule, value, callback) => {
      if ((value ?? "") != "") {
        callback();
      } else {
        callback(new Error("企业名称不能为空！"));
      }
    };
    const validateremark = (rule, value, callback) => {
      if ((value ?? "") != "") {
        callback();
      } else {
        callback(new Error("备注不能为空！"));
      }
    };
    const validatemobile = (rule, value, callback) => {
      if ((value ?? "") != "") {
        if (!isMobile(value) && value) {
          callback(new Error("手机号格式不正确！"));
        } else {
          callback();
        }
      } else {
        callback(new Error("联系方式不能为空！"));
      }
    };
    return {
      seleValue:"2",
      test: [
        { value: "1", label: "哈哈" },
        { value: "2", label: "AG" },
        { value: "3", label: "嘎嘎" },
      ],

      options: [],
      value: [],
      list: [],
      states: [],
      tags: [],
      roleList: [],
      loading: false,
      title: "添加账号",
      showModelThis: this.showModel,
      parmValue: {
        testName: "",
        nickname: "",
        remark: "",
        mobile: "",
        page: 1,
        size: 15,
      },
      ruleForm: {
        testName: "",
        nickname: "",
        mobile: "",
        remark: "",
      },
      rulesThis: this.rules,
      rules: {
        testName: [
          {
            required: true,
            validator: validatetestName,
            // trigger: "change",
            trigger: "blur",

          },
        ],
        nickname: [
          {
            required: true,
            validator: validatename,
            trigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            validator: validatemobile,
            trigger: "blur",
          },
        ],
        remark: [
          {
            required: true,
            validator: validateremark,
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },

  methods: {
    selectFn(val){
      this.ruleForm.testName = val;
      this.$refs.ruleForm.validateField("testName");
      console.log(this.ruleForm.testName)
    },
    restSearch() {
      this.select = "1";
      this.input = "";
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        testName: "",
        nickname: "",
        remark: "",
        mobile: "",
        page: 1,
        size: 15,
      };
    },

    closeModel() {},
    async initForm() {
      this.loading = true;
      if (this.isDetail === "003") {
        this.title = "新建企业";
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        if (this.isDetail === "007") {
          this.title = "企业详情";
          this.rulesThis = {};
        } else {
          this.title = "编辑企业";
          this.rulesThis = this.rules;
        }
        await this.resetForm();
        await this.initData();
      }
      this.loading = false;
    },
    async initData() {
      const res = await asyncRequest.detail({ id: this.id });
      if (res && res.code === 0 && res.data) {
        const { testName, nickname, mobile, remark } = res.data;

        this.ruleForm = {
          testName,
          nickname,
          mobile,
          remark,
        };
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      }
    },
    async resetForm() {
      this.tags = [];
      this.options = [];
      this.value = [];
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          // this.$refs.videoSet.$data.selectedLabel = "";
          // this.$refs.ruleForm.resetFields();
          // this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            testName: "",

            nickname: "",
            mobile: "",
            remark: "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;

          let res = {};
          if (this.id === "add") {
            res = await asyncRequest.add(model);
          } else {
            res = await asyncRequest.update(model);
          }
          this.loading = false;
          if (res && res.code === 0) {
            const title = this.id === "add" ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.msg);
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

