<template>
  <!-- 模板节点信息编辑弹窗 -->
  <div class="examination node_edit_wrap">
    <el-dialog
      @close="closeDialog"
      :visible.sync="nodeShowFlag"
      customClass="dialog_wrap"
      :close-on-click-modal="false"
    >
      <div slot="title" class="titleBox">
        <i
          :class="tableType === 0 ? 'el-icon-document' : 'el-icon-edit-outline'"
          style="font-size: 20px"
        ></i>
        <span style="margin-left: 10px; font-size: 18px">{{
          type
        }}</span>
      </div>
      <el-form
        ref="editData"
        label-width="85px"
        :inline="true"
        :model="editData"
        :rules="formRules"
      >
        <el-form-item prop="isoItemName" label="项目名称">
          <el-input
            v-model="editData.isoItemName"
            clearable
            size="small"
            style="width: 160px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item prop="dataTypeCode" label="数据类型">
          <el-select
            v-model="editData.dataTypeCode"
            filterable
            style="width: 160px"
            size="small"
            @change="typeChange(editData.dataTypeCode, 'dataTypeName')"
          >
            <el-option
              v-for="item in dataTypeList"
              :key="item.baseCode"
              :label="item.baseName"
              :value="item.baseCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="isCtrl" label="是否控制">
          <el-select
            v-model="editData.isCtrl"
            filterable
            style="width: 160px"
            size="small"
          >
            <el-option
              v-for="item in controlTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="isoItemDesc" label="项目描述">
          <el-input
            v-model="editData.isoItemDesc"
            clearable
            size="small"
            style="width: 160px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item prop="doOrder" label="执行顺序">
          <el-input
            v-model="editData.doOrder"
            clearable
            size="small"
            style="width: 160px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item prop="isCycl" label="是否循环">
          <el-select
            v-model="editData.isCycl"
            filterable
            style="width: 160px"
            size="small"
          >
            <el-option
              v-for="item in controlTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="itemSit" label="项目位置">
          <el-input
            v-model="editData.itemSit"
            clearable
            size="small"
            style="width: 160px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item prop="valueSit" label="默认值">
          <el-input
            v-model="editData.valueSit"
            clearable
            size="small"
            style="width: 160px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item prop="doValue" label="签名填入值">
          <el-input
            v-model="editData.doValue"
            clearable
            size="small"
            style="width: 160px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item prop="optNo" label="执行岗位">
            <el-select v-model="editData.optNo" filterable clearable collapse-tags size="small" style="width: 160px;">
              <el-option v-for="(item,index) in testPostNameType" :key="index" :label="item.postName"
                :value="item.postNo">
              </el-option>
            </el-select>
           <!-- <el-input
            v-model="editData.optNo"
            clearable
            size="small"
            style="width: 160px"
            placeholder="请输入"
          ></el-input> -->
        </el-form-item>
        <el-form-item prop="isSyn" label="是否同步">
          <el-select
            v-model="editData.isSyn"
            filterable
            style="width: 160px"
            size="small"
          >
            <el-option
              v-for="item in controlTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="adjustValue" label="签名时间填入">
          <el-input
            v-model="editData.adjustValue"
            clearable
            size="small"
            style="width: 160px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item prop="memo1" label="通知人账号">
          <el-input
            v-model="editData.memo1"
            clearable
            size="small"
            style="width: 160px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item prop="memo" label="备注">
          <el-input
            v-model="editData.memo"
            clearable
            size="small"
            style="width: 160px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item prop="isMsg" label="是否推送">
          <el-select
            v-model="editData.isMsg"
            filterable
            style="width: 160px"
            size="small"
          >
            <el-option
              v-for="item in controlTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文件上传">
          <el-input
            v-model="editData.filePath"
            size="small"
            readonly
            disabled
            style="width: 352px;display: inline-block;"
          ></el-input>
          <el-upload
            class="upload"
            action=""
            v-if="editData.filePath - 0 === 0"
            style="width: 40px;display: inline-block;"
            :multiple="false"
            :show-file-list="false"
            accept=""
            :http-request="httpRequest"
          >
            <el-button
              type="goon"
              size="mini"
              icon="el-icon-upload2"
            ></el-button>
          </el-upload>
          <el-button
            v-else
            type="primary"
            size="mini"
            class="el-icon-circle-close"
            @click="dropFile"
          ></el-button>
        </el-form-item>
        <el-form-item prop="isAuto" label="是否自动">
          <el-select
            v-model="editData.isAuto"
            filterable
            style="width: 160px"
            size="small"
          >
            <el-option
              v-for="item in controlTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="saveData">确定</el-button>
        <el-button size="mini" type="default" @click="closeDialog"
          >取消</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getCookie
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  props: ['type', 'nodeShowFlag', 'recordData'],
  data () {
    return {
      // 编辑的数据对象
      // 编辑的数据对象
      formRules: {
        isoItemName: [{required: true, message: '该项不能为空', trigger: 'input'}],
        dataTypeCode: [{required: true, message: '该项不能为空', trigger: 'input'}],
        doOrder: [{required: true, message: '该项不能为空', trigger: 'input'}]
      },
      editData: {
        memo: '',
        memo1: '',
        isoNo: '',
        filePath: '',
        isCtrl: '0',
        isCycl: '0',
        isSyn: '0',
        isMsg: '0',
        isAuto: '0',
        dataTypeCode:''
      },
      editDataCache: {
        memo: '',
        isoNo: '',
        filePath: '',
        isCtrl: '0',
        isCycl: '0',
        isSyn: '0',
        isMsg: '0',
        isAuto: '0',
        dataTypeCode:''
      },
      tableType: 0, // 当前是编辑还是新增， 0为新增
      controlTypeList: [
        {
          value: '0',
          label: '否'
        },
        {
          value: '1',
          label: '是'
        }
      ],
      dataTypeList: [],
      testPostNameType: []
    }
  },
  created () {
    this.axios
        .get(
          "pass/baseManagement/v1/limsbaseinfos/like/?pageNum=1&pageSize=50&baseCode=4825",
          {},
          { individualType: "json" }
        )
        .then((response) => {
          if (response.data && response.data.list.length > 0) {
            this.dataTypeList = response.data.list.filter(item => item.baseCode != '4825');
          }
        });
  },
  async mounted () {
    this.gettestPostNameTypeList ()
  },
  methods: {
    getCode (code, arr, codeKey, nameKey) {
      if (!code) {
        return '';
      }
      let targetRow = arr.find(item => {
        return item[codeKey] === code;
      });
      // console.log(targetRow, 'targetRow');
      return targetRow ? targetRow[nameKey] : '';
    },
    gettestPostNameTypeList () {
          let obj ={
        'object':{validFlag:1},
        'pageIndex':1,
        'pageSize':99999
      }
        this.axios.post('pass/baseManagement/v1/limsbaseposts/queryLimsBasePostPage/',obj, {individualType: 'json'}).then(res => {
              this.testPostNameType = res.data.list;
            })
      },
    // 关闭弹窗
    closeDialog () {
      this.$emit('close')
    },
    // 保存数据
    async saveData () {
      // if (this.editData.filePath - 0 === 0) {
      //   return this.$message.warning('请上传文件')
      // }
      if (!this.editData.isoNo || this.editData.isoNo - 0 === '') {
        return this.$message.warning('请先选择要新增节点的模板信息')
      }
      this.$refs.editData.validate(async valid => {
        if (valid) {
          const data = await this.axios.post('pass/baseManagement/v1/limsisotmpds/insertupdate', { ...this.editData }, { individualType: 'json' })
          if (data.status === 'succeed') {
            this.$message.success('操作成功')
            this.closeDialog()
          } else {
            this.$message.error('操作异常请重试')
          }
        }
      })
    },
    // 删除已有文件
    async dropFile () {
      let result = await this.MessageBox.confirm('确认删除该文件重新上传吗？').catch(() => 'cancel')
      if (result == 'cancel') return
       try {
         const data = await this.axios.post('pass/baseManagement/v1/limsisotmpds/delectFile', {stId : this.editData.id}, { individualType: 'json' })
          if (data.status === 'succeed') {
            this.editData.filePath = ''
            this.$message.success('文件删除成功')
          } else {
            this.$message.error(data.message)
          }
      } catch (e) {
        this.$message.error('文件删除失败，请重试')
        console.log(e)
      }
    },
    typeChange (val, valName) {
      if (val) {
        let item = this.dataTypeList.find(item => item.baseCode === val)
        if (item) {
          this.editData[valName] = item.baseName
        }
      } else {
        this.editData[valName] = ''
      }
    },
    // 文件上传
    async httpRequest (e) {
      let _this = this;
      if (!e.file) {
        // 没有文件
        return false
      }
      let file = new FormData()
      file.append('file', e.file)
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
      this.axios.post('pass/baseManagement/v1/file/upload', file, config).then(res =>{
        if (res.code === '0' || res.code === '200') {
          this.$message.success('上传成功')
          this.editData.filePath = res.data[0].fileName
        } else {
          this.$message.error(res.message)
        }
      }).catch(e => {
        return this.$message.error(e.message)
      })
    },
  },
  watch: {
    recordData (newVal, oldVal) {
      this.editData = { ...this.editDataCache, ...newVal }
      this.editData.dataTypeCode = this.getCode(this.editData.dataTypeName,this.dataTypeList,'baseName','baseCode')
    }
  }
}
</script>
<style>
.batch-edit-wrap .el-icon-close {
  display: none;
}

.batch-edit-wrap .el-dialog__header {
  padding-top: 0px;
}

.batch-edit-wrap .el-select .el-input__inner {
  padding-right: 25px !important;
}

.node_edit_wrap .dialog_wrap {
  width: 860px !important;
}

.dialog_wrap .el-dialog__footer {
  text-align:  center;
}
</style>