<template>
  <div class="shopAdd">
    <el-dialog width="600px" :title="title" destroy-on-close center :visible.sync="showVisible">
      <el-form :model="form">
        <el-form-item label="门店名称" label-width="103px">
          <el-input v-model="form.name" placeholder="请输入店铺名称" autocomplete="off" style="width: 100%"></el-input>
        </el-form-item>
        <el-form-item label="主营类别" label-width="103px">
          <div class="category_box">
            <div class="item" :class="form.categoryId == item.id ? 'active':''" v-for="(item,i) in products" :key="i" @click="$set(form,'categoryId',item.id)">
              {{item.name}}
            </div>
          </div>
        </el-form-item>
        <el-form-item label="详细地址" label-width="103px">
          <el-input v-model="form.address" placeholder="请点击地图选择具体地址" disabled autocomplete="off" style="width: 100%">
            <el-image @click="showMap" slot="append" class="map" src="../../../static/image/icon_map.png"></el-image>
          </el-input>
        </el-form-item>
        <el-form-item label="门牌号" label-width="103px">
          <el-input v-model="form.street" placeholder="请填写门牌号" autocomplete="off" style="width: 100%">
          </el-input>
        </el-form-item>
        <el-form-item label="联系人姓名" label-width="103px">
          <el-input v-model="form.contactName" placeholder="请填写姓名" autocomplete="off" style="width: 40%"></el-input>
        </el-form-item>
        <el-form-item label="联系人电话" label-width="103px">
          <el-input v-model="form.mobile" placeholder="请填写手机号" autocomplete="off" style="width: 40%"></el-input>
        </el-form-item>
        <el-form-item label="已获运力" label-width="103px" v-if="form.id">
          <div class="logo_box">
            <div v-for="(item,i) in deliveries" :key="i">
              <img v-if="item.bindStatus == 1" :src="item.logo" alt="">
            </div>
          </div>
          <div class="line"></div>
        </el-form-item>
        <el-form-item label="审核中运力" label-width="103px" v-if="form.id && revieweds.length">
          <div class="reviewed">
            <div class="item" v-for="(item, i) in revieweds" :key="i">
              <div class="left-wrap">
                <div class="left">
                  <img class="logo" :src="item.logo">
                  <span class="label">{{item.name}}</span>
                </div>
                <div class="tip">{{showStatus(item.bindStatus)}}</div>
              </div>
              <div v-show="item.authMsg" class="reason">原因：{{item.authMsg}}</div>
            </div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" class="btn" @click="save">保存</el-button>
      </div>
    </el-dialog>
    <!-- 通过地图选择地理位置 -->
    <order-map @getAddressDetail="getAddressDetail" ref="orderMap"></order-map>
  </div>
</template>

