<!-- 班表生效日期 -->
<template>
  <div>
    <Row justify="center" style="position: relative;" type="flex">
      <Col style="margin-top: 24px;">
        <RadioGroup
          @on-change="changeData"
          size="large"
          style="margin:0px 0px 10px 0px"
          type="button"
          v-if="reciSiteData.length>0"
          v-model="chosenBtn"
        >
          <Radio
            :key="item.wmSiteId"
            :label="index"
            v-for="(item,index) in reciSiteData"
          >{{item.wmName}}</Radio>
        </RadioGroup>
        <Form :label-width="100" :model="formEffectDate" :rules="ruleValidate" ref="formLeaveParam">
          <FormItem label="班表生效日期" prop="approver1Time">
            <Select
              :disabled="inputDisalbed"
              style="width: 200px;"
              v-model="formEffectDate.approver1Time"
            >
              <Option :key="index" :value="index" v-for="(item,index) in effectDateArr">{{ item }}</Option>
            </Select>
          </FormItem>
          <FormItem>
            <Button
              :disabled="isDisableEditBtn"
              :loading="loading"
              @click="handleSubmit('formLeaveParam')"
              type="primary"
              v-permission:edit
            >{{btnName}}</Button>
          </FormItem>
        </Form>
      </Col>
      <Spin fix v-if="spinShow"></Spin>
    </Row>
  </div>
</template>

<script>
import { CALC_DATE } from '@conf/dict'
import WmSitesAPI from '@api/common/wmSites'
import WfmTakeEffectConfigAPI from '@api/common/wfmTakeEffectConfig'

export default {
  name: 'schedule-effect-date',
  data () {
    return {
      btnName: '编辑',
      spinShow: true,
      isDisableEditBtn: true,
      inputDisalbed: true,
      loading: false,
      chosenBtn: 0,
      effectDateArr: CALC_DATE,
      formEffectDate: {
        approver1Time: 0,
        uuid: ''
      },
      ruleValidate: {
        approver1Time: [
          {
            required: true,
            type: 'number',
            trigger: 'change',
            message: '不能为空'
          }
        ]
      },
      reciSiteData: {}
    }
  },
  created () {
    this.btnName = '编辑'
    WmSitesAPI.listOwer().then(({ data }) => {
      if (data.status === 'success') {
        this.reciSiteData = data.data
        this.chosenBtn = 0
        let id = ''
        if (data.data.length > 0) {
          if (data.data[0].wmSiteId !== undefined) {
            this.isDisableEditBtn = false
            id = data.data[0].wmSiteId
            this.queryData(id)
            return
          }
        }
        this.$Message.destroy()
        this.$Message.warning('未查到中心')
        this.spinShow = false
      }
    })
  },
  methods: {
    // 表单提交
    handleSubmit (name) {
      if (!this.inputDisalbed) {
        this.$refs[name].validate(valid => {
          if (valid) {
            this.loading = true
            if (this.formEffectDate.uuid === '') {
              let siteId = this.reciSiteData[this.chosenBtn].wmSiteId
              this.formEffectDate.orgCode = siteId
              WfmTakeEffectConfigAPI.add(this.formEffectDate)
                .then(({ data }) => {
                  if (data.status === 'success') {
                    this.inputDisalbed = true
                    this.btnName = '编辑'
                    this.loading = false
                    this.$Message.destroy()
                    this.$Message.success('数据新增成功')
                  }
                })
                .catch(() => {
                  this.loading = false
                })
            } else {
              WfmTakeEffectConfigAPI.edit(this.formEffectDate)
                .then(({ data }) => {
                  if (data.status === 'success') {
                    this.inputDisalbed = true
                    this.btnName = '编辑'
                    this.loading = false
                    this.$Message.destroy()
                    this.$Message.success('数据修改成功')
                  }
                })
                .catch(() => {
                  this.loading = false
                  this.$Message.destroy()
                })
            }
          }
        })
        this.$Message.destroy()
      } else {
        this.inputDisalbed = false
        this.btnName = '保存'
      }
    },
    // 表单重置
    handleReset (name) {
      this.$refs[name].resetFields()
    },
    changeData (e) {
      this.queryData(this.reciSiteData[parseInt(e)].wmSiteId)
    },
    queryData (wmSiteId) {
      if (wmSiteId === undefined) {
        this.$Message.destroy()
        return
      }
      this.inputDisalbed = true
      this.btnName = '编辑'
      WfmTakeEffectConfigAPI.info({ wmSiteId }).then(({ data }) => {
        this.spinShow = false
        if (data.status === 'success') {
          if (data.data === undefined || data.data === null) {
            this.formEffectDate.approver1Time = 0
            this.formEffectDate.uuid = ''
            this.$Message.destroy()
            return
          }
          this.formEffectDate = data.data
          this.$Message.destroy()
        }
      })
    }
  }
}
</script>

<style lang="less">
</style>
