<template>
  <el-dialog
    :title="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" @input="aa"></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 :label="1" @click.native="toDisable2">启用</el-radio>-->
<!--            <el-radio :label="0" @click.native="toDisable">禁用</el-radio>-->
          </el-radio-group>
        </el-form-item>
        <el-form-item label="仓库管理员" prop="admin_id" >
          <el-select v-model="ruleForm.admin_id" @select="handleSelect" @change="changeadmin">
            <el-option
              v-for="item in adminnamelist"
              :key="item.user_id"
              :label="item.user_name"
              :value="item.user_id"
            ></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,getusername,updatestatus,getcreaterid } from "@/api/warehouse";
import login from '../../../../mock/login'
export default {
  data() {
    return {
      title: "",
      show_type: 1,
      storehouse_id: "",
      storehouse_code:"coco",
      dialogVisible: false,
      labelPosition:'left',
      adminnamelist: [],
      ruleForm: {
        admin_name:"",//管理员名字
        admin_id:"",//管理员id
        storehouse_name: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        status: 1,
        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" }],
        admin_id:[{
          required:true,message:"管理员不能为空",trigger: "change"
        }],
      },
      type:"",
      // storehouse:{
      //   storehouse_code:"",
      // },
    };
  },
  methods: {

    aa(){
      console.log('op')
    },
    changeadmin(item) {
    },
    handleSelect() {

    },
    usernamelist(){
      //获取管理员列表
      getusername().then((res) => {
        if (res.data.state == 1){
          this.adminnamelist = res.data.data.list;
          // console.log("adminnamelist",this.adminnamelist)
        }
      })
      .catch((err) => {
        console.log(err)
      })
    },
    show(data, type) {
      if (type == 1) {
        this.initstorehouseData();
        this.show_type = 1;
        this.title = "新增仓库";
        this.dialogVisible = true;
        this.usernamelist()
        this.getcreater()
      } else if (type == 2) {
        this.show_type = 2;
        this.storehouse_id = data.ID;
        this.title = "编辑仓库";
        this.storehouse_code = data.StorehouseCode;
        this.ruleForm.address = data.StorehouseAddress;
        this.ruleForm.status = data.StorehouseStatus;
        this.ruleForm.storehouse_name = data.StorehouseName;
        this.ruleForm.admin_name = data.StorehouseAdminName;
        this.ruleForm.admin_id = data.StorehouseAdminId;
        this.dialogVisible = true;
        this.usernamelist()
      }
    },
    //获取当前登录人的id
    getcreater(){
      getcreaterid().then((res) => {
        if (res.data.state == 1){
          this.ruleForm.admin_id = res.data.data.list;
        }
      })
    },
    // 获取仓库编码接口
    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.admin_name = "";
      this.ruleForm.admin_id = "";
      this.ruleForm.storehouse_name = "";
      this.ruleForm.date1 = "";
      this.ruleForm.date2 = "";
      this.ruleForm.type = [];
      this.ruleForm.status = 1;
      this.ruleForm.address = "";
    },

    //  验证表单内容
    submitForm(ruleForm) {
      if (this.show_type == 1) {
        this.$refs[ruleForm].validate((valid) => {
          if (valid) {
            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.admin_id || "",
            };
            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);
                this.dialogVisible = true
              }
            });
          } else {
            // return false;
          }
        });
      } else if (this.show_type == 2) {
        this.$refs[ruleForm].validate((valid) => {
          if (valid) {
            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.admin_id || "",
            };
            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);
                this.dialogVisible = true
              }
            });
          } 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>
