<style>
  .customerDialogBox.add-box .add-tit {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 20px;
  }

  .customerDialogBox.add-box .add-tit p {
    display: flex;
    align-items: center;
    font-weight: bold;
    color: rgba(17, 17, 17, 1);
    font-size: 14px;
  }

  .customerDialogBox.add-box .add-tit p span {
    display: inline-block;
    width: 6px;
    height: 6px;
    background: #409efe;
    border-radius: 50%;
    margin-right: 10px;
  }

  .customerDialogBox.add-box .el-dialog__header {
    padding: 15px 20px;
    background: rgba(255, 255, 255, 1);
  }

  .customerDialogBox.add-box .dialog-footer {
    text-align: center;
    background-color: rgba(248, 250, 251, 1);
    padding-bottom: 20px;
  }

  .customerDialogBox.add-box .el-dialog__footer {
    padding: 0;
  }

  .customerDialogBox.add-box .el-dialog__body {
    background-color: rgba(248, 250, 251, 1);
    padding-top: 20px;
    max-height: 500px;
    overflow: auto;
  }

  .customerDialogBox .add-box-btn.el-button {
    border-radius: 0;
    background-color: #409efecc;
    border-color: #409efecc;
    color: white;
    padding: 12px 43px;
  }

  .customerDialogBox .add-box-btn.el-button:hover {
    background-color: #409efe;
    border-color: #409efe;
    color: white;
  }

  .customerDialogBox .add-box-btn.el-button.add-box-cancel {
    color: #409efecc;
    background-color: white;
  }

  .customerDialogBox .add-box-btn.el-button.add-box-cancel:hover {
    color: #409efe;
    background-color: white;
  }

  .customerDialogBox .el-input .el-input-group__append {
    background-color: #409efe;
    border-color: #409efe;
    color: #fff;
  }

  .customerDialogBox .form-box .form-box-tit {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
  }

  .customerDialogBox .form-box .radius {
    display: flex;
    width: 4px;
    height: 4px;
    background: rgba(255, 164, 117, 1);
    border-radius: 50%;
    margin-right: 10px;
  }

  .customerDialogBox .up-img-box .uib-item {
    margin-right: 30px;
    width: 120px;
    height: 120px;
    background-color: white;
    position: relative;
    margin-bottom: 30px;
  }

  .customerDialogBox .up-img-box .uib-item p {
    position: absolute;
    bottom: -30px;
    font-size: 12px;
    text-align: center;
    width: 100%;
  }

  .customerDialogBox .up-img-box .uib-item:hover .imgzhe-btn {
    opacity: 1;
  }

  .customerDialogBox .imgzhe {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
  }

  .customerDialogBox .imgzhe .imgzhe-btn {
    background: rgba(0, 0, 0, 0.6);
    width: 100%;
    height: 100%;
    text-align: center;
    line-height: 120px;
    position: absolute;
    top: 0;
    opacity: 1;
    transition: all linear 0.5s;
  }

  .customerDialogBox .imgzhe .imgzhe-btn i {
    font-size: 22px;
    margin-right: 5px;
    color: white;
    cursor: pointer;
  }

  .customerDialogBox .up-ctrl {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
  }

  .customerDialogBox .up-ctrl>div {
    width: 100%;
    height: 100%;
    text-align: center;
    line-height: 140px;
  }
