<template>
  <div class="app-container">

    <el-form ref="form" :model="form" label-width="180px">
        <el-form-item label="每日自动同步：">
          <el-row>
            <el-col :span="20">
              <el-switch v-model="form.delivery" :disabled="form.cmo == ''" active-value="1" inactive-value="0" active-color="#169bd5" inactive-color="#8c8c8c" @change="changeEnable"></el-switch>
            </el-col>
            <el-col :span="4">
             <el-select v-model="form.cmo" placeholder="已对接公司" @change="change_company">
              <el-option  v-for="(item,index) in companyArr" :key="index" :label="item.company" :value="item.company"/>
            </el-select>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="同步内容：">
          上传数据
          <el-radio-group v-model="form.type1" @change="changeEnable">
            <el-radio v-for="item in autoArr3"  :disabled="form.cmo == ''" :key="item.id" :label="item.id" :value="item.name">{{ item.name }}</el-radio>
          </el-radio-group>
          <el-checkbox-group v-model="form.type2" @change="change_auto1('automatic','0',form.type2,myautoArr2)">
            <el-checkbox v-for="(item,index) in autoArr2" :key="index" :label="item.name" :value="item.name">{{ item.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-divider></el-divider>
        <el-form-item label="手动同步：">
          <el-button class="successBorder" :disabled="form.cmo == ''" @click="handleSyncNow">立刻同步</el-button>
           <el-date-picker v-model="form.date" :clearable="false"  :disabled="form.cmo == ''" style="margin-left: 10px;" type="date" :placeholder="$t('dailyExecutionPlan.xzrq')"  > </el-date-picker>
        </el-form-item>
        <el-form-item label="同步内容：">
          下载数据
          <el-checkbox-group v-model="form.type3" @change="change_auto1('manual','1',form.type3,myautoArr3)">
              <el-checkbox v-for="(item,index) in autoArr1" :key="index" :label="item.name" :value="item.name">{{ item.name }}</el-checkbox>
          </el-checkbox-group>
          上传数据
          <el-checkbox-group v-model="form.type4" @change="change_auto1('manual','0',form.type4,myautoArr2)">
            <el-checkbox v-for="(item,index) in autoArr2" :key="item.index" :label="item.name" :value="item.name">{{ item.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
  </div>

</template>

<script>
  import { parseTime } from '@/utils/index.js'
import { postJson,checkButtons } from '@/api/common'
import Cookies from 'js-cookie'
import Pagination from '@/components/Pagination'
export default {
  name: 'DataSynchronization',
  components: { Pagination },
  data() {
    return {
      isRoleEdit: [],
      companyArr: [
        // { id: '公司1', name: '公司1' },
        // { id: '公司2', name: '公司2' },
        // { id: '公司3', name: '公司3' }
      ],
      autoArr1: [
        // { id: '栏舍', name: '栏舍' },
        // { id: '配方', name: '配方' },
        // { id: '饲料', name: '饲料' },
        // { id: '饲料分类', name: '饲料分类' },
        // { id: '牲畜类别', name: this.$t('errorAnalysis.sxlb') },
        // { id: '驾驶员', name: '驾驶员' }
      ],
      autoArr2: [
        // { id: '栏舍', name: '栏舍' },
        // { id: '配方', name: '配方' },
        // { id: '搅拌数据', name: '饲料' },
        // { id: '发料数据', name: '饲料分类' },
        // { id: '剩料数据', name: this.$t('errorAnalysis.sxlb') },
        // { id: '干物质信息', name: '干物质信息' },
        // { id: '滨州筛', name: '滨州筛' },
        // { id: '粪便筛', name: '粪便筛' },
        // { id: '计划完成车次', name: '计划完成车次' },
        // { id: '实际完成车次', name: '实际完成车次' },
        // { id: '计划重量', name: '计划重量' },
        // { id: '完成重量', name: '完成重量' },
        // { id: '准确率', name: '准确率' },
      ],
      myautoArr2:[],
      autoArr3: [
        { id: '0', name: '每车次结束' },
        { id: '1', name: '每班次结束' },
        { id: '2', name: '每天计划完成后' }
      ],

      time1:"",
      time2:"",
      time3:"",
       form: {
         cmo:'',
          name: '',
          region: '',
          date1: '',
          date2: '',
          delivery: '',
          type1: '',
          type2: [],
          type3: [],
          type4: [],
          resource: '',
          desc: '',
          date:parseTime(new Date(), '{y}-{m}-{d}')
        },


      table: {
        getdataListParm: {
          name: 'getPastureList',
          page: 1,
          offset: 1,
          pagecount: parseInt(Cookies.get('pageCount')),
          returntype: 'Map',
          parammaps: {
            'pastureid': Cookies.get('pastureid'),
            'phone': '',
            'contact': '',
            'address': '',
            'pastureName': '',
            'enable': ''
          }
        },
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        temp: {}
      },
      requestParam: {},
      arrowDown: true,
      arrowUp: false,
      rowStyle: { maxHeight: 30 + 'px', height: 30 + 'px' },
      cellStyle: { padding: 0 + 'px' }

    }
  },

  created() {
    this.getCompanyList()
    this.getButtons()
  },
  mounted() {

  },
  methods: {
    getButtons() {
      const Edit = 'RanchInformation'
      const isRoleEdit = checkButtons(JSON.parse(sessionStorage.getItem('buttons')), Edit)
      this.isRoleEdit = isRoleEdit
    },

    onSubmit() {
        console.log('submit!');
      },
    handleEnableChange() {
      console.log('点击了是否启用')
    },
    handleConfirmStartChange() {
      console.log('点击了确认开始')
    },

    change_time1(){
      console.log(this.time1)
    },

    change_select(){
      console.log(this.time1)
    },
    getCompanyList(){
      const url = 'authdata/GetDataByName'
      const data = {}
      data.name = 'getCompanyName'
      postJson(url, data).then(response => {
        console.log('公司数据', response.data.list)
        if (response.data.list !== null) {
          this.companyArr = response.data.list
        } else {
          this.companyArr = []
        }
      })
    },
    change_company(item){
      console.log(item)
      this.getUploadCheckedList()
      this.getDownloadCheckedList()
      this.getUploadRadioCheckedList()
    },
    getUploadCheckedList(){
      const url = 'authdata/GetDataByName'
      const data = {}
      data.name = 'getScheduledUpByCompany'
      data.parammaps = {}
      data.parammaps.company = this.form.cmo
      postJson(url, data).then(response => {
        if (response.data.list !== null) {
          this.form.type2 = []
          this.autoArr2 = []
          this.myautoArr2 = []
          for(let i=0;i<response.data.list.length;i++){
            var obj={}
            obj.name = response.data.list[i].sname
            this.autoArr2.push(obj)
            this.myautoArr2.push(response.data.list[i])
            if(response.data.list[i].automatic== 1){
              this.form.type2.push(response.data.list[i].sname)
            }
            if(response.data.list[i].manual== 1){
              this.form.type4.push(response.data.list[i].sname)
            }
          }
        } else {
          this.companyArr = []
        }
      })
    },
    getDownloadCheckedList(){
      const url = 'authdata/GetDataByName'
      const data = {}
      data.name = 'getScheduledDownByCompany'
      data.parammaps = {}
      data.parammaps.company = this.form.cmo
      postJson(url, data).then(response => {
        if (response.data.list !== null) {
          this.form.type3 = []
          this.autoArr1 = []
          this.myautoArr3 = []
          for(let i=0;i<response.data.list.length;i++){
            var obj={}
            obj.name = response.data.list[i].sname
            this.autoArr1.push(obj)
            this.myautoArr3.push(response.data.list[i])
            if(response.data.list[i].manual== 1){
              this.form.type3.push(response.data.list[i].sname)
            }
          }
          console.log('this.form.type3',this.form.type3)
          console.log('this.autoArr1',this.autoArr1)
        } else {
          this.companyArr = []
        }
      })
    },
    getUploadRadioCheckedList(){
      const url = 'authdata/GetDataByName'
      const data = {}
      data.name = 'getScheduledUpStatusByCompany'
      data.parammaps = {}
      data.parammaps.company = this.form.cmo
      postJson(url, data).then(response => {
        if (response.data.list !== null) {
          this.form.type1 = response.data.list[0].times
          this.form.delivery = response.data.list[0].enable
        } else {
          this.form.type1 = ''
          this.form.delivery = ''
        }
      })
    },
    changeEnable(){
      const url = 'authdata/GetDataByName'
      const data = {}
      data.name = 'updateScheduledStatus'
      data.parammaps = {}
      data.parammaps.company = this.form.cmo
      data.parammaps.enable = this.form.delivery
      data.parammaps.times = this.form.type1
      postJson(url, data).then(response => {
        if (response.msg == 'ok') {
          this.$notify({ title: this.$t('common.succes'), message: this.$t('driver.saveSuccess'), type: 'success',  duration: 2000 })
        } else {
          this.$notify({ title:this.$t('message.msg37'), message: '保存失败', type: 'success',  duration: 2000 })
        }
      })
    },
    change_auto1(item,type,ids,list) {
      console.log(ids,'5555555555')
      // console.log(this.myautoArr2)
      var myIds = []
      for(let i=0;i<list.length;i++){
        for(let j=0;j<ids.length;j++){
          if(list[i].sname == ids[j]){
            myIds.push(list[i].scheduledid)
          }
        }
      }
      const url = 'authdata/scheduled/status/edit'
      const data = {}
      data.company = this.form.cmo
      data.id = myIds
      if(item == 'automatic'){
         data.automatic = '1'
      }else{
        data.manual = '1'
      }
      console.log('myIds',myIds)
      data.type = type
      postJson(url, data).then(response => {
        if (response.msg == 'ok') {
          this.$notify({ title: this.$t('common.succes'), message: this.$t('driver.saveSuccess'), type: 'success',  duration: 2000 })
        } else {
          this.$notify({ title:this.$t('message.msg37'), message: '保存失败', type: 'success',  duration: 2000 })
        }
      })
    },
    handleSyncNow() {
      const url = 'authdata/synchronizeNow'
      const data = {}
      data.company = this.form.cmo
      data.date = parseTime(this.form.date, '{y}-{m}-{d}')
      postJson(url, data).then(response => {
        if (response.msg == 'ok') {
          this.$notify({ title: this.$t('common.succes'), message: this.$t('driver.saveSuccess'), type: 'success',  duration: 2000 })
        } else {
          this.$notify({ title:this.$t('message.msg37'), message: '保存失败', type: 'success',  duration: 2000 })
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
  .search{margin-top:10px;}
