<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="5vh"
    width="1040px"
    @close="closeModel"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-card>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="100px"
            class="demo-ruleForm"
            v-if="addId === 'add'"
          >
            <div style="padding: 12px 0">
              <el-divider content-position="center">仓库信息</el-divider>
            </div>

            <el-row>
              <el-col :span="12"
                ><el-form-item
                  label="所属公司"
                  prop="supplierNo"
                  style="width: 100%"
                >
                  <search-supplier
                    :value="supplierNo"
                    :placeholder="'请输入公司名称'"
                    @searchChange="getCompanyName"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="仓库名称" prop="name">
                  <el-input
                    placeholder="请输入仓库名称"
                    filterable
                    clearable
                    v-model="ruleForm.name"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="省/市/区" prop="addr_code">
                  <select-area
                    :value="ruleForm.addr_code"
                    @selectChange="areaSelect"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="详细地址" prop="addr">
                  <el-input
                    placeholder="请输入内容"
                    v-model="ruleForm.addr"
                    clearable
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item
                  label="负责人"
                  prop="accountDutyer"
                  style="width: 100%"
                >
                  <search-account
                    :size="searchSize"
                    :code="accountNo"
                    :placeholder="'请输入负责人'"
                    :is-detail="true"
                    :disabled="false"
                    :names="accountNames"
                    @change="accountChange"
                  /> </el-form-item
              ></el-col>
              <el-col :span="12">
                <el-form-item label="负责人电话" prop="dutyer_mobile">
                  <el-input
                    type="number"
                    placeholder="请输入联系人电话"
                    v-model="ruleForm.dutyer_mobile"
                    clearable
                  >
                  </el-input> </el-form-item
              ></el-col>
            </el-row>
            <div style="padding: 0 0 12px 0">
              <el-divider content-position="center">收发货信息</el-divider>
            </div>
            <el-row>
              <el-col :span="12">
                <el-form-item label="联系人" prop="linker">
                  <el-input
                    placeholder="请填写联系人"
                    v-model="ruleForm.linker"
                    clearable
                    @change="linker"
                  >
                  </el-input> </el-form-item
              ></el-col>
              <el-col :span="12">
                <el-form-item label="电话" prop="linker_mobile">
                  <el-input
                    type="number"
                    placeholder="请输入联系人电话"
                    v-model="ruleForm.linker_mobile"
                    clearable
                    @change="linkerMobile"
                  >
                  </el-input> </el-form-item
              ></el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="省/市/区" prop="addrs_code">
                  <select-area
                    :value="ruleForm.addrs_code"
                    @selectChange="selectArea"
                  /> </el-form-item
              ></el-col>
              <el-col :span="12">
                <el-form-item label="详细地址" prop="linker_detail">
                  <el-input
                    placeholder="请输入详细地址"
                    v-model="ruleForm.linker_detail"
                    clearable
                    @change="linkerDetail"
                  >
                  </el-input> </el-form-item
              ></el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button type="primary" @click="submitForm" v-if="!isDetail"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
   <script>
