<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" >
        <div slot="title" class="titleBox">
        <i :class="tableType === 0? 'el-icon-document':'el-icon-edit-outline'" style="font-size: 20px"></i>
        <!-- <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/more.png' /> -->
        <span style="margin-left:10px;font-size:18px">{{tableTypeDsc}}</span>
        </div>
       <el-form ref="baseData" label-width="80px" :rules="ruleValidate" :model="baseData">
         <el-row>
          <el-col :span="8">
            <el-form-item prop="planDept" label="计划单位">
              <el-select
                v-model="baseData.planDept"
                clearable
                filterable
                style="width: 100%"
                collapse-tags
                size="small"
              >
                <el-option
                  v-for="item in companyNameType"
                  :key="item.orgCode"
                  :label="item.orgName"
                  :value="item.orgCode"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="planItem" label="计划项目">
              <el-input v-model="baseData.planItem" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="planManner" label="计划方式">
              <el-input v-model="baseData.planManner" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
           <el-col :span="8">
            <el-form-item prop="planMethod" label="计划方法">
              <el-input v-model="baseData.planMethod" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="planDate" label="开展时间">
              <el-date-picker
                v-model="baseData.planDate"
                type="date"
                placeholder="选择日期"
                size="small"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="planMan" label="参加人员">
              <el-input v-model="baseData.planMan" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="memo" label="备注">
              <el-input v-model="baseData.memo" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
       </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button type="goon" size="mini" icon="el-icon-check" @click="tableType === 0 ? addData():modifyData()">确定</el-button>
        <el-button size="mini" type="goon" icon="el-icon-remove-outline" @click="cancel">取消</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: ['showFlag2', 'Params1' ,"Params2"], //接收参数
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增质控计划',
            tableType: 0,
            updateData:{},
            companyNameType: [],
            baseData: {
              planNo: '',
              planDept: '',
              planItem: '',
              planManner: '',
              planMethod: '',
              planMan: '',
              planDate: new Date(),
              memo: '',
              validFlag: '',
              createMan: '',
              createTime: '',
              createNo: ''
            },
            ruleValidate: {
              // merchName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
             }
        }
    },
    mounted () {
        //获取公司
        this.axios
        .post(
            "pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",
            {},
            { individualType: "json" }
        )
        .then((response) => {
            this.companyNameType = response.data;
        });
    },
    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] : "";
    },
      cancel () {
        let _this = this;
        _this.baseData = {
              
        }
        _this.dialogTableVisible = false
      },
      // TODO addData
      addData () {
        let _this = this;
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
            this.baseData.validFlag = '1';
            this.baseData.createTime = new Date();
            this.baseData.createMan = userName;
            this.baseData.createNo  = userId;
             _this.axios.post('pass/baseManagement/v1/limsisoplands/', this.baseData, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                 this.$message.success('添加成功')
                 _this.dialogTableVisible = false;
                 _this.$emit('refresh2')
                } else {
                 this.$message.error(res.message)
                }
                }).catch(e => {
                 this.$message.error(e.message)
               })
         }
       })
      },
      // TODO modifyData
      modifyData () {
        let _this = this;
        _this.$refs['baseData'].validate((valid) => {
        if (valid) {
          _this.updateData.planDept = this.baseData.planDept;
          _this.updateData.planItem = this.baseData.planItem;
          _this.updateData.planManner = this.baseData.planManner;
          _this.updateData.planMethod = this.baseData.planMethod;
          _this.updateData.planMan = this.baseData.planMan;
          _this.updateData.planDate = this.baseData.planDate;
          _this.updateData.memo = this.baseData.memo;
            _this.axios.post('pass/baseManagement/v1/limsisoplands/', _this.updateData, {individualType: 'json'}).then(res => {
                  if (res.succeed) {
                  this.$message.success('修改成功')
                  _this.dialogTableVisible = false;
                  _this.$emit('refresh2')
                  } else {
                  this.$message.error(res.message)
                  }
                  }).catch(e => {
                  this.$message.error(e.message)
                })
          }
        })
      },
     closeDialog () {
       let _this = this;
       _this.baseData = {

        }
     },
      tableHeaderCellStyle () {
      return 'background-color: rgb(249,249,249) !important;color: rgb(73, 72, 72);border-bottom: 1px solid #DCDFE7;border-top:3px solid #3ac1ea;'
      }
    },
    watch: {
      showFlag2 (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params1 !== null) {
          _this.baseData = _this.Params1;
          _this.updateData = _this.Params1;
          _this.tableType = 1;
          _this.tableTypeDsc = '修改质控计划';
        } else {
          _this.tableType = 0;
          _this.baseData.planNo = _this.Params2
          _this.tableTypeDsc = '新增质控计划';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