<script>
import orderMap from "../orderComponents/orderAMap.vue";
import { getShopDetail, shopAdd, shopEdit } from "../../api/shop.js";
export default {
  props: {
    products: {
      type: Array,
      default: function () {
        return [];
      },
    },
  },
  data() {
    return {
      title: "新增门店",
      showVisible: false,
      form: {
        id: "",
        address: "",
        cityCode: "",
        cityName: "",
        contactName: "",
        districtName: "",
        lat: "",
        lng: "",
        mobile: "",
        name: "",
        street: "",
        categoryId: "",
      },
      deliveries: [],
      revieweds: [],
      loading: false,
    };
  },
  components: {
    orderMap,
  },
  methods: {
    init(e) {
      if (e) {
        this.title = "编辑门店";
        getShopDetail({ shopId: e.id }).then((res) => {
          console.log(res);
          if (res.code === 200) {
            this.form = res.data;
            this.deliveries = this.form.deliveries.filter((v) => {
              return v.bindStatus === 1 && v.type !== 3;
            });
            this.revieweds = this.form.deliveries.filter((v) => {
              return v.type === 3;
            });
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        });
      } else {
        this.title = "新增门店";
        this.form = {
          address: "",
          contactName: "",
          mobile: "",
          name: "",
          street: "",
          categoryId: "",
        };
      }
      this.showVisible = true;
    },
    showStatus(showStatus) {
      let name = "";
      switch (showStatus) {
        case 0:
          name = "未绑定";
          break;
        case 1:
          name = "已通过";
          break;
        case 2:
          name = "审核中";
          break;
        case 3:
          name = "审核失败";
          break;

        default:
          break;
      }
      return name;
    },
    showMap() {
      this.$refs.orderMap.chooseLocation();
    },
    getAddressDetail(v, lng, lat) {
      this.form.address = v.address;
      this.form.provinceName = v.province;
      this.form.cityName = v.city;
      this.form.cityCode = v.citycode;
      this.form.districtName = v.district;
      this.form.lng = lng;
      this.form.lat = lat;
    },
    save() {
      if (!this.form.name.trim()) {
        return this.$message({
          type: "error",
          message: "请输入门店名称！",
        });
      }
      if (!String(this.form.categoryId).trim()) {
        return this.$message({
          type: "error",
          message: "请选择主营类别！",
        });
      }
      if (!this.form.address.trim()) {
        return this.$message({
          type: "error",
          message: "请选择详细地址！",
        });
      }
      if (!this.form.street.trim()) {
        return this.$message({
          type: "error",
          message: "请输入门牌号！",
        });
      }
      if (!this.form.contactName.trim()) {
        return this.$message({
          type: "error",
          message: "请输入联系人姓名！",
        });
      }
      if (!this.form.mobile.trim()) {
        return this.$message({
          type: "error",
          message: "请输入联系人电话！",
        });
      }
      this.loading = true;
      if (this.form.id) {
        shopEdit(this.form).then((res) => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: `保存成功！`,
            });
            this.$emit("getData");
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
          this.showVisible = false;
          this.loading = false;
        });
      } else {
        shopAdd(this.form).then((res) => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: `新增成功！`,
            });
            this.$emit("getData");
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
          this.showVisible = false;
          this.loading = false;
        });
      }
    },
  },
};
</script>

<style lang="scss">
.shopAdd {
  .map {
    height: 25px;
    width: 25px;
    cursor: pointer;
  }
  .el-dialog {
    border-radius: 16px;
    overflow: hidden;
  }

  .el-dialog__headerbtn .el-dialog__close:hover {
    color: #909399;
  }

  .el-dialog__header {
    /*height: 48px;*/
    background: #f7f7f7;
    padding: 16px 20px;
  }

  .el-dialog__title {
    font-size: 16px;
    font-weight: 500;
    color: #000000;
  }

  .el-form-item__label {
    text-align: left;
    color: #000000;
  }

  .el-input__inner:focus {
    border-color: #fc7b0f;
  }

  .el-input__inner {
    color: #222222;
  }

  .category_box {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    margin-top: -10px;

    .item {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 92px;
      height: 38px;
      margin: 10px 10px 0 0;
      background: #f4f4f4;
      border-radius: 2px;
      cursor: pointer;

      &.active {
        color: #ffffff;
        background: #fc7200;
      }
    }
  }

  .line {
    position: absolute;
    left: -103px;
    right: 0;
    bottom: -12px;
    border: 0.5px dashed #e0e0e0;
  }

  .logo_box {
    width: 100%;
    height: 40px;
    display: flex;
    align-items: center;
    line-height: 0;

    img {
      width: 20px;
      height: 20px;
      border-radius: 50%;
      margin-right: 10px;
      vertical-align: top;
    }
  }

  .reviewed {
    .item {
      .reason {
        font-size: 12px;
        color: crimson;
      }
      .left-wrap {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 40px;
        .left {
          display: flex;
          align-items: center;
          line-height: 0;
          .logo {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            margin-right: 10px;
          }

          .label {
            font-size: 14px;
            color: #000000;
          }
        }
      }
      .tip {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 60px;
        height: 22px;
        border-radius: 22px;
        background: #f74141;
        font-size: 12px;
        color: #ffffff;
      }
    }
  }

  .btn {
    width: 220px;
  }
}
</style>
