<template>
  <el-dialog
    title="新增仓库信息"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="closePop"
  >
    <div class="main_contain">
      <el-form
        :model="ruleForm"
        :rules="rules"
        :label-position="labelPosition"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="仓库编号">
          <span>{{storehouse_code}}</span>
        </el-form-item>
        <el-form-item label="仓库名称" prop="storehouse_name">
          <el-input v-model="ruleForm.storehouse_name"></el-input>
        </el-form-item>
        <el-form-item label="仓库地址" prop="address">
          <el-input type="textarea" v-model="ruleForm.address"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1" >启用</el-radio>
            <el-radio :label="0" >禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="仓库管理员" ><!--prop="region">-->
          admin
<!--          <el-select v-model="ruleForm.region" placeholder="请选择管理员">-->
<!--            <el-option label="admin" value="0"></el-option>-->
<!--          </el-select>-->
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closePop">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getcode,addstorehouse,updatestorehouse } from "@/api/warehouse";
import login from '../../../../mock/login'
export default {
  data() {
    return {
      show_type: 1,
      storehouse_id: "",
      storehouse_code:"coco",
      dialogVisible: false,
      labelPosition:'left',
      ruleForm: {
        admin_name:"",
        storehouse_name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        status: "",
        address: "",
      },
      //表单验证规则
      rules: {
        storehouse_name:[{
          required:true,message:"仓库名称不能为空",trigger: "change"
        }],
        address:[{
          required:true,message:"仓库地址不能为空",trigger: "change"
        }],
        // region: [{ required: true, message: "请选择活动区域", trigger: "change" }],
        type: [{
            type: "array",
            required: true,
            message: "请至少选择一个活动性质",
            trigger: "change",
          },],
        desc: [{ required: true, message: "请填写活动形式", trigger: "blur" }],
      },
      type:"",
      // storehouse:{
      //   storehouse_code:"",
      // },
    };
  },
  methods: {
    show(data, type) {
      if (type == 1) {
        this.initstorehouseData();
        this.show_type = 1;
        this.title = "新增仓库";
        this.dialogVisible = true;
      } else if (type == 2) {
        console.log("data:::::::::::",data);
        this.show_type = 2;
        this.storehouse_id = data.id;
        this.title = "编辑仓库";
        this.storehouse_code = data.storehouse_code;
        this.ruleForm.address = data.storehouse_address;
        this.ruleForm.status = data.storehouse_status;
        this.ruleForm.storehouse_name = data.storehouse_name;
        this.ruleForm.admin_name = data.storehouse_admin_name;
        console.log("this.ruleForm.admin_name",this.ruleForm.admin_name);
        this.dialogVisible = true;
      }
    },
    // 获取仓库编码接口
    initstorehouseData() {
      getcode().then((res) => {
        if (res.data.state == 1) {
          this.storehouse_code =
            res.data.data.list;
        }
      });
    },
    //清除表单数据
    closePop() {
      console.log("this.ruleForm",this.ruleForm)
      this.dialogVisible = false;
      this.ruleForm.storehouse_name = "";
      this.ruleForm.region = "";
      this.ruleForm.date1 = "";
      this.ruleForm.date2 = "";
      this.ruleForm.type = [];
      this.ruleForm.status = "";
      this.ruleForm.address = "";
    },


    //  验证表单内容
    submitForm(ruleForm) {
      if (this.show_type == 1) {
        this.$refs[ruleForm].validate((valid) => {
          if (valid) {
            console.log("this.ruleForm.status",this.ruleForm.status);
            let params = {
              storehouse_code: this.storehouse_code || "",
              storehouse_name: this.ruleForm.storehouse_name || "",
              storehouse_address: this.ruleForm.address || "",
              storehouse_status:parseInt(this.ruleForm.status)
              // storehouse_admin_id: this.ruleForm.storehouse_admin_id || "",
              // storehouse_admin_name: this.ruleForm.storehouse_admin_name || "",
            };
            console.log("params::::",params)
            console.log("status::::",this.ruleForm.status)
            addstorehouse(params).then((res) => {
              if (res.data.state == 1) {
                this.$message.success("新增成功");
                this.closePop();
                this.$emit("init");
              } else {
                this.$message.error("新增失败：" + res.data.msg);
                setTimeout(() => {
                  this.closePop();
                }, 2000);
              }
            });
          } else {
            return false;
          }
        });
      } else if (this.show_type == 2) {
        this.$refs[ruleForm].validate((valid) => {
          if (valid) {
            console.log("this.ruleForm.status",this.ruleForm.status);
            let params = {
              storehouse_code: this.storehouse_code || "",
              storehouse_name: this.ruleForm.storehouse_name || "",
              storehouse_address: this.ruleForm.address || "",
              storehouse_status:parseInt(this.ruleForm.status),
              id: this.storehouse_id
              // storehouse_admin_id: this.ruleForm.storehouse_admin_id || "",
              // storehouse_admin_name: this.ruleForm.storehouse_admin_name || "",
            };
            updatestorehouse(params).then((res) => {
              if (res.data.state == 1) {
                this.$message.success("保存成功");
                this.closePop();
                this.$emit("init");
              } else {
                this.$message.error("保存失败：" + res.data.msg);
                setTimeout(() => {
                  this.closePop();
                }, 2000);
              }
            });
          } else {
            return false;
          }
        });
      }
      this.contactsinfo();
    },




  },
};
</script>

<style lang="scss" scoped>
.main_contain {
  width: 60%;
  display: flex;
  flex-direction: column;
  margin: 0 auto;
  .main_list {
    height: 30px;
    display: flex;
    line-height: 30px;
  }
}
</style>