</style>
<template>
  <el-dialog :visible.sync="dialogTableVisible" @closed="closedDialog" :close-on-click-modal="false"
    class="add-box add-box1 customerDialogBox">
    <div class="add-tit" slot="title">
      <p><span></span>{{name}}</p>
      <span icon="el-icon-close"></span>
    </div>
    <div class="form-box" v-if="show">
      <el-form :model="form" ref="form" class="MyEditForm" :rules="rules">
        <p class="form-box-tit"><span class="radius"></span> 基本信息</p>
        <el-row :gutter="30">
          <el-col :span="8">
            <div class="MyEditForm-item">
              <el-form-item label="线索名称" prop="CustomerName">
                <el-input placeholder="请输入线索姓名" v-model="form.CustomerName"></el-input>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="客户级别" prop="CustomerLevel">
                <el-select v-model="form.CustomerLevel" placeholder="请选择客户级别">
                  <el-option v-for="item in CustomerLevelEnum" :key="item.ID" :label="item.Name" :value="item.ID">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="所属品牌">
                <el-select multiple collapse-tags v-model="form.BrandIds" placeholder="请选择所属品牌">
                  <el-option v-for="item in CustomerBrandList" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="联系人">
                <el-input placeholder="请输入联系人" v-model="form.Contact"></el-input>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="电话">
                <el-input placeholder="请输入联系电话" v-model="form.ContactNumber"></el-input>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="经营场所" prop="Premises">
                <el-select v-model="form.Premises" placeholder="请选择经营场所">
                  <el-option v-for="item in jycs" :key="item.ID" :label="item.Name" :value="item.ID">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="8">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="线索来源" prop="CustomerSourceType">
                <el-select v-model="form.CustomerSourceType" placeholder="请选择线索来源">
                  <el-option v-for="item in SourceEnum" :key="item.ID" :label="item.Name" :value="item.ID">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="下次联系时间">
                <el-date-picker :clearable="false" value-format="yyyy-MM-dd HH:mm:ss" v-model="form.NextContactTime"
                  type="date" placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="24" v-if="form.CustomerSourceType === 6">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="线索来源信息" prop="CustomerSource">
                <el-input type="textarea" placeholder="请输入线索来源信息" v-model="form.CustomerSource"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="备注">
                <el-input type="textarea" placeholder="线索相关备注" v-model="form.Remark"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <p class="form-box-tit"><span class="radius"></span> 位置信息</p>
        <el-row :gutter="30">
          <el-col :span="6">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="国/省/市/区">
                <el-select @change="getProvinceList(form.Country,1)" v-model="form.Country" placeholder="国家">
                  <el-option v-for="item in countryList" :key="item.ID" :label="item.Name" :value="item.ID">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="国/省/市/区">
                <el-select @change="getProvinceList(form.Province,2)" v-model="form.Province" placeholder="省份">
                  <el-option v-for="item in provinceList" :key="item.ID" :label="item.Name" :value="item.ID">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="国/省/市/区">
                <el-select @change="getProvinceList(form.City,3)" v-model="form.City" placeholder="城市">
                  <el-option v-for="item in cityList" :key="item.ID" :label="item.Name" :value="item.ID">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="国/省/市/区">
                <el-select v-model="form.District" placeholder="地区">
                  <el-option v-for="item in district" :key="item.ID" :label="item.Name" :value="item.ID">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="MyEditForm-item label-pad-left">
              <el-form-item label="定位">
                <el-input type="textarea" placeholder="请输入详情地址" v-model="form.Address"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <p class="form-box-tit"><span class="radius"></span> 上传图片</p>
        <div class="up-img-box flex-center">
          <div class="uib-item">
            <template>
              <div class="imgzhe" :style="{backgroundImage: 'url(' + form.Images[0] + ')', backgroundSize:'100% 100%'}"
                v-if="form.Images.length">
                <div class="imgzhe-btn">
                  <i @click.stop="showImg(1)" class="el-icon-search"></i>
                  <i @click="delImg(1)" class="el-icon-delete"></i>
                </div>
              </div>
              <div v-else class="up-ctrl">
                <el-upload :file-list="fileList" :limit="1" :http-request="uploadImg" :multiple="true"
                  accept="image/jpeg,image/gif,image/png,image/bmp" :show-file-list="false" action="">
                  <img src="../../assets/img/cust/upimg.png" alt="">
                </el-upload>
              </div>
            </template>
            <p>图片</p>
          </div>
          <div class="uib-item">
            <template>
              <div class="imgzhe"
                :style="{backgroundImage: 'url(' + form.BusinessCardPhotos[0] + ')', backgroundSize:'100% 100%'}"
                v-if="form.BusinessCardPhotos.length">
                <div class="imgzhe-btn">
                  <i @click.stop="showImg(2)" class="el-icon-search"></i>
                  <i @click="delImg(2)" class="el-icon-delete"></i>
                </div>
              </div>
              <div v-else class="up-ctrl">
                <el-upload :file-list="fileList" :limit="1" :http-request="uploadImg2" :multiple="true"
                  accept="image/jpeg,image/gif,image/png,image/bmp" :show-file-list="false" action="">
                  <img src="../../assets/img/cust/upimg.png" alt="">
                </el-upload>
              </div>
            </template>
            <p>名片照片</p>
          </div>
          <div class="uib-item">
            <template>
              <div class="imgzhe"
                :style="{backgroundImage: 'url(' + form.HousePhotos[0] + ')', backgroundSize:'100% 100%'}"
                v-if="form.HousePhotos.length">
                <div class="imgzhe-btn">
                  <i @click.stop="showImg(3)" class="el-icon-search"></i>
                  <i @click="delImg(3)" class="el-icon-delete"></i>
                </div>
              </div>
              <div v-else class="up-ctrl">
                <el-upload :file-list="fileList" :limit="1" :http-request="uploadImg3" :multiple="true"
                  accept="image/jpeg,image/gif,image/png,image/bmp" :show-file-list="false" action="">
                  <img src="../../assets/img/cust/upimg.png" alt="">
                </el-upload>
              </div>
            </template>
            <p>门牌照片</p>
          </div>
        </div>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" class="add-box-btn" @click="submitForm('form')">确 定</el-button>
      <el-button class="add-box-btn add-box-cancel" @click="dialogTableVisible = false, resetForm('form')">取 消
      </el-button>
    </div>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </el-dialog>
