<template>
  <div>
    <xy-dialog ref="dialog" :width="60" :is-show.sync="isShow" :type="'form'" :title="'数据填报'" :form="form"
      :rules='rules' @submit="submit">
      <template v-slot:business_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>业务类型：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="business_name" disabled placeholder="请输入业务名称" style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:area_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>填报区域：
          </div>
          <div class="xy-table-item-content">
            <el-select style="width:100%" @change="changeArea" :disabled="(stateObj.is_admin)?false:true" v-model="form.area_id" placeholder="请选择">
              <el-option v-for="item in list_areas" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:year>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>填报年份：
          </div>
          <div class="xy-table-item-content">
            <el-date-picker style="width:100%" @change="changeYear" format="yyyy" value-format="yyyy" v-model="form.year" type="year"
              placeholder="选择年">
            </el-date-picker>
          </div>
        </div>
      </template>
      <template v-slot:month>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>填报月份：
          </div>
          <div class="xy-table-item-content">
            <el-date-picker style="width:100%" format="MM月" value-format="MM"  v-model="form.month" type="month" placeholder="选择月">
            </el-date-picker>
          </div>
        </div>
      </template>
      <template v-slot:year_datas v-if="form.year_datas.length>0">
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>年度预算：
          </div>
          <div class="xy-table-item-content">
            <xy-table style="width:850px" :list="form.year_datas" :isPage="false" :height="150"
              :table-item="year_datas_item">
              <template v-slot:btns>
                <div></div>
              </template>
            </xy-table>
          </div>
        </div>
      </template>
      <template v-slot:id_fill_datas_fill_id_relation>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>数据填报：
          </div>
          <div class="xy-table-item-content">
            <!-- <el-button type="primary" style="margin-bottom:10px" size="small" @click="addRow">新增</el-button> -->
            <xy-table style="width:850px" :list="fill_datas" :isPage="false" :height="550"
              :table-item="fill_datas_item">
              <template v-slot:business_type_id>
                <el-table-column align='left' label="业务类别" width="320">
                  <template slot-scope="scope">
                    <template v-for="item in list_types">
                      <div v-if="item.id === scope.row.business_type_id">
                        {{item.name}}
                      </div>
                    </template>
                  </template>
                </el-table-column>
              </template>
              <template v-slot:business_data_id>
                <el-table-column align='left' label="业务数据" width="320">
                  <template slot-scope="scope">
                      <template v-for="item in list_datas">
                        <div v-if="item.id === scope.row.business_data_id">
                          {{item.name}}
                        </div>
                      </template>
                  </template>
                </el-table-column>
              </template>
              <template v-slot:value>
                <el-table-column align='left' label="数据值" width="160">
                  <template slot-scope="scope">
                      <el-input v-model="scope.row.value" style="width:100%"></el-input>
                  </template>
                </el-table-column>
              </template>
              <template v-slot:btns>
                <div></div>
              </template>
            </xy-table>
          </div>
        </div>
      </template>



    </xy-dialog>

  </div>
</template>

