<template>
  <div>
    <el-form :model="musicForm" label-width="110px" ref="form">
      <el-form-item
        label="乐器分类"
        prop="goodsCategoryId"
        :rules="[
          { required: true, message: '请选择乐器分类', trigger: 'blur' },
        ]"
      >
        <el-form-item>
          <el-select
            style="width: 400px !important"
            v-model.trim="musicForm.goodsCategoryId"
            filterable
            clearable
            @change="changeCategory"
            placeholder="乐器分类"
          >
            <el-option
              v-for="(item, index) in typeList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form-item>
      <el-form-item
        label="乐器名称"
        prop="goodsId"
        :rules="[
          { required: true, message: '请选择乐器名称', trigger: 'blur' },
        ]"
      >
        <el-select
          style="width: 400px !important"
          :disabled="!musicForm.goodsCategoryId"
          v-model.trim="musicForm.goodsId"
          filterable
          clearable
          placeholder="乐器名称"
        >
          <el-option
            v-for="(item, index) in musicList"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="是否乐保"
        prop="status"
        :rules="[
          { required: true, message: '请选择是否乐保', trigger: 'blur' },
        ]"
      >
        <el-select
          style="width: 400px !important"
          v-model.trim="musicForm.status"
          clearable
          placeholder="是否乐保"
        >
          <el-option value="0" label="否"></el-option>
          <el-option value="1" label="是"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="乐保结束日期"
        :rules="[
          { required: true, message: '请选择乐保结束日期', trigger: 'blur' },
        ]"
        v-if="musicForm.status == 1"
        prop="date"
      >
        <el-date-picker
          style="width: 400px"
          v-model="musicForm.date"
          clearable
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="23:59:59"
          :picker-options="pickerOptions"
          placeholder="乐保结束日期"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { goodsQuery } from "@/api/businessManager";
import { addStudentInstrument, updateStudentInstrument } from "@/api/buildTeam";
import { getTimes } from "@/utils";
import dayjs from 'dayjs'
export default {
  props: ["categoryList", "operationData", "operationStatus"],
  data() {
    return {
      musicForm: {
        studentId: this.$route.query.userId,
        goodsId: "",
        goodsCategoryId: "",
        status: "",
        date: null,
      },
      pickerOptions: {
        firstDayOfWeek: 1,
        disabledDate(time) {
          let times = dayjs(dayjs().format('YYYY-MM-DD')).valueOf()
          return times > time.getTime();
        },
      },
      typeList: this.categoryList,
      musicList: [],
    };
  },
  async mounted() {
    console.log(this.operationData)
    let operationData = this.operationData
    if(operationData) {
      let musicForm = this.musicForm
      musicForm.goodsCategoryId = operationData.goodsCategoryId
      if(operationData.goodsCategoryId) {
        await this.changeCategory(operationData.goodsCategoryId)
      }
      musicForm.goodsId = operationData.goodsId
      musicForm.goodsName = operationData.goodsName
      musicForm.status = operationData.status + ''
      musicForm.date = operationData.endTime || null


      musicForm.id = operationData.id
    }
  },
  methods: {
    changeCategory(val) {
      this.musicForm.goodsName = "";
      this.musicForm.goodsId = null;
      if (val) {
        goodsQuery({ goodsCategoryId: val, rows: 99999, type: 'INSTRUMENT' }).then((res) => {
          if (res.code == 200 && res.data) {
            this.musicList = res.data.rows;
          }
        });
      }
    },
    addMusicSubmit() {
      this.$refs.form.validate(async (res) => {
        if (res) {
          try {
            const { date, status, ...rest } = this.musicForm;
            let obj = {
              ...rest,
              status
            };
            // 是否有乐保
            if(status == 1) {
              if(this.operationData && this.operationData.startTime) {
                obj.startTime = this.operationData.startTime
              } else {
                obj.startTime = dayjs().format('YYYY-MM-DD HH:mm') + ':00'
              }
              obj.endTime = date
            } else {
              obj.startTime = ''
              obj.endTime = ''
            }
            if(this.operationStatus == 'create') {
              const res = await addStudentInstrument(obj);
              this.$message.success('添加成功')
            } else if(this.operationStatus == 'update') {
              const res = await updateStudentInstrument(obj);
              this.$message.success('修改成功')
            }
            this.$emit('close')
            this.$emit('getList')
          } catch (e) {
            console.log(e);
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
