<template>
    <dialog-edit :visible.sync="dialogVisible"
                 :title="$t('common.ExpeditedOrder')"
                 :footerBtnShow="false"
                 :editModel="'V'"
                 :api="dialogApi"
                 :form="dialogForm"
                 :appendToBody="true"
                 center="center"
                 :width="'90%'"
                 :before-close="closeDialog">
        <!--表格数据-->
        <el-col>
            <el-tabs>
                <el-tab-pane :label="$t('common.SelectdExpeditedProcess')"></el-tab-pane>
            </el-tabs>

            <urgentOrderList :assetCodeList="assetCodeList" @listData="listDataFn" :dialogVisible="dialogVisible"></urgentOrderList>
        </el-col>

        <!--表单-->
        <el-col v-if="dialogVisible">
                <el-form :model="form" :rules="rules" ref="form">
                    <el-form-item :label="$t('common.ExpeditedProcess')" label-width="80px" prop="expeditedProcess">
                        <el-select class="form-item-class" v-model="form.expeditedProcess" :placeholder="$t('common.point')" @change="orderUrgentConfigChange()">
                            <el-option
                                    v-for="kv in expeditedProcessOptions"
                                    :key="kv.k"
                                    :label="kv.v"
                                    :value="kv.k">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
        </el-col>
      <div slot="footer" class="dialog-footer">
        <auth-button type="primary" size="medium" :label="$t('common.complete')" @click="selectedUrgentOrderDialog_conmit()"></auth-button>
        <el-button size="medium" @click="dialogVisible=false">{{$t('common.close')}}</el-button>
      </div>
    </dialog-edit>
</template>

<script>
import { DialogEdit, authButton } from 'newepoch-components'
import urgentOrderList from './urgentOrderList'

import tracemgrApi from '@/api/tracemgrApi.js'

export default {

  props: {
    visible: {
      type: Boolean
    },
    assetCodeList: {
      type: Array
    }
  },

  computed: {
    dialogVisible: {
      get: function () {
        return this.visible
      },
      set: function (val) {
        this.$emit('update:visible', val)
      }
    }
  },

  watch: {
    dialogVisible: {
      deep: true,
      handler: function () {
        if (this.dialogVisible) { // 页面初始化
          tracemgrApi['selectedUrgentOrderDialogApi'].getDictTypeCode().then(res => {
            this.expeditedProcessOptions = res.data
          })
        } else {
          this.form = {}
        }
      }
    }
  },

  name: 'selectedUrgentOrderDialog',
  components: {
    urgentOrderList,
    DialogEdit,
    authButton
  },

  data () {
    return {
      form: {},
      rules: { expeditedProcess: [{ required: true, message: this.$i18n.messages[this.$i18n.locale].common.expeditedProcessNotEmpty, trigger: 'change' }] },
      dialogApi: {},
      dialogForm: {},

      /* 加急流程下拉框数据 */
      expeditedProcessOptions: [],
      activeName: 'second',
      expeditedProcessValue: '',
      fullscreenLoading: false
    }
  },
  methods: {

    /**
      * 关闭弹窗
      */
    closeDialog () {
      this.dialogVisible = false
    },

    /**
     * 当加急流程变化后，给请求数据赋值
     */
    orderUrgentConfigChange () {
      let list = this.form.list
      if (list === undefined || list.length === 0) {
        return
      }

      let orderUrgentConfig = this.form.expeditedProcess
      for (let i = 0; i < list.length; i++) {
        this.form.list[i].orderUrgentConfigId = orderUrgentConfig
      }
    },

    /**
       * 接收列表数据
       * @param data
       */
    listDataFn (data) {
      this.form.list = data
    },

    /**
       * 保存操作
       */
    selectedUrgentOrderDialog_conmit () {
      this.$refs['form'].validate(result => {
        if (result) { // 验证通过，提交方法
          let data = this.form.list
          if (data === undefined || data.length === 0) {
            this.$message.error(this.$i18n.messages[this.$i18n.locale].baoLiWorkbenchOrderTrack.noDataSubmit)
            return
          }

          const loading = this.$loading({
            lock: true,
            text: this.$i18n.messages[this.$i18n.locale].baoLiWorkbenchOrderTrack.pleaseWait,
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          const timeout = setTimeout(() => {
            loading.close()
            this.$message.error(this.$i18n.messages[this.$i18n.locale].baoLiWorkbenchOrderTrack.serverBusy)
          }, 10000)

          let requestData = { listJson: JSON.stringify(data) }

          // 执行保存
          tracemgrApi['selectedUrgentOrderDialogApi'].orderUrgentSave(requestData).then(res => {
            loading.close()
            clearTimeout(timeout)
            if (res.code !== '0') { // 请求失败
              this.$message.error(this.$i18n.messages[this.$i18n.locale].baoLiWorkbenchOrderTrack.serverBusy)
            } else {
              if (res.data.code === -1) {
                this.$message.error(this.$i18n.messages[this.$i18n.locale].common.saveError)
              } else {
                this.$message.success(this.$i18n.messages[this.$i18n.locale].common.saveSuccess)
                this.closeDialog()
              }
            }
          })
        }
      })
    }
  }
}
</script>
