<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <div>
      <div :style="{ borderBottom: '1px solid #E9E9E9' }">
        <a-checkbox :checked="checkAll" @change="onCheckAllChange">
          全选
        </a-checkbox>
      </div>
      <br />
      <a-checkbox-group v-model="model.layoutIds" @change="onChange">
        <a-checkbox :value="item.id" v-for="item in layouts" :key="item.id">
          {{ item.name }}
        </a-checkbox>
      </a-checkbox-group>
    </div>
  </j-modal>
</template>
    
    <script>
import { getAllLayouts } from "@/api/roomLayout";
import { editHourRule } from '@/api/allDaysPriceRuleApi.js' 
export default {
  name: "hourRoomForm",
  components: {},
  data() {
    return {
      checkAll: false,
      model: {
        id: null,
        layoutIds: [],
      },
      title: "关联房型",
      width: 1200,
      visible: false,
      disableSubmit: false,
      layouts: [],
    };
  },
  mounted() {},
  methods: {
    setData(data) {
      getAllLayouts().then((res) => {
        if (res.code == 200) {
          this.layouts = res.result.records;
          this.visible = true;
          this.model = JSON.parse(JSON.stringify(data));
          this.model.layoutIds =this.model.layoutIds? this.model.layoutIds.split(","):[]
          this.checkAll = this.isAllInArr()
        }
      });
    },
    onChange(checkedList) {
      this.checkAll = (checkedList.length === this.layouts.length);
    },
    onCheckAllChange(e) {
        if(e.target.checked) {
            this.model.layoutIds = this.layouts.map(s=>s.id)
        } else {
            this.model.layoutIds = []
        }
        this.checkAll = e.target.checked
    },
    isAllInArr() {
        let result = true
        this.layouts.forEach(s=>{
            let index = this.model.layoutIds.findIndex(a=>a==s.id)
            if(index == -1) {
                result = false
            }
        })
        return result
    },
    edit(record) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.realForm.edit(record);
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
        let param = JSON.parse(JSON.stringify(this.model))
        param.layoutIds = (param.layoutIds||[]).toString()
        editHourRule(param).then(res => {
                if(res.code == 200 )  {
                    // this.confirmLoading = false
                    this.$message.success('保存成功')
                    this.$emit('ok')
                }
            }).finally(_ => {
                // this.confirmLoading = false
            })
    },
    handleCancel() {
      this.close();
    },
  },
};
</script>