<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="驾校名称" prop="schoolName">
        <el-input v-model="form.schoolName" placeholder="请输入驾校名称" />
      </el-form-item>
      <el-form-item label="驾校封面图" prop="photo">
        <image-upload  v-model="form.photo"/>
      </el-form-item>
      <el-form-item label="营业开始时间" prop="businessStartTime">
        <el-time-select
          placeholder="起始时间"
          v-model="form.businessStartTime"
          :picker-options="{
      start: '08:30',
      step: '00:15',
      end: '18:30'
    }">
        </el-time-select>
      </el-form-item>

      <el-form-item label="营业结束时间" prop="businessEndTime">
        <el-time-select
          placeholder="结束时间"
          v-model="form.businessEndTime"
          :picker-options="{
      start: '08:30',
      step: '00:15',
      end: '18:30',
      minTime: businessStartTime
    }">
        </el-time-select>
      </el-form-item>
      <el-form-item label="驾校负责人" prop="leaderName">
        <el-input v-model="form.leaderName" placeholder="请输入驾校负责人" />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入联系方式" />
      </el-form-item>
      <map-componment  style="margin-left: 35px" :pform="mapForm" ref="mapRef" @pform="getForm"></map-componment>
<!--      <p>详细地址：<el-input v-model="form.address"-->
<!--                            placeholder="请输入内容"-->
<!--                            style="width: 50%"-->
<!--                            @change="getLngLat"-->
<!--      ></el-input></p>-->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="yesitis">确 定</el-button>
    </div>
    <!-- 添加或修改电梯品牌对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>

    </el-dialog>
  </div>
</template>

<script>
import {
  listSchoolInfo,
  getSchoolInfo,
  delSchoolInfo,
  addSchoolInfo,
  updateSchoolInfo,
  getSchoolInfoByDeptId
} from "./api/schoolInfoConfig";
import mapComponment from "@/views/components/map/mapComponent.vue";
export default {
  props:['pform'],
  name: "schoolInfoConfig",
  components:{
    mapComponment
  },
  data() {
    return {
      // 位置信息
      mapForm: {
        lng: "116.9645",
        lat: "36.975",
        address: "",
        adcode: "", //地区编码
      },
      businessStartTime: null,
      businessEndTime: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 驾校信息表格数据
      schoolInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        schoolName: null,
        input: null,
        sort: null,
        phone: null,
        region: this.$refs.mapRef?.inputInfo,
        photo: null,
        address: null,
        businessStartTime: null,
        businessEndTime: null,
      },
      // 表单参数
      form: {
        businessStartTime: null,
        businessEndTime: null,
      },
      // 表单校验
      rules: {
        schoolName: [
          { required: true, message: "驾校名称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
        ],
        leaderName: [
          { required: true, message: "驾校负责人不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "显示排序不能为空", trigger: "blur" }
        ],
      }
    };
  },

  created() {
    this.getList();
  },
  mounted() {
    this.getList();
  },
  methods: {
    busChange(data){
      console.log(data,'业务变化')
      //处理新增
      for (let i = 0; i < data.length; i++) {
        let flag ='0'
        for (let j = 0; j < this.form.priceDtoList.length; j++) {
          if (data[i]== this.form.priceDtoList[j].serviceId){
            flag = '1'
          }
        }
        if (flag=='0'){
          console.log(data[i],i,'199')
          //就需要追加进去这个业务
          this.elevTypeDict.forEach(it=>{
            if (it.dictValue==data[i]){
              let item ={
                serviceId:it.dictValue,
                serviceName:it.dictLabel,
                priceStart:0,
                priceEnd:0
              }
              this.form.priceDtoList.push(item)
            }
          })

        }
      }
      //处理删除
      for (let i = 0; i < this.form.priceDtoList.length; i++) {
        let flag = '0'
        for (let j = 0; j < data.length; j++) {
          if (data[i] == this.form.priceDtoList[j].serviceId) {
            flag = '1'
          }
        }
        if (flag=='0'){
          //就删除这个业务

          this.form.priceDtoList.splice(i,1)
        }
      }
    },
    getForm(data){

      if (data != undefined){
        this.mapForm = data;

        this.form.address = this.mapForm.address
        this.form.lgt = this.mapForm.lng
        this.form.lat = this.mapForm.lat
      }
    },
    /** 查询电梯品牌列表 */
    getList() {
      this.loading = true;
      getSchoolInfoByDeptId().then(res => {
        if (res.data){
          this.form = res.data;
          this.mapForm.lat = this.form.lat;
          this.mapForm.lng = this.form.lgt;
          this.mapForm.address = this.form.address;
        }
        this.$refs.mapRef.initAMap();
        console.log(this.mapForm)
        this.loading = false;
      })
    },
    async yesitis(){
      this.form.region = String(this.$refs.mapRef.inputInfo)
      // this.form.region = this.$refs.mapRef.inputInfo.repeat(/\[|]/g,'')

      await updateSchoolInfo(this.form).then(res=>{
        this.$modal.msgSuccess("保存成功");
        this.getList()
      })

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        schoolName: null,
        leaderName: null,
        phone: null,
        photo: null,
        region: this.$refs.mapRef.inputInfo,
        address: null,
        lat: null,
        lgt: null,
        businessStartTime: null,
        businessEndTime: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getElevBrand(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电梯品牌";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.lat){
            this.$message.warning("请选择位置信息")
            return
          }
          if (!this.form.brandAdress){
            this.$message.warning("请填写详细位置")
            return
          }
          if (this.form.id != null) {
            updateElevBrand(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addElevBrand(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
