
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog :visible.sync="showDialog" :title="title" :modal-append-to-body="false" style="text-align: left;" @close="closeDialog" :close-on-click-modal="false">
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'140px'">
        <el-row>
          <el-col :span="12">
            <el-form-item label="门店名称" prop="name">
              <el-input v-model="formModel.name" placeholder="请输入门店名称" style="width: 300px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业执照" prop="license">
              <el-input v-model="formModel.license" placeholder="请输入营业执照" style="width: 300px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属区域" prop="areaId">
              <el-select-tree size="mediumn" style="width: 300px" :props="props" :options="queryAreaResult" v-model="formModel.areaId" height="200"></el-select-tree>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="加盟时间" prop="joinTime">
              <el-date-picker v-model="formModel.joinTime" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 300px"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="加盟人" prop="joinName">
              <el-input v-model="formModel.joinName" placeholder="请输入加盟人" style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item label="加盟人电话" prop="joinPhone">
              <el-input v-model="formModel.joinPhone" placeholder="请输入加盟人电话" style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item label="加盟人身份证" prop="joinIdCard">
              <el-input v-model="formModel.joinIdCard" placeholder="请输入加盟人身份证" style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item label="加盟人实名状态" prop="joinIsRealName">
              <el-radio-group v-model="formModel.joinIsRealName">
                <el-radio-button :label="false">未实名</el-radio-button>
                <el-radio-button :label="true">已实名</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管理人" prop="managerName">
              <el-input v-model="formModel.managerName" placeholder="请输入管理人" style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item label="管理人电话" prop="managerPhone">
              <el-input v-model="formModel.managerPhone" placeholder="请输入管理人电话" style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item label="管理人身份证" prop="managerIdCard">
              <el-input v-model="formModel.managerIdCard" placeholder="请输入管理人身份证" style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item label="管理人实名状态" prop="managerIsRealName">
              <el-radio-group v-model="formModel.managerIsRealName">
                <el-radio-button :label="false">未实名</el-radio-button>
                <el-radio-button :label="true">已实名</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import shopApi from "@/api/base/shop";
import areaApi from "@/api/base/area";
import SelectTree from "@/components/SelectTree";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        name: [{ required: true, message: "门店名称不能为空", trigger: "blur"}],
        areaId: [{ required: true, message: "区域编号不能为空", trigger: "blur"}],
        license: [{ required: true, message: "营业执照不能为空", trigger: "blur"}],
        joinName: [{ required: true, message: "加盟人不能为空", trigger: "blur"}],
        joinPhone: [{ required: true, message: "加盟人电话不能为空", trigger: "blur"}],
        joinIdCard: [{ required: true, message: "加盟人身份证不能为空", trigger: "blur"}],
        joinIsRealName: [{ required: true, message: "加盟人实名状态不能为空", trigger: "blur"}],
        joinTime: [{ required: true, message: "加盟时间不能为空", trigger: "blur"}],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      props: {
        value: "id",
        label: "name",
        children: "children"
      },
      queryAreaResult: [],
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate((valid) => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return shopApi.add(self.formModel);
            } else {
              return shopApi.update(self.formModel);
            }
          })().then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success",
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning",
              });

              self.$emit("close", false);
            }
          });
        }
      });
    },
  },
  mounted: function () {
    var self = this;

    var formData = new FormData();
    areaApi.queryList(formData).then(response=>{
      var jsonData = response.data;
      this.queryAreaResult = jsonData;
    });

    (function () {
      if (self.businessKey.length == 0) {
        return shopApi.create();
      } else {
        return shopApi.edit(self.businessKey);
      }
    })()
      .then((response) => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
  components: {
    "el-select-tree": SelectTree,
  },
};
</script>