<template>
  <div>
    <div v-if="!store.state.pageOnlyRead" class="container normal-panel normal-margin">
      <el-button type="warning" size="small" icon="el-icon-s-claim" @click="onSubmit((res)=>tool.showMessage(res))">保 存
      </el-button>
    </div>
    <div style="overflow-x:hidden">
      <el-row :gutter="16">
        <el-col :span="13">
          <div class="container normal-panel normal-margin">
            <p class="normal-title normal-margin">提报设置</p>
            <el-row :gutter="16">
              <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left" size="small">
                <el-col :span="24">
                  <el-form-item label="标题" prop="title">
                    <el-input v-model="form.title" placeholder="标题"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="提报类型" prop="onceonly">
                    <el-select v-model="form.onceonly" placeholder="选择提报类型">
                      <el-option label="一事一报" :value="1"></el-option>
                      <el-option label="一事多报" :value="0"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="生效日期" prop="begdate">
                    <el-date-picker style="width:100%" v-model="form.begdate" value-format="yyyy-MM-dd" type="date"
                      placeholder="生效日期">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="截止日期" prop="enddate">
                    <el-date-picker style="width:100%" v-model="form.enddate" value-format="yyyy-MM-dd" type="date"
                      placeholder="截止日期">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="提报要求" prop="notes">
                    <el-input v-model="form.notes" placeholder="提报要求"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="提报形式" prop="type">
                    <el-checkbox-group v-model="form.type" @change="onCheckboxChange">
                      <el-checkbox label="附件提报" name="isattrequired"></el-checkbox>
                      <el-checkbox label="文字提报" name="istextrequired"></el-checkbox>
                    </el-checkbox-group>
                    <!-- <el-checkbox :true-label="1" :false-label="0" v-model="form.isattrequired">附件提报</el-checkbox>
                    <el-checkbox :true-label="1" :false-label="0" v-model="form.istextrequired">文字提报</el-checkbox> -->
                  </el-form-item>
                </el-col>
              </el-form>
            </el-row>
          </div>
          <div class="container normal-panel normal-margin">
            <attachmentList :attinfos="form.attinfos" @onSuccess="querySubmitMain" :onlyread="store.state.pageOnlyRead">
              <upload slot="upload" :folderid="folderid"
                :bindData="{ ownertable: 'sat_submiteditmodel', ownerid: form.sat_submiteditmodelid, usetype: 'default' }"
                @onSuccess="onSubmit()"></upload>
            </attachmentList>
          </div>
        </el-col>
        <el-col :span="11">
          <scopeOfauth :defaultData="defaultData" appidname="sat_submiteditmodelid" obiectName="sat_submiteditmodel" :dataid="$route.query.id"
            @onChecked="onChecked"></scopeOfauth>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import attachmentList from '@/components/attachment_list/index.vue'
import upload from '@/components/upload/hw_obs_upload.vue'
import scopeOfauth from '@/components/scopeOfAuthority/index.vue'
import store from '@/store'
export default {
  components: {
    attachmentList,
    upload,
    scopeOfauth
  },
  data() {
    return {
      store,
      folderid: JSON.parse(sessionStorage.getItem('folderid')).appfolderid,
      defaultData: {},
      rules: {
        title: [
          { required: true, message: '请输入课程名称', trigger: 'blur' },
        ],
        begdate: [
          { required: true, message: '请选择生效日期', trigger: 'change' }
        ],
        enddate: [
          { required: true, message: '请选择截止日期', trigger: 'change' }
        ],
        onceonly: [
          { required: true, message: '请选择版块', trigger: 'change' }
        ],
        notes: [
          { required: true, message: '请输入提报要求', trigger: 'blur' },
        ],
        type: [
          { type: 'array', required: true, message: '请至少选择一个提报形式', trigger: 'change' }
        ],
      },
      form: {
        "title": "",
        "notes": "",
        "istextrequired": 1,
        "isattrequired": 1,
        "onceonly": 1,
        "sat_submiteditmodelid": "0",
        "begdate": "",
        "enddate": "",
        "type": []
      }
    }
  },
  methods: {
    async querySubmitMain() {
      const res = await this.$api.requested({
        "classname": "webmanage.saletool.submitedit.submitedit",
        "method": "select_submitdetailed",
        "content": {
          "sat_submiteditmodelid": this.$route.query.id
        }
      })
      res.data.notes == '初始化数据' + JSON.parse(sessionStorage.getItem('active_account')).userid ? res.data.notes = '' : '';
      this.form = Object.assign({}, this.form, res.data)
      if (this.form.isattrequired === 1) { this.form.type.push('附件提报') }
      if (this.form.istextrequired === 1) { this.form.type.push('文字提报') }

      this.form.status !== '新建' ? store.state.pageOnlyRead = true : store.state.pageOnlyRead = false
    },
    onSubmit(callback) {
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false;
        const res = await this.$api.requested({
          "classname": "webmanage.saletool.submitedit.submitedit",
          "method": "insertorupdate",
          "content": this.form
        })
        this.insertCoursewareauth()
        callback?callback(res):''
      })
    },
    // 监听提报形式
    onCheckboxChange(val) {
      this.form.isattrequired = val.indexOf('附件提报') === -1 ? 0 : 1
      this.form.istextrequired = val.indexOf('文字提报') === -1 ? 0 : 1
    },
    // 获取保存授权范围数据
    onChecked(param) {
      this.authData = param
      console.log(this.authData)
    },
    // 新增授权范围
    async insertCoursewareauth() {
      const res = await this.$api.requested({
        "classname": "webmanage.saletool.submitedit.submiteditauth",
        "method": "insertOrUpdate",
        "content": this.authData
      })
      res.code === 0 ? this.$notify({
        title: '失败',
        message: res.data,
        type: 'error'
      }) : this.querySubmitMain()

    },
    // 查询授权信息
    async query_auth() {
      const res = await this.$api.requested({
        "classname": "webmanage.saletool.submitedit.submiteditauth",
        "method": "selectAuth",
        "content": {
          "sat_submiteditmodelid": this.$route.query.id,
        }
      })
      this.defaultData = res.data
    },

  },
  created() {
    this.querySubmitMain()
    this.query_auth()
  },
}

</script>
<style>
</style>