<!--
 * @Description: 新增/编辑弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: Sugar.
 * @LastEditTime: 2023-11-24 13:55:00
 * @FilePath: \cattle_webui\src\views\venue\schedulingMr\dialog\AddOrEditDialog.vue
 * @Copyright: Copyright (c) 2016~2023 by Sugar., All Rights Reserved.
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="600px"
    class="text-dia-log-class"
    append-to-body
    :close-on-click-modal="false"
    @close="cancel"
  >
    <div class="dialog">
      <el-form :model="form" ref="form" :rules="rules" label-width="140px">
        <el-form-item label="团队名称：" prop="teamIdList">
          <el-select
            v-model="form.teamIdList"
            placeholder="团队名称"
            clearable
            multiple
          >
            <el-option
              v-for="dict in goodsPageListS"
              :key="dict.id"
              :value="dict.id"
              :label="dict.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm"
        v-loading.fullscreen.lock="loading"
        element-loading-text="提交中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <span v-if="loading">提交中...</span>
        <span v-else>保存</span>
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { bindTeamApi,getSelectById } from "@/api/priceConfiguration/index";
import { pageList as goodsPageListApi } from '@/api/team/teamMr'
export default {
  name: "addAndEdit",
  props: {
    channelType: {
      type: [String]
    }
  },
  data() {
    return {
      title: "绑定团队",
      model: "EDIT",
      open: false,
      loading: false,
      tableType: false,
      form: {
        id: undefined,
        teamIdList: []
      },
      rules: {
        teamIdList: [{ required: true, message: "请选择团队", trigger: ["change","blur"] }],
      },
      editType: false,

      goodsPageListS: [], // 团队
    };
  },
  created() {
    this.goodsPageListFun()
  },
  methods: {
    /**
     * 打开弹框
     * @date 2023-11-22
     * @param {any} obj
     * @returns {any}
     */
    async openDialog(title, obj, type) {
      this.open = true;
      this.editType = false;
      this.form = {
        id: obj.id,
        teamIdList: []
      }
      await this.getSelectByIdFun(obj)
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    /**  获取详情 */
    async getSelectByIdFun(obj) {
      try {
        let res = await getSelectById({
          id: obj.id
        })
        if(res.code == 200){
          let list = []
          res.data.teamList ? res.data.teamList.forEach((item)=>{
              if(item.teamId) {
                list.push(item.teamId)
              }
          }): ''
          this.form = {
            id: obj.id,
            teamIdList: list
          }
          console.log("this.form===",this.form)
        }
      } catch (error) {      
      }
    },
    /**  获取团队  */
    async goodsPageListFun(){
      try {
        let res = await goodsPageListApi({
          pageNum: 1,
          pageSize: 999,
        })
        if(res.code == 200){
          this.goodsPageListS = res.data.rows || []
        }
      } catch (error) {      
      }
    },
    /**
     * 保存
     * @date 2023-11-22
     * @returns {any}
     */
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true
            let postEdit = JSON.parse(JSON.stringify(this.form))
            const { code } = await bindTeamApi({ ...postEdit });
            this.loading = false
            if (code === 200) {
              this.$message.success("操作成功!");
              this.$emit("getList");
              this.cancel();
            }
          } catch (error) {
            console.error("error====",error)
          } finally {
            this.loading = false;
          }
        }
      });
    },
    /**
     * 重置
     * @date 2023-11-22
     * @returns {any}
     */
    reset() {
      
    },
    /**
     * 关闭弹框
     * @date 2023-11-22
     * @returns {any}
     */
    cancel() {
      this.reset();
      this.open = false;
    },
    selectWeekDay(item){
      let index = -1
      this.form.weekName.forEach((item1,index1)=>{
        if(item.key == item1) {
          index = index1
        }
      })
      if(index != -1) {
        this.form.weekName.splice(index,1)
      }else {
        this.form.weekName.push(item.key)
      }
      if(this.form.weekName.length>0) {
        this.form.weekType = 1
      }else {
        this.form.weekType = 0
      }
    },
    selectWeekType() {
      this.form.weekType = this.form.weekType == 1 ? 0 : 1
      if(!this.form.weekType) {
        this.form.weekName = []
      }
    },
    /**  */
    setSaleAmounttile(){
      let str = '销售价：'
      if(this.channelType == "applet") {
        str = "销售价："
      }
      if(this.channelType == "retail") {
        str = "分销价："
      }
      if(this.channelType == "window") {
        str = "销售价："
      }
      if(this.channelType == "group") {
        str = "销售价："
      }
      return str
    },
    selectTouristLimit() {
      this.$set(this.form,'touristMin',undefined)
      this.$set(this.form,'touristMax',undefined)
    }
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  height: 200px;
  overflow-y: auto;
}
.dialog {
  .upload-btn {
    width: 100px;
    height: 100px;
    background-color: #fbfdff;
    border: dashed 1px #c0ccda;
    border-radius: 5px;
    i {
      font-size: 30px;
      margin-top: 20px;
    }
    &-text {
      margin-top: -10px;
    }
  }
  .avatar {
    cursor: pointer;
  }
}
</style>
