<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="planStdNo" label="计划编号">
              <el-input v-model="baseData.planStdNo" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="planName" label="计划名称">
              <el-input v-model="baseData.planName" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="qltTypeName" label="质量类型">
              <el-select
                v-model="baseData.qltTypeCode"
                clearable
                filterable
                style="width: 100%"
                collapse-tags
                size="small"
              >
                <el-option
                  v-for="item in qualityType"
                  :key="item.baseCode"
                  :label="item.baseName"
                  :value="item.baseCode"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="8">
            <el-form-item prop="planDesc" label="计划描述">
              <el-input v-model="baseData.planDesc" 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="filePath" label="文件">
              <el-input v-model="baseData.filePath" style="width: 100%" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="8">
            <el-form-item prop="companyNo" label="管理单位">
              <el-select
                v-model="baseData.companyNo"
                clearable
                filterable
                style="width: 100%"
                collapse-tags
                @change="selectDepartment"
                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 label="管理车间">
              <el-select
                v-model="baseData.deptNo"
                clearable
                filterable
                style="width: 100%"
                collapse-tags
                @change="selectPlanMent"
                size="small"
              >
                <el-option
                  v-for="item in deptNameType"
                  :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 label="管理班组">
              <el-select
                v-model="baseData.sectionNo"
                clearable
                filterable
                style="width: 100%"
                collapse-tags
                size="small"
              >
                <el-option
                  v-for="item in sectionNameType"
                  :key="item.orgCode"
                  :label="item.orgName"
                  :value="item.orgCode"
                >
                </el-option>
              </el-select>
            </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: ['showFlag', 'Params'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增质控计划',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            updateData:{},
            companyNameTypeIndex: 0,
            companyNameType: [],
            deptNameType: [],
            sectionNameType: [], //计划班组数据列表
            qualityType: [], //质控类型
            tableData: [],
            baseData: {
              planStdNo: '',
              planName: '',
              qltTypeName: '',
              companyNo: '',
              companyName: '',
              deptNo: '',
              deptName: '',
              sectionNo: '',
              sectionName: '',
              planDesc: '',
              planDate: new Date(),
              filePath: '',
              memo: ''
            },
            ruleValidate: {
              merchName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
             }
        }
    },
    mounted () {
        //获取公司
        this.axios
        .post(
            "pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",
            {},
            { individualType: "json" }
        )
        .then((response) => {
            this.companyNameType = response.data;
        });
        this.axios
        .post(
            "pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",
            { validFlag: 1, baseCode: 4853 },
            { individualType: "json" }
        )
        .then((res) => {
            this.qualityType = res.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.tableData = [];
        _this.baseData = {
              
        }
        _this.dialogTableVisible = false
      },
      selectTriggerC (val) {
        let _this = this;
        _this.baseData.companyNo = _this.companyNameType[val].companyNo
      },
      //查询选中的公司的部门名称数据
    selectDepartment(val) {
      this.deptNameType = [];
      this.companyNameType.find((item) => {
        if (val === item.orgCode) {
          this.deptNameType = item.sysOrgList;
        }
      });
    },
    //根据计划车间联动查询计划班组数据
      selectPlanMent(val) {
      this.sectionNameType = []
      this.deptNameType.find((item) => {
        if (val === item.orgCode) {
          this.sectionNameType = item.sysOrgList;
        }
      });
    },
      // TODO addData
      addData () {
        let _this = this;
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
            this.baseData.companyName = this.getCode(
            this.baseData.companyNo,
            this.companyNameType,
            "orgCode",
            "orgName"
            );
            this.baseData.deptName = this.getCode(
            this.baseData.deptNo,
            this.deptNameType,
            "orgCode",
            "orgName"
            );
            this.baseData.sectionName = this.getCode(
            this.baseData.sectionNo,
            this.sectionNameType,
            "orgCode",
            "orgName"
            );
            this.baseData.qltTypeName = this.getCode(
            this.baseData.qltTypeCode,
            this.qualityType,
            "baseCode",
            "baseName"
            );
            let obj = {
              planName:_this.baseData.planName,
              planDesc:_this.baseData.planDesc,
              planDate:_this.baseData.planDate,
              qltTypeCode:_this.baseData.qltTypeCode,
              qltTypeName:_this.baseData.qltTypeName,
              companyNo:_this.baseData.companyNo,
              companyName:_this.baseData.companyName,
              deptNo:_this.baseData.deptNo,
              deptName:_this.baseData.deptName,
              sectionNo:_this.baseData.sectionNo,
              sectionName:_this.baseData.sectionName,
              filePath:_this.baseData.filePath,
              memo:_this.baseData.memo,
              planStdNo:_this.baseData.planStdNo,
              validFlag:'1',
              stateFlag:'0',
              createMan:userName,
              createNo:userId,
              createTime:new Date(),
            }
             _this.axios.post('pass/baseManagement/v1/limsisoplans/', obj, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                 this.$message.success('添加成功')
                 _this.dialogTableVisible = false;
                 _this.$emit('refresh')
                } 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.companyName = this.getCode(
            this.baseData.companyNo,
            this.companyNameType,
            "orgCode",
            "orgName"
            );
            _this.updateData.deptName = this.getCode(
            this.baseData.deptNo,
            this.deptNameType,
            "orgCode",
            "orgName"
            );
            _this.updateData.sectionName = this.getCode(
            this.baseData.sectionNo,
            this.sectionNameType,
            "orgCode",
            "orgName"
            );
            _this.updateData.qltTypeName = this.getCode(
            this.baseData.qltTypeCode,
            this.qualityType,
            "baseCode",
            "baseName"
            );
            _this.updateData.planName = _this.baseData.planName,
            _this.updateData.planDesc = _this.baseData.planDesc,
            _this.updateData.planDate = _this.baseData.planDate,
            _this.updateData.qltTypeCode = _this.baseData.qltTypeCode,
            _this.updateData.qltTypeName = _this.baseData.qltTypeName,
            _this.updateData.companyNo = _this.baseData.companyNo,
            _this.updateData.companyName = _this.baseData.companyName,
            _this.updateData.deptNo = _this.baseData.deptNo,
            _this.updateData.deptName = _this.baseData.deptName,
            _this.updateData.sectionNo = _this.baseData.sectionNo,
            _this.updateData.sectionName = _this.baseData.sectionName,
            _this.updateData.filePath = _this.baseData.filePath,
            _this.updateData.memo = _this.baseData.memo,
            _this.updateData.planStdNo = _this.baseData.planStdNo,
            _this.updateData.updateName = userName,
            _this.updateData.updateNo = userId,
            _this.updateData.updateTime = new Date(),
            _this.axios.post('pass/baseManagement/v1/limsisoplans/', _this.updateData, {individualType: 'json'}).then(res => {
                  if (res.succeed) {
                  this.$message.success('修改成功')
                  _this.dialogTableVisible = false;
                  _this.$emit('refresh')
                  } else {
                  this.$message.error(res.message)
                  }
                  }).catch(e => {
                  this.$message.error(e.message)
                })
          }
        })
      },
     closeDialog () {
       let _this = this;
       _this.tableData = [];
       _this.baseData = {

        }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
     },
      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: {
      showFlag (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params !== null) {
          _this.baseData = _this.Params;
          _this.updateData = _this.Params;
          _this.tableType = 1;
          _this.tableTypeDsc = '修改质控计划';
          _this.seen = false;
          _this.beInvalid = true;
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.tableTypeDsc = '新增质控计划';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
