<template>
  <div>
  <el-dialog v-dialogDrag title="备忘录" :visible.sync="visible" width="70%" @close="handleClose" :modal-append-to-body="false" :append-to-body="true" v-if="visible">
    <el-form class="diary" style="height:  100%;">
			<c-table :list="model.trndia.dias||[]" style="width:100%,text-align:center,margin-bottom:18px;height:calc(100% - 60px);" :border="true" :total="model.trndia.dias.length||0"
				:paginationShow="false" height="100%">
				<el-table-column prop="dat" label="提示日期" min-width="110px">
						<template slot-scope="scope">
							<el-form-item label-width="0" :prop="'trndia.dias.' + scope.$index + '.dat'">
								{{scope.row.dat}}
							</el-form-item>
						</template>
				</el-table-column>
				<el-table-column prop="cod" label="备忘录原因" min-width="140px">
						<template slot-scope="scope">
							<el-form-item label-width="0" :prop="'trndia.dias.' + scope.$index + '.cod'">
								<c-select-value-to-label v-model="scope.row.cod" dbCode="DIATXT" disabled>
								</c-select-value-to-label>
							</el-form-item>
						</template>
				</el-table-column>
				<el-table-column prop="objref" label="业务编号" min-width="150px">
				</el-table-column>
				<el-table-column prop="nam" label="描述" min-width="260px">
				</el-table-column>
				<el-table-column prop="frm" label="交易码" min-width="95px">
				</el-table-column>
				<el-table-column prop="diamodflg" label="备忘录状态" min-width="200px">
						<template slot-scope="scope">
							<el-form-item label-width="0" :prop="'trndia.dias.' + scope.$index + '.diamodflg'">
								<c-select-value-to-label v-model="scope.row.diamodflg" dbCode="DIAMOD" uil="CN" disabled>
								</c-select-value-to-label>
							</el-form-item>
						</template>
				</el-table-column>
				<el-table-column prop="ownusr" label="经办柜员" min-width="100px"></el-table-column>
				<el-table-column prop="ptyextkey" label="客户号" min-width="130px"></el-table-column>
				<el-table-column prop="ptynam" label="客户名称" min-width="200px"></el-table-column>
				<el-table-column prop="paycur" label="付款币种" min-width="100px"></el-table-column>
				<el-table-column prop="payamt" label="付款金额" min-width="100px">
						<template slot-scope="scope">
							<el-form-item label-width="0" :prop="'trndia.dias.' + scope.$index + '.payamt'">
								{{ moneyFormat(scope.row.payamt, scope.row.paycur) }}
							</el-form-item>
						</template>
				</el-table-column>
				<el-table-column prop="prechkdat" label="预计核验日期" width="110px"></el-table-column>
				<el-table-column label="" prop="det" width="180px" height="0px" fixed="right">
					<template slot-scope="scope" slot="header">
						<c-button circle style="padding: 4px" class="el-icon-plus" size="mini" @click="addNewDiary()">
						</c-button>
					</template>
					<template slot-scope="scope">
						<c-button size="mini" type="primary" @click="updateDiary(scope.row, scope.$index)">修改</c-button>
						<el-button size="mini" type="primary" style="margin-left: 5px" @click="viewDiary(scope.row)">查看</el-button>
						<c-button size="mini" type="primary" v-if="scope.row.donflg !== 'X'" style="margin-left: 5px" @click="deleteDiary(scope.row)">删除</c-button>
					</template>
				</el-table-column>
			</c-table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleClose()">退出</el-button>
    </span>
  </el-dialog>
  <!-- 弹框 -->
  <ViewDiaryDialog :visible="showViewDialog" :diaVo="diaVo" @closeViewDialog="closeViewDialog"/>
  <UpdateDiaryDialog :visible="showUpdateDialog" :diaVo="diaVo" @closeUpdateDialog="closeUpdateDialog" @updateDialog="updateDialog"/>
  <InsertDiaryDialog :visible="showInsertDialog" :diaVo="diaVo1" @closeInsertDialog="closeInsertDialog" @insertDialog="insertDialog"/>
  </div>
</template>
<script>
import Api from "~/service/Api";
import event from '../event';
import { cloneDeep } from 'lodash';
import commonDepend from "~/mixin/commonDepend.js";
import ViewDiaryDialog from "./ViewDiaryDialog.vue";
import UpdateDiaryDialog from "./UpdateDiaryDialog.vue";
import InsertDiaryDialog from "./InsertDiaryDialog.vue";

export default {
  inject: ["root"],
  mixins: [commonDepend, event],
  components: {
      ViewDiaryDialog,UpdateDiaryDialog,InsertDiaryDialog
	},
  props: ["model", "codes"],
  data() {
    return {
      visible: false,
      showViewDialog: false,
      showUpdateDialog: false,
      showInsertDialog: false,
      updateIndex: -1,
      diaVo:{},
      diaVo1:{},
    };
	},
  methods: {
    handleClose () {
      this.visible = false
    },
    closeViewDialog(){
      this.showViewDialog = false;
    },
    viewDiary(diary) {
      this.showViewDialog = true;
      console.log("查看diary数据", diary);
      this.diaVo = cloneDeep(diary);
    },
    deleteDiary(diary){
       this.$confirm(
					"您确定要删除备忘录数据 "+diary.nam+"吗?",
					"提示",
					{
						confirmButtonText: "确认",
						cancelButtonText: "取消",
						type: "warning"
					}
					).then(() => {
            this.delete(diary);
            this.$message.success("删除成功!");
					})
					.catch(() => {});
    },
    updateDiary(diary, index) {
      this.showUpdateDialog = true;
      console.log("更新diary数据", diary);
      console.log("更新diary数据下标", index);
      this.diaVo = cloneDeep(diary);
      if (this.diaVo.objref === '') {
         this.diaVo.objref = this.model.trndia.buildParams.rec.ownref;
      }
      this.updateIndex = index;
    },
    closeUpdateDialog(){
      this.showUpdateDialog = false;
    },
    updateDialog(diary){
      this.showUpdateDialog = false;
      Object.assign(this.model.trndia.dias[this.updateIndex],diary);
      this.update(this.model.trndia.dias[this.updateIndex]);
    },
    addNewDiary(){
      let diary = {
         diatyp: "MANUAL",
         donflg: "",
         cod:"",
         nam:"",
         frm:"",
         inftxt:"",
         diamodflg:"",
         objref: this.model.trndia.buildParams.rec.ownref
      };
      this.diaVo1 = cloneDeep(diary);
      this.showInsertDialog = true;
    },
    closeInsertDialog(){
      this.showInsertDialog = false;
    },
    insertDialog(diary){
      this.showInsertDialog = false;
      let newDiary = {};
      Object.assign(newDiary,diary);
      this.update(newDiary);
      console.log("新增的备忘录数据", newDiary)
      let start = 0;
      if (this.model.trndia.dias) {
        start = this.model.trndia.dias.length;
      }
      this.model.trndia.dias.splice(start, 0, newDiary);
    }
  }
};
</script>
<style lang="less" scoped>
.diary {
	/deep/ .eContainer-table-block {
		margin-top: 0!important;
		height: 100%!important;
	}
}
</style>