<template>
  <div class="app-container">
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="100px"
      :disabled="disabled"
    >
      <el-form-item label="门店名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入门店名称" />
      </el-form-item>
      <el-form-item label="门店图片：" prop="icon">
        <Upload
          list-type="picture-card"
          :url="form.icon"
          @upload="upload"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item label="联系电话：">
        <el-input v-model="form.linkPhone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="城市筛选：" prop="cascader">
        <el-cascader
          style="width: 100%"
          v-model="form.cascader"
          :options="mapOptions"
          placeholder="请选择省市区"
          :props="{
            value: 'id',
            label: 'name',
            children: 'childList',
            checkStrictly: true,
          }"
        />
      </el-form-item>
      <el-form-item label="详细地址：" prop="address">
        <el-input v-model="form.address" placeholder="请输入详细地址" />
      </el-form-item>
      <el-form-item label="经度：" prop="longitude">
        <el-input v-model="form.longitude" placeholder="请输入经度" />
      </el-form-item>
      <el-form-item label="纬度：" prop="latitude">
        <el-input v-model="form.latitude" placeholder="请输入纬度" />
      </el-form-item>
    </el-form>
    <div class="form-btn">
      <el-button @click="cancel">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="getSubmit"
        >确定</el-button
      >
    </div>
  </div>
</template>

<script>
import { detail, submit } from "@/api/operation/map";
import { mapMixin } from "@/mixin/index";
export default {
  mixins: [mapMixin],
  data() {
    return {
      // 表单
      form: {
        icon: "",
        status: 0,
      },
      // 校验
      rules: {
        name: [
          {
            required: true,
            message: "请输入门店名称",
            trigger: "blur",
          },
        ],
        icon: [
          {
            required: true,
            message: "请上传门店图片",
            trigger: "change",
          },
        ],
        cascader: [
          {
            required: true,
            message: "请选择省市区",
            trigger: "change",
          },
        ],
        address: [
          {
            required: true,
            message: "请输入详细地址",
            trigger: "blur",
          },
        ],
        latitude: [
          {
            required: true,
            message: "请输入纬度",
            trigger: "blur",
          },
        ],
        longitude: [
          {
            required: true,
            message: "请输入经度",
            trigger: "blur",
          },
        ],
      },
      // 只读
      disabled: false,
    };
  },
  mounted() {
    if (this.$route.query.id) {
      this.getDetail();
      this.disabled = Boolean(this.$route.query.boolean);
    }
  },
  methods: {
    // 详情
    getDetail() {
      detail(this.$route.query.id).then((res) => {
        if (res.code === 0) {
          this.form = res.data;
          this.$set(this.form, "cascader", [res.data.province]);
          if (res.data.city) {
            this.form.cascader[1] = res.data.city;
          }
          if (res.data.area) {
            this.form.cascader[2] = res.data.area;
          }
        }
      });
    },

    // 上传
    upload(e) {
      this.form.icon = e.file;
    },

    cancel() {
      this.$tab.closeOpenPage("/operation/map");
    },

    // 提交
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.province = this.form.cascader[0];
          this.form.city = this.form.cascader[1] ? this.form.cascader[1] : null;
          this.form.area = this.form.cascader[2] ? this.form.cascader[2] : null;
          submit(this.form).then((res) => {
            if (res.code === 0) {
              this.$message.success("提交成功！");
              this.cancel();
            }
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-form {
  .el-form-item {
    width: 500px;
  }
}

::v-deep .el-dialog__wrapper {
  .el-dialog__body {
    padding: 0;
  }
}
</style>