<template>
  <div>
    <xy-dialog ref="dialog" :width='40' :is-show.sync="isShow" type="form" :title="type==='add'?'新增巡查计划':'编辑巡查计划'" :form="form"
      :rules="rules" @submit="submit">
      <template v-slot:nianfen>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>年份：
          </div>
          <div class="xy-table-item-content">
            <el-date-picker style="width: 300px;" v-model="form.nianfen" type="year" placeholder="选择日期"
              value-format="yyyy">
            </el-date-picker>
          </div>
        </div>
      </template>
      <template v-slot:jianchaleixing>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>检查类型：
          </div>
          <div class="xy-table-item-content">
             <el-radio-group v-model="form.jianchaleixing">
               <el-radio :label="item.value" v-for="item in listCheck">{{item.value}}</el-radio>
             </el-radio-group>
          </div>
        </div>
      </template>
      <template v-slot:kaishiriqi>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>开始日期：
          </div>
          <div class="xy-table-item-content">
            <el-date-picker style="width: 300px;" v-model="form.kaishiriqi" type="date" placeholder="选择日期"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </div>
        </div>
      </template>
      <template v-slot:jieshuriqi>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>结束日期：
          </div>
          <div class="xy-table-item-content">
            <el-date-picker style="width: 300px;" v-model="form.jieshuriqi" type="date" placeholder="选择日期"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </div>
        </div>
      </template>
      <template v-slot:jihuashuoming>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>计划说明：
          </div>
          <div class="xy-table-item-content">
            <el-input type="textarea"  style="width: 300px;" v-model="form.jihuashuoming" ></el-input>
          </div>
        </div>
      </template>
      <template v-slot:fujian>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>附件：
          </div>
          <div class="xy-table-item-content">
            <el-upload :action="action" multiple class='upload-demo' :file-list="fileList" ref="pictureUpload"
              style="width:400px" :auto-upload="true" :data="uploadOther" :on-success="handlesuccess"
              :on-remove="handleRemove">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </div>
        </div>
      </template>

    </xy-dialog>

  </div>
</template>

<script>
  import {
    save,
    show
  } from "@/api/system/baseForm.js"
  import {getparameteritem} from "@/api/system/dictionary.js"
  import {
    Message
  } from 'element-ui'
  import {
    getToken
  } from '@/utils/auth'
  export default {

    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',
        tableName:'flood_checks',
        listCheck:[],
        fileList:[],
        uploadOther: {
          token: ""
        },
        action: `${process.env.VUE_APP_BASE_API}api/admin/upload-file`,
        form: {
          nianfen:'',
          jianchaleixing:'',
          kaishiriqi:'',
          jieshuriqi:'',
          jihuashuoming:'',
          fujian:''
        },
        rules: {

        },

      }
    },
    created() {
      this.uploadOther.token = getToken();
      this.loadCheck()
      this.form.nianfen = this.$moment().format('yyyy')
    },
    methods: {
      loadCheck(){
        getparameteritem("checkType").then(res=>{
          this.listCheck = res.detail
        })
      },
      handleRemove(file, fileList) {
        this.fileList = fileList
      },
      handlesuccess(response, file, fileList) {
        this.fileList = fileList
      },
      async getDetail() {
        const res = await show({
          id: this.id,
          table_name: this.tableName
        })
        this.$integrateData(this.form,res)
        if(res.fujian_upload_details.length>0){
          res.fujian_upload_details.map(item=>{
            this.fileList.push({
              url:item.url,
              id:item.id,
              name:item.original_name
            })
          })
        }
      },

      submit() {
        let _files = []
        if (this.fileList.length > 0) {
          for (var h of this.fileList) {
            if (h.response) {
              _files.push(h.response.id)
            } else {
              _files.push(h.id)
            }
          }
          this.form.fujian = _files
        } else {
          this.form.fujian = []
        }
        console.log("form",this.form)
        // return
        if (this.type === 'add') {
          save({
            table_name: this.tableName,
            ...this.form
          }).then(res => {
            Message({
              type: 'success',
              message: '新增成功'
            })
            this.$emit('refresh')
            this.isShow = false

          })
          return
        }
        if (this.type === 'editor') {
          save({
            id: this.id,
            table_name: this.tableName,
            ...this.form
          }).then(res => {
            Message({
              type: 'success',
              message: '编辑成功'
            })
            this.$emit('refresh')
            this.isShow = false
          })
        }
      },

    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.id = ''
          this.type = ''
          this.fileList=[]
          this.$refs['dialog'].reset()
        }
      }
    }

  }
</script>

<style scoped lang="scss">

</style>
