<template>
  <div>
    <Modal :value="isShow" title="排班" width="48" @on-visible-change="e => $emit('update:isShow',e)">
      <template v-slot:default>
        <el-form ref="elForm" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="时间选择" prop="end_time">
            <div style="font-weight: 600">{{ date }}</div>
            <el-time-picker
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="form.start_time"
              :picker-options="{
                selectableRange: '00:00:00 - 23:59:59'
              }"
              placeholder="开始时间">
            </el-time-picker>
            <el-time-picker
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="form.end_time"
              :picker-options="{
                selectableRange: '00:00:00 - 23:59:59'
              }"
              placeholder="结束时间">
            </el-time-picker>
          </el-form-item>
          <el-form-item label="护工选择" prop="nurse_id">
            <el-select v-model="form.nurse_id" placeholder="请选择护工" style="width: 300px;">
              <el-option v-for="(item,index) in nurses" :key="item.index" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="服务项目" prop="schedule_list_skus">
            <el-checkbox-group v-model="form.schedule_list_skus">
              <template v-for="(item) in skus">
                <el-checkbox :label="item.sku.id">{{ item.sku.name }}</el-checkbox>
              </template>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
      </template>

      <template v-slot:footer>
        <Poptip
          :transfer="true"
          confirm
          title="确认要删除吗"
          @on-ok="destroySchedule">
          <Button type="primary" ghost>删除</Button>
        </Poptip>
        <Button @click="$emit('update:isShow',false)" style="margin-left: 8px">取消</Button>
        <Button type="primary" @click="submit">确认</Button>
      </template>
    </Modal>
  </div>
</template>

<script>
import {parseTime} from '@/utils'
import {scheduleDelete, scheduleSave} from '@/api/schedule'

export default {
  props: {
    customerId: [Number, String],
    orderId: [Number, String],
    productId: [Number, String],
    date: {
      type: String,
      default: ''
    },
    isShow: {
      type: Boolean,
      default: false
    },
    skus: {
      type: Array,
      default: () => []
    },
    nurses: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {
        id:'',
        customer_id: '',
        product_id: '',
        order_id: '',
        nurse_id: '',
        start_time: '',
        end_time: '',
        schedule_list_skus: []
      },
      time: [new Date(), new Date()],
      rules: {
        nurse_id: [
          {required: true, message: '请选择护工'}
        ],
        start_time: [
          {required: true, message: '请选择开始时间'}
        ],
        end_time: [
          {required: true, message: '请选择结束时间'}
        ],
        schedule_list_skus: [
          {required: true, message: '请选择护理项目'}
        ]
      }
    }
  },
  methods: {
    submit() {
      this.form.customer_id = this.customerId
      this.form.product_id = this.productId
      this.form.order_id = this.orderId
      let temp = this.form.schedule_list_skus.map(item => {
        return {sku_id: item}
      })
      this.form.schedule_list_skus = temp
      this.$refs['elForm'].validate().then(valid => {
        console.log(valid)
        if (valid) {
          scheduleSave(this.form).then(res => {
            this.$successMessage('add', '排班')
            this.$emit('update:isShow', false)
            this.$emit('refresh')
          }).catch(err => {
            this.$emit('update:isShow', false)
          })
        }
      })
    },
    destroySchedule(){
      scheduleDelete({id:this.form.id}).then(res => {
        this.$successMessage('destroy','排班')
        this.isShow = false
        this.$emit('refresh')
      })
    },
  },
  watch: {
    isShow(val) {
      if (val) {
      } else {
        this.$refs['elForm'].resetFields()
      }
    }
  }
}
</script>

<style lang="scss" scoped>

::v-deep .el-form-item__error {
  transform: none !important;
  left: 0 !important;
}
</style>
