<template>
    <div>
        <el-form :inline="true" :model="query" class="query-form" size="mini">
            <el-row>
                <el-form-item class="query-form-item">
                    <el-input v-model="query.key" placeholder="搜索" style="width: 300px"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button-group>
                        <el-button
                            type="primary"
                            icon="el-icon-refresh"
                            @click="onReset"
                        ></el-button>
                        <el-button
                            type="primary"
                            icon="search"
                            width="1200px"
                            @click="getList"
                        >查询
                        </el-button
                        >
                    </el-button-group>
                </el-form-item>
            </el-row>
        </el-form>

        <el-table
            v-loading="loading"
            :data="list"
            style="width: 100%"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="序号" type="index"></el-table-column>
            <el-table-column
                label="自定义码"
                prop="erpId"
                show-overflow-tooltip
                width="100"
                v-if="erpIdColumn"
            ></el-table-column>
            <el-table-column
                label="第三方ID"
                prop="thirdId"
                show-overflow-tooltip
                width="100"
                v-if="thirdIdColumn"
            ></el-table-column>
            <el-table-column
                label="名称"
                prop="name"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="简写"
                prop="spell"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="社会信用号"
                prop="creditNo"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="地址"
                prop="addr"
                show-overflow-tooltip
            ></el-table-column>

            <el-table-column label="操作" fixed="right">
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        size="small"
                        @click.native="handleForm(scope.$index, scope.row)"
                    >详情
                    </el-button
                    >
                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="deleteDialog(scope.row)"
                    >删除
                    </el-button
                    >
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
            :page-size="query.limit"
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="total"
        ></el-pagination>

        <el-dialog
            title="选入往来单位信息"
            :visible.sync="selectDrugDialogVisible"
            width="85%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="selectDrugDialogVisible"
        >
            <selectUnit v-on:closeMain="closeMain"></selectUnit>
        </el-dialog>


        <!--表单-->
        <el-dialog
            :title="formMap[formName]"
            :visible.sync="formVisible"
            :before-close="hideForm"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            width="85%"
            top="5vh"
        >
            <el-form :model="formData" :rules="formRules" ref="dataForm">
                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <div class="text item">
                            <div class="text item">
                                <el-form-item label="名称" prop="name">
                                    <el-input v-model="formData.name" auto-complete="off" :disabled="true"></el-input>
                                </el-form-item>
                                <el-form-item label="社会信用号" prop="creditNo">
                                    <el-input v-model="formData.creditNo" auto-complete="off"
                                              :disabled="true"></el-input>
                                </el-form-item>
                                <el-form-item label="联系人" prop="contact">
                                    <el-input v-model="formData.contact" auto-complete="off"
                                              :disabled="true"></el-input>
                                </el-form-item>
                                <el-form-item label="地址" prop="addr">
                                    <el-input type="" v-model="formData.addr" auto-complete="off"
                                              :disabled="true"></el-input>
                                </el-form-item>
                            </div>
                        </div>
                    </el-col>
                    <el-col :span="10" class="el-col" type="flex">
                        <div class="text item">
                            <el-form-item label="往来单位编码" prop="erpId">
                                <el-input v-model="formData.erpId" auto-complete="off" :disabled="true"></el-input>
                            </el-form-item>
                            <el-form-item label="第三方往来单位ID" prop="thirdId">
                                <el-input v-model="formData.thirdId" auto-complete="off" :disabled="true"></el-input>
                            </el-form-item>
                            <el-form-item label="联系电话" prop="mobile">
                                <el-input v-model="formData.mobile" auto-complete="off" :disabled="true"></el-input>
                            </el-form-item>
                            <el-form-item v-if="spellUpdate" label="拼音码" prop="spell">
                                <el-input v-model="formData.spell" auto-complete="off" :disabled="true"></el-input>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="hideForm">取消</el-button>
                <el-button type="primary" @click.native="formSubmit()" :loading="formLoading">提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    getBasicUnitMaintains,
    combine,
    basicUnitMaintainSave,
    deleteBasicUnitMaintain,
} from "../../api/basic/basicUnitMaintain";
import selectUnit from "./basicUnitRelevance";

var pinyin = require("pinyin");