</template>
<script>
  export default {
    props: {
      CustomerId: {
        type: Number,
        default: 0,
      },
      name: {
        type: String,
        default: "新建客户",
      },
      type: {
        type: String,
        default: "1",
      },
    },
    data() {
      let checkPhone = (rule, value, callback) => {
        if (!value) {
          return callback(new Error("手机号不能为空"));
        } else {
          const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
          if (reg.test(value)) {
            callback();
          } else {
            return callback(new Error("请输入正确的手机号"));
          }
        }
      };
      return {
        dialogTableVisible: false,
        SourceEnum: [],
        countryList: "",
        provinceList: "",
        dialogImageUrl: "",
        dialogVisible: false,
        cityList: "",
        district: "",
        jycs: [],
        fileList: [],
        CustomerLevelEnum: [],
        CustomerBrandList: [],
        form: {
          NextContactTime: "",
          CustomerId: 0,
          CustomerName: "",
          CustomerLevel: "",
          Contact: "",
          ContactNumber: "",
          Address: "",
          Remark: "",
          Premises: "",
          CustomerStatus: 0,
          CustomerSourceType: "",
          CustomerSource: "",
          Country: "",
          Province: "",
          City: "",
          District: "",
          BrandIds: [],
          Images: [],
          BusinessCardPhotos: [],
          HousePhotos: [],
        },
        rules: {
          CustomerName: [{
            required: true,
            message: "请输入线索名称",
            trigger: "blur"
          }, ],
          CustomerSource: [{
            required: true,
            message: "请输入客户来源信息",
            trigger: "blur"
          }, ],
          CustomerLevel: [{
            required: true,
            message: "请选择客户级别",
            trigger: "change"
          }, ],
          CustomerSourceType: [{
            required: true,
            message: "请选择客户来源",
            trigger: "change"
          }, ],
          Contact: [{
            required: true,
            message: "请输入联系人",
            trigger: "blur"
          }],
          ContactNumber: [{
            validator: checkPhone,
            trigger: "blur"
          }],
          Premises: [{
            required: true,
            message: "请选择经营场所",
            trigger: "change"
          }, ],
          BrandIds: [{
            required: true,
            message: "请选择所属品牌",
            trigger: "change"
          }, ],
        },
        show: true,
      };
    },
    watch: {
      CustomerId: {
        handler(val, oldVal) {

        },
        deep: true,
      },
    },
    mounted() {
      this.GetCustomerLevelEnum();
      this.getJycs();
      this.getCustomerBrandList();
      this.getCountryList();
      this.GetCustomerSourceEnum();
      let $this = this;
      setTimeout(() => {
        $this.dialogTableVisible = true;
      }, 50);
      if (this.CustomerId !== 0) {
        this.apipost(
          "/api/Customer/GetAllCustmersModel", {
            CustomerId: this.CustomerId
          },
          (res) => {
            if (res.data.resultCode == 1) {
              let data = res.data.data;
              if (!data.HousePhotos) {
                data.HousePhotos = [];
              }
              if (!data.BusinessCardPhotos) {
                data.BusinessCardPhotos = [];
              }
              if (!data.Images) {
                data.Images = [];
              }
              if (!data.CustomerSourceType) {
                data.CustomerSourceType = "";
              }
              if (!data.CustomerSource) {
                data.CustomerSource = "";
              }
              if (!data.Province) {
                data.Province = "";
              }
              if (!data.Country) {
                data.Country = "";
              }
              if (!data.City) {
                data.City = "";
              }
              if (!data.District) {
                data.District = "";
              }
              if (data.Country) {
                this.getProvinceList2(data.Country, 1);
              }
              if (data.Province) {
                this.getProvinceList2(data.Province, 2);
              }
              if (data.City) {
                this.getProvinceList2(data.City, 3);
              }
              this.form = data;
            }
          }
        );
      }
    },
    methods: {
      closedDialog() {
        this.MsgBus.$emit("closeCustomerDialogBox");
      },
      addData() {
        this.apipost("/api/Customer/CreateCustomerClue", this.form, (res) => {
          if (res.data.resultCode == 1) {
            this.dialogTableVisible = false;
            // 有id 发送更新抽屉消息
            if (this.CustomerId !== 0) {
              this.$message.success("修改线索成功");
              this.MsgBus.$emit("RefreshInfoBox");
            } else {
              this.$message.success("新建线索成功");
            }
            if (this.type !== "2") {
              this.$emit("getList");
            }
            this.resetForm("form");
          }
        });
      },
      submitForm(addMsg) {
        //提交创建、修改表单
        this.$refs[addMsg].validate((valid) => {
          if (valid) {
            this.addData();
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        // 重置表单
        this.form = {
          CustomerId: 0,
          CustomerName: "",
          CustomerLevel: "",
          Contact: "",
          ContactNumber: "",
          Address: "",
          Remark: "",
          Premises: "",
          CustomerStatus: 0,
          CustomerSourceType: "",
          CustomerSource: "",
          Country: "",
          Province: "",
          City: "",
          District: "",
          BrandIds: [],
          Images: [],
          BusinessCardPhotos: [],
          HousePhotos: [],
          NextContactTime: "",
        };
        this.$refs[formName].resetFields();
      },
      getCustomerBrandList() {
        // 获取品牌
        this.apipost2(
          "app_get_customer_brand", {},
          (res) => {
            if (res.data.resultCode == 1) {
              this.CustomerBrandList = res.data.data;
            } else {
              this.$message.error(res.data.message);
            }
          },
          (err) => {}
        );
      },
      getJycs() {
        this.apipost2(
          "app_today_visit_GetCustomerPremisesEnum", {},
          (res) => {
            if (res.data.resultCode == 1) {
              this.jycs = res.data.data;
            }
          },
          (err) => {}
        );
      },
      GetCustomerLevelEnum() {
        this.apipost("/api/Customer/GetCustomerLevelEnum", {}, (res) => {
          if (res.data.resultCode == 1) {
            this.CustomerLevelEnum = res.data.data;
          }
        });
      },
      GetCustomerSourceEnum() {
        this.apipost("/api/Customer/GetCustomerSourceEnum", {}, (res) => {
          if (res.data.resultCode == 1) {
            this.SourceEnum = res.data.data;
          }
        });
      },
      getProvinceList2(ID, type) {
        let msg = {
          Id: ID,
        };
        this.apipost2(
          "dict_post_Destination_GetChildList",
          msg,
          (res) => {
            if (type == 1) {
              this.provinceList = res.data.data;
            } else if (type == 2) {
              this.cityList = res.data.data;
            } else if (type == 3) {
              this.district = res.data.data;
            }
          },
          (err) => {}
        );
      },
      //获取国家
      getCountryList() {
        this.apipost2(
          "dict_post_Destination_GetCountry", {},
          (res) => {
            this.countryList = res.data.data;
          },
          (err) => {}
        );
      },
      //根据省份获取城市
      getProvinceList(ID, type) {
        let msg = {
          Id: ID,
        };
        if (type == 1) {
          this.form.Province = "";
          this.form.City = "";
          this.form.District = "";
        } else if (type == 2) {
          this.form.City = "";
          this.form.District = "";
        } else if (type == 3) {
          this.form.District = "";
        }
        if (this.QCountry !== "") {
          this.apipost2(
            "dict_post_Destination_GetChildList",
            msg,
            (res) => {
              if (type == 1) {
                this.provinceList = res.data.data;
              } else if (type == 2) {
                this.cityList = res.data.data;
              } else if (type == 3) {
                this.district = res.data.data;
              }
            },
            (err) => {}
          );
        }
      },
      uploadImg(file) {
        //上传
        let newArr = [];
        newArr.push(file.file);
        let path = "/Upload/Temporary/";
        this.UploadSelfFileT(path, newArr, (x) => {
          let url = this.domainManager().ViittoFileUrl + x.data.FilePath;
          this.form.Images.push(url);
        });
      },
      uploadImg2(file) {
        //上传
        let newArr = [];
        newArr.push(file.file);
        let path = "/Upload/Temporary/";
        this.UploadSelfFileT(path, newArr, (x) => {
          let url = this.domainManager().ViittoFileUrl + x.data.FilePath;
          this.form.BusinessCardPhotos.push(url);
        });
      },
      uploadImg3(file) {
        //上传
        let newArr = [];
        newArr.push(file.file);
        let path = "/Upload/Temporary/";
        this.UploadSelfFileT(path, newArr, (x) => {
          let url = this.domainManager().ViittoFileUrl + x.data.FilePath;
          this.form.HousePhotos.push(url);
        });
      },
      showImg(i) {
        let url = "";
        if (i === 1) {
          url = this.form.Images[0];
        } else if (i === 2) {
          url = this.form.BusinessCardPhotos[0];
        } else if (i === 3) {
          url = this.form.HousePhotos[0];
        }
        this.dialogImageUrl = url;
        this.dialogVisible = true;
      },
      delImg(i) {
        if (i === 1) {
          this.form.Images = [];
        } else if (i === 2) {
          this.form.BusinessCardPhotos = [];
        } else if (i === 3) {
          this.form.HousePhotos = [];
        }
      },
    },
  };
</script>