<script>
  import {
    save,
    show,
    index,
    destroy
  } from "@/api/system/baseForm.js"
  import state from '@/store/modules/user.js'
  export default {
    components: {

    },
    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',
        stateObj: {},
        business_name: '', //业务名
        business_id: '', // 业务id
        area_id: '',
        table_name: 'fills',
        list_areas: [],
        list_types: [],
        list_datas: [],
        year_datas:[],
        year_datas_item:[{
          prop: 'year',
          label: '年份',
          align: 'center',
          width: 100
        },{
          prop: 'all_year',
          label: '年度总预算',
          align: 'center',
          width: 150
        },{
          prop: 'quarter_1',
          label: '第一季度预算',
          align: 'center',
          width: 150
        },{
          prop: 'quarter_2',
          label: '第二季度预算',
          align: 'center',
          width: 150
        },{
          prop: 'quarter_3',
          label: '第三季度预算',
          align: 'center',
          width: 150
        },{
          prop: 'quarter_4',
          label: '第四季度预算',
          align: 'center',
          width: 150
        }],
        // 填报数据
        fill_datas:[],
        fill_datas_item: [{
          type: 'index',
          width: 50,
        }, {
          prop: 'business_type_id',
          label: '填报类别',
          align: 'left',
          width: 620
        }, {
          prop: 'business_data_id',
          label: '填报数据',
          align: 'left',
          width: 620
        }, {
          prop: 'value',
          label: '填报值',
          align: 'left',
          width: 620
        }],
        form: {
          business_id: '',
          area_id: '',
          year: '',
          month: '',
          year_datas:[],
          id_fill_datas_fill_id_relation: []
        },
        rules: {
          business_id: [{
            required: true,
            message: '请选择业务'
          }],
          area_id: [{
            required: true,
            message: '请选择区域'
          }],
          year: [{
            required: true,
            message: '请选择年份'
          }],
          month: [{
            required: true,
            message: '请选择月份'
          }]
        }
      }
    },
    created() {
      this.stateObj = state.state

    },
    methods: {
      //  获取 业务类型数据
      setBusinessId(id){
        this.form.business_id = id
      },
      async getBusiness() {
        const res = await show({
          id: this.business_id,
          table_name: 'businesses',
          json_data_fields: ['area_ids'],
        })
        this.business_name = res.name
        this.list_areas = res.area_ids_details ? res.area_ids_details : []
        this.list_types = res.id_business_types_business_id_relation ? res.id_business_types_business_id_relation : []
        if(res.id_business_datas_business_id_relation){
          this.list_datas = res.id_business_datas_business_id_relation.sort((a, b) => parseInt(a.sort) - parseInt(b.sort))
        }
        // this.list_datas = res.id_business_datas_business_id_relation ? res.id_business_datas_business_id_relation : []
        // 区域年度预算
        this.year_datas = res.id_budgets_business_id_relation
        this.form.year_datas = res.id_budgets_business_id_relation.filter(item=>{return item.area_id===this.form.area_id && item.year==this.form.year})

        //  初始化 填报数据
        if(this.type==='add'){
          this.form.area_id = this.stateObj.area_id ? this.stateObj.area_id : ''
          let _arr = []

          this.list_types.map(item=>{
            this.list_datas.map(item1=>{
              if(item.area_id===this.stateObj.area_id){
                _arr.push({
                  business_id: this.business_id,
                  year: this.$moment(new Date()).format("YYYY"),
                  month: this.$moment(new Date()).format("MM"),
                  area_id: this.area_id,
                  business_type_id: item.id,
                  business_data_id: item1.id,
                  value: 0,
                  sort:item1.sort
                })
              }
            })
          })
          console.log("_arr",_arr)
          this.fill_datas = _arr.sort()
        }
      },
      changeYear(e){
        // 区域年度预算
        console.log(e,this.year_datas)
        this.form.year_datas = this.year_datas.filter(item=>{return item.area_id===this.form.area_id && item.year==e})

      },
      changeArea(e){
        this.form.area_id = e ? e : ''
        let _arr = []
        this.list_types.map(item=>{
          this.list_datas.map(item1=>{
            if(item.area_id===e){
              _arr.push({
                business_id: this.business_id,
                year: this.$moment(new Date()).format("YYYY"),
                month: this.$moment(new Date()).format("MM"),
                area_id: e,
                business_type_id: item.id,
                business_data_id: item1.id,
                value: 0,
                sort:item1.sort
              })
            }
          })
        })
        this.fill_datas = _arr

        this.form.year_datas = this.year_datas.filter(item=>{return item.area_id === e && item.year===this.form.year})
        console.log("this.form.year_datas",e,this.year_datas,this.form.year_datas)
      },


      submit() {
        if (this.id) {
          this.form.id = this.id
        }
        if (this.type == 'add') {
          this.form.id = ''
        }
        this.fill_datas.map(item=>{
          item.year = this.form.year
          item.month = this.form.month
          item.area_id = this.form.area_id
        })
        this.form.id_fill_datas_fill_id_relation = this.fill_datas
        console.log("this.form", this.form)
        // return
        // // 判断 填报数据是否有 为空的数据
        // if (this.form.id_fill_datas_fill_id_relation.length > 0) {
        //   // 判断属性中是否为空
        //   for (var p of this.form.id_fill_datas_fill_id_relation) {
        //     if (this.base.isNull(p.business_type_id)) {
        //       this.$message({
        //         type: 'warning',
        //         message: `业务类别不能为空`
        //       })
        //       return
        //     }
        //     if (this.base.isNull(p.business_data_id)) {
        //       this.$message({
        //         type: 'warning',
        //         message: `填报数据不能为空`
        //       })
        //       return
        //     }
        //   }
        // }
        // return
        save({
          table_name: this.table_name,
          ...this.form
        }).then(res => {
          this.$message({
            type: 'success',
            message: this.type === 'add' ? '新增成功' : '编辑成功'
          })
          this.isShow = false
          this.$emit('refresh')
        })
      },

      getDetail() {
        show({
          id: this.id,
          table_name: this.table_name,
        }).then(res => {
          this.form = this.base.requestToForm(res, this.form)
          this.business_id = res.business_id
          this.form.area_id = res.area_id ? res.area_id : [],
          this.form.id_fill_datas_fill_id_relation = res.id_fill_datas_fill_id_relation ? res
            .id_fill_datas_fill_id_relation : []
          this.fill_datas = this.form.id_fill_datas_fill_id_relation
        })
      },
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          this.getBusiness()
          if (this.type === 'editor') {
            this.getDetail()
          }else{
            this.form.year = this.$moment(new Date).format("YYYY")
            this.form.month = this.$moment(new Date).format("MM")
          }
        } else {
          this.id = ''
          this.business_id = ''
          this.business_name = ''
          this.list_areas = []
          this.list_types = []
          this.list_datas = []
          this.year_datas = []
          this.type = "add"
          this.form = {
            business_id: '',
            area_id: '',
            year: '',
            month: '',
            year_datas:[],
            id_fill_datas_fill_id_relation: []
          }
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">
  // ::v-deep .area_ids,
  // ::v-deep .name{
  //   flex-basis: 100%;
  // }
</style>