import asyncRequest from "@/apis/service/purchaseIn/storeSet";
import resToken from "@/mixins/resToken";
import {
  isnumber,
  isMobile,
  validEmail,
  isAlphanumeric,
  isChinese,
  isEmoticon,
  validAlphabets,
} from "@/utils/validate";
export default {
  name: "storeSet",
  props: ["showModel", "id", "isDetail", "sitem"],
  mixins: [resToken],
  data() {
    const validatemobile = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("负责人联系方式不能为空！"));
      } else {
        if (!isMobile(value)) {
          callback(new Error("负责人联系方式格式不正确！"));
        } else {
          callback();
        }
      }
    };
    const validatewsmmobile = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("收发货联系方式不能为空！"));
      } else {
        if (!isMobile(value)) {
          callback(new Error("收发货联系方式格式不正确！"));
        } else {
          callback();
        }
      }
    };

    return {
      loading: false,
      title: "添加账号",
      showModelThis: this.showModel,
      ruleForm: {
        name: "", // 仓库名称
        supplierNo: [], //供应商编码
        wsmtype: "", //仓库类型 目前随便填
        addr_code: [], //仓库省市区
        addr: "", //仓库详细地址
        contactor: [], //仓库联系人
        mobile: "", //仓库联系方式
        position: "", //联系人职位  目前随便填
        wsm_type: "", //目前随便填

        wsm_name: "", //收发货联系人
        wsm_mobile: "", //收发货联系方式
        wsm_addr: "", //收发货地址详细
        addrs_code: [], //收发货省市区
      },
      rulesThis: this.rules,
      rules: {
        name: [
          { required: true, message: "请输入仓库名称", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        supplierNo: [
          {
            type: "array",
            required: true,
            message: "请选择供应商公司",
            trigger: "change",
          },
        ],
        addr_code: [
          {
            type: "array",
            required: true,
            message: "请选择仓库所在省/市/区",
            trigger: "change",
          },
        ],
        addr: [
          { required: true, message: "请输入仓库详细地址", trigger: "blur" },
          {
            min: 1,
            max: 100,
            message: "长度在 1 到 100 个字符",
            trigger: "blur",
          },
        ],

        contactor: [
          {
            required: true,
            type: "array",
            message: "请选择仓库负责人",
            trigger: "change",
          },
        ],

        mobile: [
          {
            required: true,
            validator: validatemobile,
            trigger: "blur",
          },
        ],
        wsm_name: [
          { required: true, message: "请输入收发货联系人", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        wsm_mobile: [
          {
            required: true,
            validator: validatewsmmobile,
            trigger: "blur",
          },
        ],
        addrs_code: [
          {
            type: "array",
            required: true,
            message: "请选择收发货所在省/市/区",
            trigger: "change",
          },
        ],
        wsm_addr: [
          { required: true, message: "请输入收发货详细地址", trigger: "blur" },
          {
            min: 1,
            max: 100,
            message: "长度在 1 到 100 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    //供应商公司选择
    searchChange(e) {
      if (e && e.id) {
        this.ruleForm.supplierNo = [e.code]; //供应商编码
      } else {
        this.ruleForm.supplierNo = [];
      }
      this.$refs.ruleForm.validateField("supplierNo");
    },
    async initForm() {
      this.loading = true;
      if (this.id === "add") {
        this.title = "新建仓库";
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        if (this.isDetail) {
          this.title = "账号详情";
          this.rulesThis = {};
        } else {
          this.title = "修改账号";
          this.rulesThis = this.rules;
        }
        await this.resetForm(this.sitem);
        await this.initData();
      }
      this.loading = false;
    },
    async initData() {
      const res = await asyncRequest.detail({ id: this.id });
      if (res && res.code === 0 && res.data) {
        this.ruleForm = res.data;
        this.ruleForm.role_id = this.ruleForm.role;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            name: "", // 仓库名称
            supplierNo: [], //供应商编码
            wsmtype: "", //仓库类型 目前随便填
            addr_code: [], //仓库省市区
            addr: "", //仓库详细地址
            contactor: [], //仓库联系人
            mobile: "", //仓库联系方式
            position: "", //联系人职位  目前随便填
            wsm_type: "", //目前随便填

            wsm_name: "", //收发货联系人
            wsm_mobile: "", //收发货联系方式
            wsm_addr: "", //收发货地址详细
            addrs_code: [], //收发货省市区
          };
        }
      });
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const { username, name, mobile, email, role_id, status } = JSON.parse(
            JSON.stringify(this.ruleForm)
          );
          const model = {
            id: this.id,
            username: username || "", // 账号
            nickname: name || "", // 真实姓名
            mobile: mobile || "",
            email: email || "",
            role: role_id || "",
            status: status || "",
          };
          let res = {};
          if (this.id === "add") {
            delete model["id"];
            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.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

   <style lang="scss" scoped>
.storeSet {
}
</style>
   