<template>
  <!--三时纪-缓存站点-->
  <div>
    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span class="role-span">输送系统：</span>
      </div>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="78px">
        <el-row>
          <el-col :span="8">
            <el-tooltip class="item" effect="dark" content="设备为输送机或者NDC系统时需为数字" placement="bottom-end">
              <el-form-item label="电气调度号" label-width="150px">
                <el-input v-model="form.address" />
              </el-form-item>
            </el-tooltip>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span class="role-span">指令相关：</span>
      </div>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="78px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="检验有货">
              <el-switch v-model="form.inspect_in_stocck" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="忽视取货校验" label-width="150px">
              <el-switch v-model="form.ignore_pickup_check" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="忽视放货校验" label-width="150px">
              <el-switch v-model="form.ignore_release_check" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="呼叫">
              <el-switch v-model="form.apply_task" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="响应" label-width="150px">
              <el-switch v-model="form.manual_create_task" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="自动创建任务" label-width="150px">
              <el-switch v-model="form.auto_create_task" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-tooltip class="item" effect="dark" content="创建设备点位记录有无货信号" placement="bottom-end">
            <el-form-item label="站点管理" label-width="150px">
              <el-switch v-model="form.station_manager" />
            </el-form-item>
          </el-tooltip>
          <el-col :span="8">
            <el-form-item label="清除" label-width="80px">
              <el-switch v-model="form.auto_clean_task" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否输入物料" label-width="150px">
              <el-switch v-model="form.input_material" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="关联三色灯" prop="device_code" label-width="100px">
              <el-select
                v-model="form.link_three_lamp"
                filterable
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in deviceList"
                  :key="item.device_code"
                  :label="item.device_name"
                  :value="item.device_code"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span class="role-span">AGV相关：</span>
      </div>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="78px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="取货">
              <el-switch v-model="form.is_pickup" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="放货">
              <el-switch v-model="form.is_release" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span class="role-span" />
        <el-button
          :loading="configLoading"
          icon="el-icon-check"
          size="mini"
          style="float: right; padding: 6px 9px"
          type="primary"
          @click="doSubmit"
        >保存
        </el-button>
      </div>
    </el-card>

  </div>
</template>

<script>
import { updateConfig, queryDriverConfig } from '@/api/acs/device/driverConfig'
import crud from '@/mixins/crud'
import deviceCrud from '@/api/acs/device/device'

export default {
  name: 'StandardOrdinarySite',
  mixins: [crud],
  props: {
    // eslint-disable-next-line vue/require-default-prop
    parentForm: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      device_code: '',
      device_id: '',
      configLoading: false,
      deviceList: [],
      address: '',
      form: {
        inspect_in_stocck: true,
        ignore_pickup_check: true,
        ignore_release_check: true,
        apply_task: true,
        link_three_lamp: '',
        manual_create_task: true,
        auto_create_task: true,
        is_pickup: true,
        is_release: true,
        station_manager: true,
        auto_clean_task: true,
        input_material: true
      },
      rules: {}
    }
  },
  created() {
    this.$nextTick(() => {
      // 从父表单获取设备编码
      this.device_code = this.$props.parentForm.device_code
      this.device_id = this.$props.parentForm.device_id
      queryDriverConfig(this.device_id, this.$props.parentForm.driver_code).then(data => {
        // 给表单赋值，并且属性不能为空
        if (data.form) {
          const arr = Object.keys(data.form)
          // 不为空
          if (arr.length > 0) {
            this.form = data.form
          }
        }
      })
      deviceCrud.selectDeviceList().then(data => {
        this.deviceList = data
      })
    })
  },
  methods: {
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.configLoading = true
          // 根据驱动类型判断是否为路由设备
          const parentForm = this.parentForm
          parentForm.is_route = false
          updateConfig(parentForm, this.form).then(res => {
            this.notify('保存成功', 'success')
            this.configLoading = false
          }).catch(err => {
            this.configLoading = false
            console.log(err.response.data.message)
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