const formJson = {
    id: "",
    thirdId: "",
    erpId: "",
    name: "",
    spell: "",
    addr: "",
    status: "",
    type: "",
    creditNo: "",
    contact: "",
    mobile: ""
};
export default {
    data() {
        return {
            query: {
                key: "",
                page: 1,
                limit: 20,
            },
            erpIdColumn: false,
            thirdIdColumn: false,
            editQuery: null,
            checked: false,
            formName: null,
            formMap: {
                add: "新增",
                update: "编辑"
            },
            formLoading: false,
            formVisible: false,
            formData: formJson,
            formRules: {
                // name: [
                //     {required: true, message: "请输入名称", trigger: "blur"}
                // ],
                // erpId: [
                //     {required: true, message: "请输入自定义码", trigger: "blur"}
                // ],
                // creditNo: [
                //     {required: true, message: "请输入社会信用号", trigger: "blur"}
                // ]
            },
            spellUpdate: false,
            editDialogVisible: false,
            selectDrugDialogVisible: false,
            list: [],
            total: 0,
            multipleSelection: [],
        };
    },

    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.query = {
                key: "",
                page: 1,
                limit: 20,
            };
            this.getList();
        },
        getList() {
            this.loading = true;
            this.erpIdColumn = false;
            this.thirdIdColumn = false;
            getBasicUnitMaintains(this.query)
                .then((response) => {
                    this.loading = false;
                    this.erpIdColumn = response.data.BUMErpIdValue == "1" ? true : false;
                    this.thirdIdColumn = response.data.BUMThirdIdValue == "1" ? true : false;
                    this.list = response.data.page.list || [];
                    this.total = response.data.page.total || 0;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
        // 刷新表单
        resetForm() {
            if (this.$refs["dataForm"]) {
                // 清空验证信息表单
                this.$refs["dataForm"].clearValidate();
                // 刷新表单
                this.$refs["dataForm"].resetFields();
                this.getList();
            }
        },
        // 隐藏表单
        hideForm() {
            // 更改值
            this.formVisible = !this.formVisible;
            return true;
        },
        // 显示表单
        handleForm(index, row) {
            this.formVisible = true;
            this.formData = JSON.parse(JSON.stringify(formJson));
            if (row !== null) {
                this.formData = Object.assign({}, row);
            }
            this.formName = "add";
            this.spellUpdate = false;
            if (index !== null) {
                this.index = index;
                this.formName = "update";
                this.spellUpdate = true;
            }
        },
        formSubmit() {
            this.$refs["dataForm"].validate(valid => {
                if (valid) {
                    this.formLoading = true;
                    let data = Object.assign({}, this.formData);

                    if (!this.spellUpdate) {
                        var py = pinyin(data.name.trim(), {
                            style: pinyin.STYLE_FIRST_LETTER, // 设置拼音风格
                            heteronym: true
                        });
                        var pyStr = py.join('');
                        data.spell = pyStr.toUpperCase();
                    }

                    basicUnitMaintainSave(data, this.formName)
                        .then(response => {
                            this.formLoading = false;
                            if (response.code !== 20000) {
                                this.$message.error(response.message);
                                return false;
                            }
                            this.$message.success("操作成功");
                            this.formVisible = false;
                            if (this.formName === "add") {
                                // 向头部添加数据
                                if (response.data && response.data.id) {
                                    data.id = response.data.id;
                                    this.list.unshift(data);
                                }
                            } else {
                                this.list.splice(this.index, 1, data);
                            }
                            // 刷新表单
                            this.resetForm();
                            this.getList();
                        })
                        .catch(() => {
                            this.formLoading = false;
                        });
                }
            });
        },

        deleteUnit(data) {
            this.loading = true;
            let tquery = {
                id: data.id + "",
            };

            deleteBasicUnitMaintain(tquery)
                .then((response) => {
                    this.getList();
                    this.$message({
                        type: "success",
                        message: "删除成功！",
                    });
                })
                .catch(() => {
                });
        },

        cancelDialog() {
            this.editDialogVisible = false;
        },
        closeMain(val) {
            this.selectDrugDialogVisible = false;
            if (val) {
                this.$message.success("添加成功！");
                this.getList();
            } else {
                this.$message.error("添加失败！");
            }
        },

        handleModifyClick(row) {
            this.editDialogVisible = true;
            this.editQuery = row;
        },

        deleteDialog(rowId) {
            this.$confirm("此操作将永久删除该往来单位信息, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.deleteUnit(rowId);
                })
                .catch(() => {
                });
        },

        exportTxt() {
        },

        intentDetail() {
            // this.$router.push({ path: "../product/unitrevance" });
            this.selectDrugDialogVisible = true;
            //  this.$router.push({name:'home',query: {id:'1'}})
        },

        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        handleCurrentChange(val) {
            this.query.page = val;
            this.getList();
        },
    }
    ,
    mounted() {
    }
    ,
    components: {
        selectUnit,
    }
    ,
    created() {
        this.getList();
    }
    ,
}
;
</script>
