<template>
    <lj-win-common title="新建任务">
        <template slot="body">
            <lj-dictionary :data="mainObject" :columns="columns" :editing="true"
                           @action-row-click="handleActionRowClick" style="margin-bottom: 0;"/>
            <div class="lj-card-item">
                <div style="font-size:15px;margin: 10px 20px;color:#777" @click="handleFileAdd">
                    <i class="iconfont icon-fujian" style="font-size: 14px;"></i>
                    添加附件
                    <input v-show="false" id="fileinput" type="file" multiple="multiple" :value="inputFile"
                           @change="tirggerFile($event)">
                </div>
                <template v-for="(file,index) in fileList">
                    <lj-fj :fileFj="file" :clickEventDefault="false" deletable
                           @on-delete="fileList.splice(index,1)"></lj-fj>
                </template>
            </div>
        </template>
        <template slot="footer">
            <mu-button large class="lj-background-color-primary" @click="handleSave" style="width:100%;color: white;">
                <i class="iconfont icon-tongyi  lj-card-summary-icon"></i>
                确定
            </mu-button>
        </template>
    </lj-win-common>
</template>

<script>
    import LjWinCommon from "components/lj-win-common";
    import LjDictionary from "components/lj-dictionary";
    import LjFj from "components/lj-fj";

    export default {
        name: "workplan-edit",
        components: {LjFj, LjDictionary, LjWinCommon},
        data() {
            return {
                mainObject: {},
                fileList: [],
                inputFile: []
            }
        },
        created() {
            let user = $lj.getStorage("userInfo");
            this.mainObject = {
                kind: 0,
                needCheck: 1,
                check_empid: user.empid,
                check_empname: user.username
            };
        },
        methods: {
            handleFileAdd() {
                document.getElementById('fileinput').click();
            },
            tirggerFile(event) {
                let self = this;
                var reader = new FileReader();
                var file = event.target.files[0];
                reader.readAsDataURL(file);
                reader.onloadend = function () {
                    self.fileList.push({
                        base64: reader.result,
                        fileName: file.name,
                        fileType: file.name.substring(file.name.lastIndexOf('.')),
                        fileSize: file.size
                    });
                };
            },
            handleActionRowClick: function (eventData) {
                if (eventData.field === "custname") {//客户
                    let self = this;
                    $lj.removeEventListener("cust_select");
                    $lj.addEventListener("cust_select", function (ret) {
                        if (ret.value) {
                            let custInfo = ret.value;
                            if (custInfo.cusid) {
                                self.mainObject.relid = custInfo.cusid;
                                self.$set(self.mainObject, "custname", custInfo.name);
                                self.$set(self.mainObject, "relrep", custInfo.rep);
                                self.$set(self.mainObject, "reltele", custInfo.tele);
                                self.$set(self.mainObject, "address", custInfo.address);
                            }
                        }
                    });
                    $lj.openWin("cust", {
                        winMode: "single-select"
                    });
                }
                if(eventData.field === "empname"){
                  let idProp = "empid";
                  let nameProp = eventData.field;
                  let self = this;
                  $lj.removeEventListener("user_select");
                  $lj.addEventListener("user_select", function (ret) {
                    if (ret.value) {
                        let str_empnames = "";
                        let str_empids = "";
                        let tempusers = ret.value
                        if (typeof tempusers == 'string') {
                            tempusers = JSON.parse(ret.value)
                        }
                        for(var i = 0;i<tempusers.length;i++){
                            if(tempusers[i].username && tempusers[i].empid){
                              str_empids = str_empids + tempusers[i].empid + ',';
                              str_empnames = str_empnames + tempusers[i].username + ',';
                            }
                        }
                        if (str_empnames && str_empids) {
                            self.$set(self.mainObject, idProp, 0);
                            self.$set(self.mainObject, "plangroup_empid", str_empids);
                            self.$set(self.mainObject, "plangroup_emp", str_empnames);
                            self.$set(self.mainObject, nameProp, str_empnames);
                        }
                    }
                  });
                  $lj.openWin("user", {
                      winMode: "multiple-select"
                  });
                }
                if (eventData.field === "check_empname") {
                    let idProp = "check_empid";
                    let nameProp = eventData.field;
                    let self = this;
                    $lj.removeEventListener("user_select");
                    $lj.addEventListener("user_select", function (ret) {
                        if (ret.value) {
                            let userInfo = ret.value;
                            if (userInfo.userid) {
                                self.$set(self.mainObject, idProp, userInfo.empid);
                                self.$set(self.mainObject, nameProp, userInfo.username);
                            }
                        }
                    });
                    $lj.openWin("user", {
                        winMode: "single-select"
                    });
                }
            },
            // handleFjClick(file) {
            //     $lj.openFileByUrl(file.url);
            // },
            handleSave() {
                if (!this.mainObject.empid && !this.mainObject.plangroup_empid) {
                    alert("请先选择任务人");
                    return;
                }
                let requestBody = {
                    token: $lj.getStorage("token"),
                    oaworkplan: Object.assign({}, this.mainObject),
                    fileList: this.fileList
                };
                if (!this.mainObject.needCheck) {
                    requestBody.oaworkplan.check_empid = 0;
                    requestBody.oaworkplan.check_empname = "";
                }
                $lj.postLJRequest("SaveL1OaWorkPlan", requestBody, function (ret) {
                    if (ret) {
                        $lj.openWin("workplan-detail", {mainObject: {planid: ret.planid}});
                        setTimeout(function () {
                            $lj.closeWin();
                        }, 2000);
                    }
                });
            }
        },
        computed: {
            columns() {
                let result = [
                    {
                        label: "类型", property: "kind", type: "Select", selectOptions: {
                            valueMap: ["kind", "kindStr"],
                            options: [{kind: 0, kindStr: "临时任务"},
                                {kind: 1, kindStr: "客户走访"},
                                // {kind: 2, kindStr: "稽核"}
                            ]
                        }
                    },
                    {label: "任务人", property: "empname", type: "Action"},
                    // {
                    //     label: "任务人", property: "empid",
                    //     type: "Select",
                    //     selectOptions: {
                    //         valueMap: ["empid", "empname"],
                    //         optionsApi: "GetL1EmpList",
                    //         apiParams: {onlyUser: 1}
                    //     }
                    // }
                ];
                if (this.mainObject.kind === 1) {
                    result.push({label: "客户", property: "custname", type: "Action"});
                    result.push({label: "联系人", property: "relrep"});
                    result.push({label: "电话", property: "reltele"});
                    result.push({label: "地址", property: "address"});
                    result.push({
                        label: "走访类型",
                        property: "interviewform",
                        type: "Select",
                        selectOptions: {options: ["电话回访", "上门走访"]}
                    });
                }
                result.push({label: "任务", property: "content", inputLine: 5});
                result.push({
                    label: "时间",
                    property: "stime",
                    type: "DateTime",
                    inlineDesc: "开始",
                    format: "YYYY-MM-DD HH:mm"
                });
                result.push({
                    label: "",
                    property: "etime",
                    type: "DateTime",
                    inlineDesc: "结束",
                    format: "YYYY-MM-DD HH:mm"
                });
                result.push({
                    label: "重复", property: "planrule", type: "Select", selectOptions: {
                        valueMap: ["planrule", "planruleDscrp"],
                        options: [
                            {planrule: 0, planruleDscrp: "一次"},
                            {planrule: 1, planruleDscrp: "本月每周同日"},
                            {planrule: 2, planruleDscrp: "本年每周同日"},
                            {planrule: 3, planruleDscrp: "本年每月同日"}
                        ]
                    }
                });
                result.push({
                    label: "提醒", property: "prehours", type: "Select", selectOptions: {
                        valueMap: ["prehours", "dscrp"],
                        options: [
                            {prehours: 0.25, dscrp: "提前15分钟"},
                            {prehours: 0.5, dscrp: "提前30分钟"},
                            {prehours: 1, dscrp: "提前1小时"},
                            {prehours: 2, dscrp: "提前2小时"},
                            {prehours: 0, dscrp: "无"},
                        ]
                    }
                });
                result.push({label: "需要验收", property: "needCheck", type: "Boolean"});
                if (this.mainObject.needCheck) {
                    result.push({label: "验收人", property: "check_empname", type: "Action"});
                    // result.push({
                    //     label: "验收人",
                    //     property: "check_empid",
                    //     type: "Select",
                    //     selectOptions: {
                    //         valueMap: ["empid", "empname"],
                    //         optionsApi: "GetL1EmpList",
                    //         apiParams: {onlyUser: 1}
                    //     }
                    // });
                }
                return result;
            },
            kind() {
                return this.mainObject.kind;
            }
        },
        watch: {
            kind(newVal, oldVal) {
                if (newVal !== oldVal) {
                    this.mainObject.needCheck = newVal !== 1 ? 1 : 0;
                }
            }
        }
    }
</script>

<style scoped>

</style>
